From f7c1ca86c3ca6ff0ac81e94e03cfa046b321fe4d Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 May 2019 01:57:23 +0100 Subject: [PATCH] Remove Resource.cpp's dependence on SDL2 This also makes its API a lot more similar to Windows' --- src/Draw.cpp | 7 +- src/Main.cpp | 13 +- src/Organya.cpp | 63 +++---- src/Resource.cpp | 445 ++++++++++------------------------------------- src/Resource.h | 4 +- 5 files changed, 135 insertions(+), 397 deletions(-) diff --git a/src/Draw.cpp b/src/Draw.cpp index bc491684..1d5a7259 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -354,10 +354,13 @@ static BOOL LoadBitmap_File(const char *name, Surface_Ids surf_no, bool create_s 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; diff --git a/src/Main.cpp b/src/Main.cpp index a74aa7c8..c3d2c1ee 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -212,10 +212,13 @@ int main(int argc, char *argv[]) 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 @@ int main(int argc, char *argv[]) #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); diff --git a/src/Organya.cpp b/src/Organya.cpp index c54f693a..28d4a76b 100644 --- a/src/Organya.cpp +++ b/src/Organya.cpp @@ -261,19 +261,16 @@ void ReleaseOrganyaObject(int8_t track) //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 @@ void SetPlayPointer(int32_t x) 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 @@ void LoadOrganya(const char *name) //Open file printf("Loading org %s\n", name); - SDL_RWops *fp = FindResource(name); - - if (!fp) - { - printf("Failed to open %s\n", name); - return; - } + const unsigned char *p = FindResource(name, "ORG", NULL); //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 @@ void LoadOrganya(const char *name) 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,37 +490,35 @@ void LoadOrganya(const char *name) //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++) MakeOrganyaWave(j, info.tdata[j].wave_no, info.tdata[j].pipi); diff --git a/src/Resource.cpp b/src/Resource.cpp index 7306b03d..88837f5b 100644 --- a/src/Resource.cpp +++ b/src/Resource.cpp @@ -1,11 +1,8 @@ #include "Resource.h" -#include -#include +#include #include -#include - #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")) - { + 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 - *size = sizeof(rpixel_jp); - return rpixel_jp; + {"BITMAP", "PIXEL", rpixel_jp, sizeof(rpixel_jp)}, #else - *size = sizeof(rpixel); - return rpixel; -#endif - } - -#ifndef WINDOWS - //ICON - if (!strcmp(name, "ICON_MINI")) - { - *size = sizeof(rICON_MINI); - return rICON_MINI; - } + {"BITMAP", "PIXEL", rpixel, sizeof(rpixel)}, #endif - //CURSOR - if (!strcmp(name, "CURSOR_NORMAL")) +#ifndef WINDOWS + {"ICON", "ICON_MINI", rICON_MINI, sizeof(rICON_MINI)}, +#endif + + {"CURSOR", "CURSOR_NORMAL", rCURSOR_NORMAL, sizeof(rCURSOR_NORMAL)}, + {"CURSOR", "CURSOR_IKA", rCURSOR_IKA, sizeof(rCURSOR_IKA)}, + +}; + +const unsigned char* FindResource(const char *name, const char *type, size_t *size) +{ + for (unsigned int i = 0; i < sizeof(resources) / sizeof(resources[0]); ++i) { - *size = sizeof(rCURSOR_NORMAL); - return rCURSOR_NORMAL; - } - if (!strcmp(name, "CURSOR_IKA")) - { - *size = sizeof(rCURSOR_IKA); - return rCURSOR_IKA; + if (!strcmp(name, resources[i].name) && !strcmp(type, resources[i].type)) + { + if (size) + *size = resources[i].size; + + return resources[i].data; + } } + return NULL; } - -SDL_RWops* FindResource(const char *name) -{ - size_t resSize; - const unsigned char* resource = GetResource(name, &resSize); - - if (!resource) - return NULL; - - SDL_RWops *fp = SDL_RWFromConstMem(resource, (int)resSize); - - if (!fp) - { - printf("Couldn't open resource %s\nSDL Error: %s\n", name, SDL_GetError()); - return NULL; - } - - return fp; -} diff --git a/src/Resource.h b/src/Resource.h index 89d552e6..9f477df6 100644 --- a/src/Resource.h +++ b/src/Resource.h @@ -1,5 +1,5 @@ #pragma once -#include +#include -SDL_RWops* FindResource(const char *name); +const unsigned char* FindResource(const char *name, const char *type, size_t *size);