shithub: cstory

Download patch

ref: f7c1ca86c3ca6ff0ac81e94e03cfa046b321fe4d
parent: 58a3974e1a648a235a136aa2249c2bab22ed3e91
author: Clownacy <Clownacy@users.noreply.github.com>
date: Sat May 4 21:57:23 EDT 2019

Remove Resource.cpp's dependence on SDL2

This also makes its API a lot more similar to Windows'

--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -354,10 +354,13 @@
 
 static BOOL LoadBitmap_Resource(const char *res, Surface_Ids surf_no, bool create_surface)
 {
-	SDL_RWops *fp = FindResource(res);
+	size_t size;
+	const unsigned char *data = FindResource(res, "BITMAP", &size);
 	
-	if (fp)
+	if (data)
 	{
+		SDL_RWops *fp = SDL_RWFromConstMem(data, size);
+
 		printf("Loading surface from resource %s for surface id %d\n", res, surf_no);
 		if (LoadBitmap(fp, surf_no, create_surface))
 			return TRUE;
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -212,10 +212,13 @@
 		RECT unused_rect = {0, 0, 320, 240};
 
 		//Load cursor
-		SDL_RWops *fp = FindResource("CURSOR_NORMAL");
+		size_t size;
+		const unsigned char *data = FindResource("CURSOR_NORMAL", "CURSOR", &size);
 		
-		if (fp)
+		if (data)
 		{
+			SDL_RWops *fp = SDL_RWFromConstMem(data, size);
+
 			SDL_Surface *cursor_surface = SDL_LoadBMP_RW(fp, 1);
 			SDL_SetColorKey(cursor_surface, SDL_TRUE, SDL_MapRGB(cursor_surface->format, 0xFF, 0, 0xFF));	// Pink regions are transparent
 
@@ -314,10 +317,12 @@
 			
 #ifndef WINDOWS
 			//Load icon
-			SDL_RWops *fp = FindResource("ICON_MINI");
+			size_t size;
+			const unsigned char *data = FindResource("ICON_MINI", "ICON", &size);
 			
-			if (fp)
+			if (data)
 			{
+				SDL_RWops *fp = SDL_RWFromConstMem(data, size);
 				SDL_Surface *iconSurf = SDL_LoadBMP_RW(fp, 1);
 				SDL_Surface *iconConverted = SDL_ConvertSurfaceFormat(iconSurf, SDL_PIXELFORMAT_RGB888, 0);
 				SDL_FreeSurface(iconSurf);
--- a/src/Organya.cpp
+++ b/src/Organya.cpp
@@ -261,19 +261,16 @@
 //Handling WAVE100
 int8_t wave_data[100][0x100];
 
-bool InitWaveData100()
+BOOL InitWaveData100()
 {
-	SDL_RWops *fp = FindResource("WAVE100");
-	if (fp == NULL)
-	{
-		printf("Failed to open WAVE100\n");
-		return false;
-	}
+	const unsigned char *data = FindResource("WAVE100", "WAVE", NULL);
 
-	fp->read(fp, wave_data, 1, 100 * 0x100);
+	if (data == NULL)
+		return FALSE;
 
-	SDL_RWclose(fp);
-	return true;
+	memcpy(wave_data, data, 100 * 0x100);
+
+	return TRUE;
 }
 
 //Create org wave
@@ -409,6 +406,9 @@
 	play_p = x;
 }
 
+#define READ_LE16(pointer) pointer[0] | (pointer[1] << 8); pointer += 2;
+#define READ_LE32(pointer) pointer[0] | (pointer[1] << 8) | (pointer[2] << 16) | (pointer[3] << 24); pointer += 4;
+
 //Load organya file
 void LoadOrganya(const char *name)
 {
@@ -426,19 +426,14 @@
 
 	//Open file
 	printf("Loading org %s\n", name);
-	SDL_RWops *fp = FindResource(name);
+	const unsigned char *p = FindResource(name, "ORG", NULL);
 
-	if (!fp)
-	{
-		printf("Failed to open %s\n", name);
-		return;
-	}
-
 	//Version Check
 	uint8_t ver = 0;
 	char pass_check[6];
 
-	SDL_RWread(fp, &pass_check[0], sizeof(char), 6);
+	memcpy(pass_check, p, 6);
+	p += 6;
 
 	if (!memcmp(pass_check, "Org-01", 6))ver = 1;
 	if (!memcmp(pass_check, "Org-02", 6))ver = 2;
@@ -446,23 +441,23 @@
 
 	if (!ver)
 	{
-		printf("Failed to open.org, invalid version %s", pass_check);
+		printf("Failed to open .org, invalid version %s", pass_check);
 		return;
 	}
 	
 	//Set song information
-	info.wait = SDL_ReadLE16(fp);
-	info.line = SDL_ReadU8(fp);
-	info.dot = SDL_ReadU8(fp);
-	info.repeat_x = SDL_ReadLE32(fp);
-	info.end_x = SDL_ReadLE32(fp);
+	info.wait = READ_LE16(p);
+	info.line = *p++;
+	info.dot = *p++;
+	info.repeat_x = READ_LE32(p);
+	info.end_x = READ_LE32(p);
 	
 	for (int i = 0; i < 16; i++) {
-		info.tdata[i].freq = SDL_ReadLE16(fp);
-		info.tdata[i].wave_no = SDL_ReadU8(fp);
-		const int8_t pipi = SDL_ReadU8(fp);
+		info.tdata[i].freq = READ_LE16(p);
+		info.tdata[i].wave_no = *p++;
+		const int8_t pipi = *p++;
 		info.tdata[i].pipi = ver == 1 ? 0 : pipi;
-		info.tdata[i].note_num = SDL_ReadLE16(fp);
+		info.tdata[i].note_num = READ_LE16(p);
 	}
 
 	//Load notes
@@ -495,36 +490,34 @@
 		//Set note properties
 		np = info.tdata[j].note_p; //X position
 		for (int i = 0; i < info.tdata[j].note_num; i++) {
-			np->x = SDL_ReadLE32(fp);
+			np->x = READ_LE32(p);
 			np++;
 		}
 
 		np = info.tdata[j].note_p; //Y position
 		for (int i = 0; i < info.tdata[j].note_num; i++) {
-			np->y = SDL_ReadU8(fp);
+			np->y = *p++;
 			np++;
 		}
 
 		np = info.tdata[j].note_p; //Length
 		for (int i = 0; i < info.tdata[j].note_num; i++) {
-			np->length = SDL_ReadU8(fp);
+			np->length = *p++;
 			np++;
 		}
 
 		np = info.tdata[j].note_p; //Volume
 		for (int i = 0; i < info.tdata[j].note_num; i++) {
-			np->volume = SDL_ReadU8(fp);
+			np->volume = *p++;
 			np++;
 		}
 
 		np = info.tdata[j].note_p; //Pan
 		for (int i = 0; i < info.tdata[j].note_num; i++) {
-			np->pan = SDL_ReadU8(fp);
+			np->pan = *p++;
 			np++;
 		}
 	}
-
-	SDL_RWclose(fp);
 
 	//Create waves
 	for (int j = 0; j < 8; j++)
--- a/src/Resource.cpp
+++ b/src/Resource.cpp
@@ -1,11 +1,8 @@
 #include "Resource.h"
 
-#include <stdint.h>
-#include <stdio.h>
+#include <stddef.h>
 #include <string.h>
 
-#include <SDL_rwops.h>
-
 #include "WindowsWrapper.h"
 
 #include "Resource/ORG/Access.org.h"
@@ -79,362 +76,102 @@
 #include "Resource/CURSOR/CURSOR_IKA.bmp.h"
 #include "Resource/CURSOR/CURSOR_NORMAL.bmp.h"
 
-const unsigned char* GetResource(const char *name, size_t *size)
+static const struct
 {
-	//ORG
-	if (!strcmp(name, "ACCESS"))
-	{
-		*size = sizeof(rAccess);
-		return rAccess;
-	}
-	if (!strcmp(name, "ANZEN"))
-	{
-		*size = sizeof(rAnzen);
-		return rAnzen;
-	}
-	if (!strcmp(name, "BALCONY"))
-	{
-		*size = sizeof(rBalcony);
-		return rBalcony;
-	}
-	if (!strcmp(name, "BALLOS"))
-	{
-		*size = sizeof(rBallos);
-		return rBallos;
-	}
-	if (!strcmp(name, "BDOWN"))
-	{
-		*size = sizeof(rBreakDown);
-		return rBreakDown;
-	}
-	if (!strcmp(name, "CEMETERY"))
-	{
-		*size = sizeof(rCemetery);
-		return rCemetery;
-	}
-	if (!strcmp(name, "CURLY"))
-	{
-		*size = sizeof(rCurly);
-		return rCurly;
-	}
-	if (!strcmp(name, "DR"))
-	{
-		*size = sizeof(rDr);
-		return rDr;
-	}
-	if (!strcmp(name, "ENDING"))
-	{
-		*size = sizeof(rEnding);
-		return rEnding;
-	}
-	if (!strcmp(name, "ESCAPE"))
-	{
-		*size = sizeof(rEscape);
-		return rEscape;
-	}
-	if (!strcmp(name, "FANFALE1"))
-	{
-		*size = sizeof(rFanfale1);
-		return rFanfale1;
-	}
-	if (!strcmp(name, "FANFALE2"))
-	{
-		*size = sizeof(rFanfale2);
-		return rFanfale2;
-	}
-	if (!strcmp(name, "FANFALE3"))
-	{
-		*size = sizeof(rFanfale3);
-		return rFanfale3;
-	}
-	if (!strcmp(name, "FIREEYE"))
-	{
-		*size = sizeof(rFireEye);
-		return rFireEye;
-	}
-	if (!strcmp(name, "GAMEOVER"))
-	{
-		*size = sizeof(rGameover);
-		return rGameover;
-	}
-	if (!strcmp(name, "GINSUKE"))
-	{
-		*size = sizeof(rGinsuke);
-		return rGinsuke;
-	}
-	if (!strcmp(name, "GRAND"))
-	{
-		*size = sizeof(rGrand);
-		return rGrand;
-	}
-	if (!strcmp(name, "GRAVITY"))
-	{
-		*size = sizeof(rGravity);
-		return rGravity;
-	}
-	if (!strcmp(name, "HELL"))
-	{
-		*size = sizeof(rHell);
-		return rHell;
-	}
-	if (!strcmp(name, "IRONH"))
-	{
-		*size = sizeof(rironH);
-		return rironH;
-	}
-	if (!strcmp(name, "JENKA"))
-	{
-		*size = sizeof(rJenka);
-		return rJenka;
-	}
-	if (!strcmp(name, "JENKA2"))
-	{
-		*size = sizeof(rJenka2);
-		return rJenka2;
-	}
-	if (!strcmp(name, "KODOU"))
-	{
-		*size = sizeof(rKodou);
-		return rKodou;
-	}
-	if (!strcmp(name, "LASTBT3"))
-	{
-		*size = sizeof(rLastBtl3);
-		return rLastBtl3;
-	}
-	if (!strcmp(name, "LASTBTL"))
-	{
-		*size = sizeof(rLastBtl);
-		return rLastBtl;
-	}
-	if (!strcmp(name, "LASTCAVE"))
-	{
-		*size = sizeof(rLastCave);
-		return rLastCave;
-	}
-	if (!strcmp(name, "MARINE"))
-	{
-		*size = sizeof(rMarine);
-		return rMarine;
-	}
-	if (!strcmp(name, "MAZE"))
-	{
-		*size = sizeof(rMaze);
-		return rMaze;
-	}
-	if (!strcmp(name, "MDOWN2"))
-	{
-		*size = sizeof(rMDown2);
-		return rMDown2;
-	}
-	if (!strcmp(name, "MURA"))
-	{
-		*size = sizeof(rMura);
-		return rMura;
-	}
-	if (!strcmp(name, "OSIDE"))
-	{
-		*size = sizeof(rOside);
-		return rOside;
-	}
-	if (!strcmp(name, "PLANT"))
-	{
-		*size = sizeof(rPlant);
-		return rPlant;
-	}
-	if (!strcmp(name, "QUIET"))
-	{
-		*size = sizeof(rquiet);
-		return rquiet;
-	}
-	if (!strcmp(name, "REQUIEM"))
-	{
-		*size = sizeof(rRequiem);
-		return rRequiem;
-	}
-	if (!strcmp(name, "TOROKO"))
-	{
-		*size = sizeof(rToroko);
-		return rToroko;
-	}
-	if (!strcmp(name, "VIVI"))
-	{
-		*size = sizeof(rVivi);
-		return rVivi;
-	}
-	if (!strcmp(name, "WANPAK2"))
-	{
-		*size = sizeof(rWanpak2);
-		return rWanpak2;
-	}
-	if (!strcmp(name, "WANPAKU"))
-	{
-		*size = sizeof(rWanpaku);
-		return rWanpaku;
-	}
-	if (!strcmp(name, "WEED"))
-	{
-		*size = sizeof(rWeed);
-		return rWeed;
-	}
-	if (!strcmp(name, "WHITE"))
-	{
-		*size = sizeof(rWhite);
-		return rWhite;
-	}
-	if (!strcmp(name, "XXXX"))
-	{
-		*size = sizeof(rXXXX);
-		return rXXXX;
-	}
-	if (!strcmp(name, "ZONBIE"))
-	{
-		*size = sizeof(rZonbie);
-		return rZonbie;
-	}
-	
-	//WAVE
-	if (!strcmp(name, "WAVE100"))
-	{
-		*size = sizeof(rWave);
-		return rWave;
-	}
-	
-	//Bitmap
-	if (!strcmp(name, "CREDIT01"))
-	{
-		*size = sizeof(rCredit01);
-		return rCredit01;
-	}
-	if (!strcmp(name, "CREDIT02"))
-	{
-		*size = sizeof(rCredit02);
-		return rCredit02;
-	}
-	if (!strcmp(name, "CREDIT03"))
-	{
-		*size = sizeof(rCredit03);
-		return rCredit03;
-	}
-	if (!strcmp(name, "CREDIT04"))
-	{
-		*size = sizeof(rCredit04);
-		return rCredit04;
-	}
-	if (!strcmp(name, "CREDIT05"))
-	{
-		*size = sizeof(rCredit05);
-		return rCredit05;
-	}
-	if (!strcmp(name, "CREDIT06"))
-	{
-		*size = sizeof(rCredit06);
-		return rCredit06;
-	}
-	if (!strcmp(name, "CREDIT07"))
-	{
-		*size = sizeof(rCredit07);
-		return rCredit07;
-	}
-	if (!strcmp(name, "CREDIT08"))
-	{
-		*size = sizeof(rCredit08);
-		return rCredit08;
-	}
-	if (!strcmp(name, "CREDIT09"))
-	{
-		*size = sizeof(rCredit09);
-		return rCredit09;
-	}
-	if (!strcmp(name, "CREDIT10"))
-	{
-		*size = sizeof(rCredit10);
-		return rCredit10;
-	}
-	if (!strcmp(name, "CREDIT11"))
-	{
-		*size = sizeof(rCredit11);
-		return rCredit11;
-	}
-	if (!strcmp(name, "CREDIT12"))
-	{
-		*size = sizeof(rCredit12);
-		return rCredit12;
-	}
-	if (!strcmp(name, "CREDIT14"))
-	{
-		*size = sizeof(rCredit14);
-		return rCredit14;
-	}
-	if (!strcmp(name, "CREDIT15"))
-	{
-		*size = sizeof(rCredit15);
-		return rCredit15;
-	}
-	if (!strcmp(name, "CREDIT16"))
-	{
-		*size = sizeof(rCredit16);
-		return rCredit16;
-	}
-	if (!strcmp(name, "CREDIT17"))
-	{
-		*size = sizeof(rCredit17);
-		return rCredit17;
-	}
-	if (!strcmp(name, "CREDIT18"))
-	{
-		*size = sizeof(rCredit18);
-		return rCredit18;
-	}
-	if (!strcmp(name, "PIXEL"))
-	{
-#ifdef JAPANESE
-		*size = sizeof(rpixel_jp);
-		return rpixel_jp;
+	const char *type;
+	const char *name;
+	const unsigned char *data;
+	size_t size;
+} resources[] = {
+	{"ORG", "ACCESS", rAccess, sizeof(rAccess)},
+	{"ORG", "ANZEN", rAnzen, sizeof(rAnzen)},
+	{"ORG", "BALCONY", rBalcony, sizeof(rBalcony)},
+	{"ORG", "BALLOS", rBallos, sizeof(rBallos)},
+	{"ORG", "BDOWN", rBreakDown, sizeof(rBreakDown)},
+	{"ORG", "CEMETERY", rCemetery, sizeof(rCemetery)},
+	{"ORG", "CURLY", rCurly, sizeof(rCurly)},
+	{"ORG", "DR", rDr, sizeof(rDr)},
+	{"ORG", "ENDING", rEnding, sizeof(rEnding)},
+	{"ORG", "ESCAPE", rEscape, sizeof(rEscape)},
+	{"ORG", "FANFALE1", rFanfale1, sizeof(rFanfale1)},
+	{"ORG", "FANFALE2", rFanfale2, sizeof(rFanfale2)},
+	{"ORG", "FANFALE3", rFanfale3, sizeof(rFanfale3)},
+	{"ORG", "FIREEYE", rFireEye, sizeof(rFireEye)},
+	{"ORG", "GAMEOVER", rGameover, sizeof(rGameover)},
+	{"ORG", "GINSUKE", rGinsuke, sizeof(rGinsuke)},
+	{"ORG", "GRAND", rGrand, sizeof(rGrand)},
+	{"ORG", "GRAVITY", rGravity, sizeof(rGravity)},
+	{"ORG", "HELL", rHell, sizeof(rHell)},
+	{"ORG", "IRONH", rironH, sizeof(rironH)},
+	{"ORG", "JENKA", rJenka, sizeof(rJenka)},
+	{"ORG", "JENKA2", rJenka2, sizeof(rJenka2)},
+	{"ORG", "KODOU", rKodou, sizeof(rKodou)},
+	{"ORG", "LASTBT3", rLastBtl3, sizeof(rLastBtl3)},
+	{"ORG", "LASTBTL", rLastBtl, sizeof(rLastBtl)},
+	{"ORG", "LASTCAVE", rLastCave, sizeof(rLastCave)},
+	{"ORG", "MARINE", rMarine, sizeof(rMarine)},
+	{"ORG", "MAZE", rMaze, sizeof(rMaze)},
+	{"ORG", "MDOWN2", rMDown2, sizeof(rMDown2)},
+	{"ORG", "MURA", rMura, sizeof(rMura)},
+	{"ORG", "OSIDE", rOside, sizeof(rOside)},
+	{"ORG", "PLANT", rPlant, sizeof(rPlant)},
+	{"ORG", "QUIET", rquiet, sizeof(rquiet)},
+	{"ORG", "REQUIEM", rRequiem, sizeof(rRequiem)},
+	{"ORG", "TOROKO", rToroko, sizeof(rToroko)},
+	{"ORG", "VIVI", rVivi, sizeof(rVivi)},
+	{"ORG", "WANPAK2", rWanpak2, sizeof(rWanpak2)},
+	{"ORG", "WANPAKU", rWanpaku, sizeof(rWanpaku)},
+	{"ORG", "WEED", rWeed, sizeof(rWeed)},
+	{"ORG", "WHITE", rWhite, sizeof(rWhite)},
+	{"ORG", "XXXX", rXXXX, sizeof(rXXXX)},
+	{"ORG", "ZONBIE", rZonbie, sizeof(rZonbie)},
+
+	{"WAVE", "WAVE100", rWave, sizeof(rWave)},
+
+	{"BITMAP", "CREDIT01", rCredit01, sizeof(rCredit01)},
+	{"BITMAP", "CREDIT02", rCredit02, sizeof(rCredit02)},
+	{"BITMAP", "CREDIT03", rCredit03, sizeof(rCredit03)},
+	{"BITMAP", "CREDIT04", rCredit04, sizeof(rCredit04)},
+	{"BITMAP", "CREDIT05", rCredit05, sizeof(rCredit05)},
+	{"BITMAP", "CREDIT06", rCredit06, sizeof(rCredit06)},
+	{"BITMAP", "CREDIT07", rCredit07, sizeof(rCredit07)},
+	{"BITMAP", "CREDIT08", rCredit08, sizeof(rCredit08)},
+	{"BITMAP", "CREDIT09", rCredit09, sizeof(rCredit09)},
+	{"BITMAP", "CREDIT10", rCredit10, sizeof(rCredit10)},
+	{"BITMAP", "CREDIT11", rCredit11, sizeof(rCredit11)},
+	{"BITMAP", "CREDIT12", rCredit12, sizeof(rCredit12)},
+	{"BITMAP", "CREDIT14", rCredit14, sizeof(rCredit14)},
+	{"BITMAP", "CREDIT15", rCredit15, sizeof(rCredit15)},
+	{"BITMAP", "CREDIT16", rCredit16, sizeof(rCredit16)},
+	{"BITMAP", "CREDIT17", rCredit17, sizeof(rCredit17)},
+	{"BITMAP", "CREDIT18", rCredit18, sizeof(rCredit18)},
+#ifdef JAPANESE
+	{"BITMAP", "PIXEL", rpixel_jp, sizeof(rpixel_jp)},
 #else
-		*size = sizeof(rpixel);
-		return rpixel;
+	{"BITMAP", "PIXEL", rpixel, sizeof(rpixel)},
 #endif
-	}
-	
+
 #ifndef WINDOWS
-	//ICON
-	if (!strcmp(name, "ICON_MINI"))
-	{
-		*size = sizeof(rICON_MINI);
-		return rICON_MINI;
-	}
+	{"ICON", "ICON_MINI", rICON_MINI, sizeof(rICON_MINI)},
 #endif
 
-	//CURSOR
-	if (!strcmp(name, "CURSOR_NORMAL"))
-	{
-		*size = sizeof(rCURSOR_NORMAL);
-		return rCURSOR_NORMAL;
-	}
-	if (!strcmp(name, "CURSOR_IKA"))
-	{
-		*size = sizeof(rCURSOR_IKA);
-		return rCURSOR_IKA;
-	}
-	return NULL;
-}
+	{"CURSOR", "CURSOR_NORMAL", rCURSOR_NORMAL, sizeof(rCURSOR_NORMAL)},
+	{"CURSOR", "CURSOR_IKA", rCURSOR_IKA, sizeof(rCURSOR_IKA)},
 
-SDL_RWops* FindResource(const char *name)
+};
+
+const unsigned char* FindResource(const char *name, const char *type, size_t *size)
 {
-	size_t resSize;
-	const unsigned char* resource = GetResource(name, &resSize);
-	
-	if (!resource)
-		return NULL;
-	
-	SDL_RWops *fp = SDL_RWFromConstMem(resource, (int)resSize);
-	
-	if (!fp)
+	for (unsigned int i = 0; i < sizeof(resources) / sizeof(resources[0]); ++i)
 	{
-		printf("Couldn't open resource %s\nSDL Error: %s\n", name, SDL_GetError());
-		return NULL;
+		if (!strcmp(name, resources[i].name) && !strcmp(type, resources[i].type))
+		{
+			if (size)
+				*size = resources[i].size;
+
+			return resources[i].data;
+		}
 	}
-	
-	return fp;
+
+	return NULL;
 }
--- a/src/Resource.h
+++ b/src/Resource.h
@@ -1,5 +1,5 @@
 #pragma once
 
-#include <SDL_rwops.h>
+#include <stddef.h>
 
-SDL_RWops* FindResource(const char *name);
+const unsigned char* FindResource(const char *name, const char *type, size_t *size);