Remove Resource.cpp's dependence on SDL2

This also makes its API a lot more similar to Windows'
This commit is contained in:
Clownacy 2019-05-05 01:57:23 +01:00
parent 58a3974e1a
commit f7c1ca86c3
5 changed files with 135 additions and 397 deletions

View file

@ -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) 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); printf("Loading surface from resource %s for surface id %d\n", res, surf_no);
if (LoadBitmap(fp, surf_no, create_surface)) if (LoadBitmap(fp, surf_no, create_surface))
return TRUE; return TRUE;

View file

@ -212,10 +212,13 @@ int main(int argc, char *argv[])
RECT unused_rect = {0, 0, 320, 240}; RECT unused_rect = {0, 0, 320, 240};
//Load cursor //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_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 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 #ifndef WINDOWS
//Load icon //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 *iconSurf = SDL_LoadBMP_RW(fp, 1);
SDL_Surface *iconConverted = SDL_ConvertSurfaceFormat(iconSurf, SDL_PIXELFORMAT_RGB888, 0); SDL_Surface *iconConverted = SDL_ConvertSurfaceFormat(iconSurf, SDL_PIXELFORMAT_RGB888, 0);
SDL_FreeSurface(iconSurf); SDL_FreeSurface(iconSurf);

View file

@ -261,19 +261,16 @@ void ReleaseOrganyaObject(int8_t track)
//Handling WAVE100 //Handling WAVE100
int8_t wave_data[100][0x100]; int8_t wave_data[100][0x100];
bool InitWaveData100() BOOL InitWaveData100()
{ {
SDL_RWops *fp = FindResource("WAVE100"); const unsigned char *data = FindResource("WAVE100", "WAVE", NULL);
if (fp == NULL)
{
printf("Failed to open WAVE100\n");
return false;
}
fp->read(fp, wave_data, 1, 100 * 0x100); if (data == NULL)
return FALSE;
SDL_RWclose(fp); memcpy(wave_data, data, 100 * 0x100);
return true;
return TRUE;
} }
//Create org wave //Create org wave
@ -409,6 +406,9 @@ void SetPlayPointer(int32_t x)
play_p = 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 //Load organya file
void LoadOrganya(const char *name) void LoadOrganya(const char *name)
{ {
@ -426,19 +426,14 @@ void LoadOrganya(const char *name)
//Open file //Open file
printf("Loading org %s\n", name); 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 //Version Check
uint8_t ver = 0; uint8_t ver = 0;
char pass_check[6]; 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-01", 6))ver = 1;
if (!memcmp(pass_check, "Org-02", 6))ver = 2; if (!memcmp(pass_check, "Org-02", 6))ver = 2;
@ -446,23 +441,23 @@ void LoadOrganya(const char *name)
if (!ver) if (!ver)
{ {
printf("Failed to open.org, invalid version %s", pass_check); printf("Failed to open .org, invalid version %s", pass_check);
return; return;
} }
//Set song information //Set song information
info.wait = SDL_ReadLE16(fp); info.wait = READ_LE16(p);
info.line = SDL_ReadU8(fp); info.line = *p++;
info.dot = SDL_ReadU8(fp); info.dot = *p++;
info.repeat_x = SDL_ReadLE32(fp); info.repeat_x = READ_LE32(p);
info.end_x = SDL_ReadLE32(fp); info.end_x = READ_LE32(p);
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
info.tdata[i].freq = SDL_ReadLE16(fp); info.tdata[i].freq = READ_LE16(p);
info.tdata[i].wave_no = SDL_ReadU8(fp); info.tdata[i].wave_no = *p++;
const int8_t pipi = SDL_ReadU8(fp); const int8_t pipi = *p++;
info.tdata[i].pipi = ver == 1 ? 0 : pipi; 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 //Load notes
@ -495,37 +490,35 @@ void LoadOrganya(const char *name)
//Set note properties //Set note properties
np = info.tdata[j].note_p; //X position np = info.tdata[j].note_p; //X position
for (int i = 0; i < info.tdata[j].note_num; i++) { for (int i = 0; i < info.tdata[j].note_num; i++) {
np->x = SDL_ReadLE32(fp); np->x = READ_LE32(p);
np++; np++;
} }
np = info.tdata[j].note_p; //Y position np = info.tdata[j].note_p; //Y position
for (int i = 0; i < info.tdata[j].note_num; i++) { for (int i = 0; i < info.tdata[j].note_num; i++) {
np->y = SDL_ReadU8(fp); np->y = *p++;
np++; np++;
} }
np = info.tdata[j].note_p; //Length np = info.tdata[j].note_p; //Length
for (int i = 0; i < info.tdata[j].note_num; i++) { for (int i = 0; i < info.tdata[j].note_num; i++) {
np->length = SDL_ReadU8(fp); np->length = *p++;
np++; np++;
} }
np = info.tdata[j].note_p; //Volume np = info.tdata[j].note_p; //Volume
for (int i = 0; i < info.tdata[j].note_num; i++) { for (int i = 0; i < info.tdata[j].note_num; i++) {
np->volume = SDL_ReadU8(fp); np->volume = *p++;
np++; np++;
} }
np = info.tdata[j].note_p; //Pan np = info.tdata[j].note_p; //Pan
for (int i = 0; i < info.tdata[j].note_num; i++) { for (int i = 0; i < info.tdata[j].note_num; i++) {
np->pan = SDL_ReadU8(fp); np->pan = *p++;
np++; np++;
} }
} }
SDL_RWclose(fp);
//Create waves //Create waves
for (int j = 0; j < 8; j++) for (int j = 0; j < 8; j++)
MakeOrganyaWave(j, info.tdata[j].wave_no, info.tdata[j].pipi); MakeOrganyaWave(j, info.tdata[j].wave_no, info.tdata[j].pipi);

View file

@ -1,11 +1,8 @@
#include "Resource.h" #include "Resource.h"
#include <stdint.h> #include <stddef.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <SDL_rwops.h>
#include "WindowsWrapper.h" #include "WindowsWrapper.h"
#include "Resource/ORG/Access.org.h" #include "Resource/ORG/Access.org.h"
@ -79,362 +76,102 @@
#include "Resource/CURSOR/CURSOR_IKA.bmp.h" #include "Resource/CURSOR/CURSOR_IKA.bmp.h"
#include "Resource/CURSOR/CURSOR_NORMAL.bmp.h" #include "Resource/CURSOR/CURSOR_NORMAL.bmp.h"
const unsigned char* GetResource(const char *name, size_t *size) static const struct
{ {
//ORG const char *type;
if (!strcmp(name, "ACCESS")) const char *name;
{ const unsigned char *data;
*size = sizeof(rAccess); size_t size;
return rAccess; } resources[] = {
} {"ORG", "ACCESS", rAccess, sizeof(rAccess)},
if (!strcmp(name, "ANZEN")) {"ORG", "ANZEN", rAnzen, sizeof(rAnzen)},
{ {"ORG", "BALCONY", rBalcony, sizeof(rBalcony)},
*size = sizeof(rAnzen); {"ORG", "BALLOS", rBallos, sizeof(rBallos)},
return rAnzen; {"ORG", "BDOWN", rBreakDown, sizeof(rBreakDown)},
} {"ORG", "CEMETERY", rCemetery, sizeof(rCemetery)},
if (!strcmp(name, "BALCONY")) {"ORG", "CURLY", rCurly, sizeof(rCurly)},
{ {"ORG", "DR", rDr, sizeof(rDr)},
*size = sizeof(rBalcony); {"ORG", "ENDING", rEnding, sizeof(rEnding)},
return rBalcony; {"ORG", "ESCAPE", rEscape, sizeof(rEscape)},
} {"ORG", "FANFALE1", rFanfale1, sizeof(rFanfale1)},
if (!strcmp(name, "BALLOS")) {"ORG", "FANFALE2", rFanfale2, sizeof(rFanfale2)},
{ {"ORG", "FANFALE3", rFanfale3, sizeof(rFanfale3)},
*size = sizeof(rBallos); {"ORG", "FIREEYE", rFireEye, sizeof(rFireEye)},
return rBallos; {"ORG", "GAMEOVER", rGameover, sizeof(rGameover)},
} {"ORG", "GINSUKE", rGinsuke, sizeof(rGinsuke)},
if (!strcmp(name, "BDOWN")) {"ORG", "GRAND", rGrand, sizeof(rGrand)},
{ {"ORG", "GRAVITY", rGravity, sizeof(rGravity)},
*size = sizeof(rBreakDown); {"ORG", "HELL", rHell, sizeof(rHell)},
return rBreakDown; {"ORG", "IRONH", rironH, sizeof(rironH)},
} {"ORG", "JENKA", rJenka, sizeof(rJenka)},
if (!strcmp(name, "CEMETERY")) {"ORG", "JENKA2", rJenka2, sizeof(rJenka2)},
{ {"ORG", "KODOU", rKodou, sizeof(rKodou)},
*size = sizeof(rCemetery); {"ORG", "LASTBT3", rLastBtl3, sizeof(rLastBtl3)},
return rCemetery; {"ORG", "LASTBTL", rLastBtl, sizeof(rLastBtl)},
} {"ORG", "LASTCAVE", rLastCave, sizeof(rLastCave)},
if (!strcmp(name, "CURLY")) {"ORG", "MARINE", rMarine, sizeof(rMarine)},
{ {"ORG", "MAZE", rMaze, sizeof(rMaze)},
*size = sizeof(rCurly); {"ORG", "MDOWN2", rMDown2, sizeof(rMDown2)},
return rCurly; {"ORG", "MURA", rMura, sizeof(rMura)},
} {"ORG", "OSIDE", rOside, sizeof(rOside)},
if (!strcmp(name, "DR")) {"ORG", "PLANT", rPlant, sizeof(rPlant)},
{ {"ORG", "QUIET", rquiet, sizeof(rquiet)},
*size = sizeof(rDr); {"ORG", "REQUIEM", rRequiem, sizeof(rRequiem)},
return rDr; {"ORG", "TOROKO", rToroko, sizeof(rToroko)},
} {"ORG", "VIVI", rVivi, sizeof(rVivi)},
if (!strcmp(name, "ENDING")) {"ORG", "WANPAK2", rWanpak2, sizeof(rWanpak2)},
{ {"ORG", "WANPAKU", rWanpaku, sizeof(rWanpaku)},
*size = sizeof(rEnding); {"ORG", "WEED", rWeed, sizeof(rWeed)},
return rEnding; {"ORG", "WHITE", rWhite, sizeof(rWhite)},
} {"ORG", "XXXX", rXXXX, sizeof(rXXXX)},
if (!strcmp(name, "ESCAPE")) {"ORG", "ZONBIE", rZonbie, sizeof(rZonbie)},
{
*size = sizeof(rEscape); {"WAVE", "WAVE100", rWave, sizeof(rWave)},
return rEscape;
} {"BITMAP", "CREDIT01", rCredit01, sizeof(rCredit01)},
if (!strcmp(name, "FANFALE1")) {"BITMAP", "CREDIT02", rCredit02, sizeof(rCredit02)},
{ {"BITMAP", "CREDIT03", rCredit03, sizeof(rCredit03)},
*size = sizeof(rFanfale1); {"BITMAP", "CREDIT04", rCredit04, sizeof(rCredit04)},
return rFanfale1; {"BITMAP", "CREDIT05", rCredit05, sizeof(rCredit05)},
} {"BITMAP", "CREDIT06", rCredit06, sizeof(rCredit06)},
if (!strcmp(name, "FANFALE2")) {"BITMAP", "CREDIT07", rCredit07, sizeof(rCredit07)},
{ {"BITMAP", "CREDIT08", rCredit08, sizeof(rCredit08)},
*size = sizeof(rFanfale2); {"BITMAP", "CREDIT09", rCredit09, sizeof(rCredit09)},
return rFanfale2; {"BITMAP", "CREDIT10", rCredit10, sizeof(rCredit10)},
} {"BITMAP", "CREDIT11", rCredit11, sizeof(rCredit11)},
if (!strcmp(name, "FANFALE3")) {"BITMAP", "CREDIT12", rCredit12, sizeof(rCredit12)},
{ {"BITMAP", "CREDIT14", rCredit14, sizeof(rCredit14)},
*size = sizeof(rFanfale3); {"BITMAP", "CREDIT15", rCredit15, sizeof(rCredit15)},
return rFanfale3; {"BITMAP", "CREDIT16", rCredit16, sizeof(rCredit16)},
} {"BITMAP", "CREDIT17", rCredit17, sizeof(rCredit17)},
if (!strcmp(name, "FIREEYE")) {"BITMAP", "CREDIT18", rCredit18, sizeof(rCredit18)},
{
*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 #ifdef JAPANESE
*size = sizeof(rpixel_jp); {"BITMAP", "PIXEL", rpixel_jp, sizeof(rpixel_jp)},
return rpixel_jp;
#else #else
*size = sizeof(rpixel); {"BITMAP", "PIXEL", rpixel, sizeof(rpixel)},
return rpixel;
#endif
}
#ifndef WINDOWS
//ICON
if (!strcmp(name, "ICON_MINI"))
{
*size = sizeof(rICON_MINI);
return rICON_MINI;
}
#endif #endif
//CURSOR #ifndef WINDOWS
if (!strcmp(name, "CURSOR_NORMAL")) {"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); if (!strcmp(name, resources[i].name) && !strcmp(type, resources[i].type))
return rCURSOR_NORMAL; {
} if (size)
if (!strcmp(name, "CURSOR_IKA")) *size = resources[i].size;
{
*size = sizeof(rCURSOR_IKA); return resources[i].data;
return rCURSOR_IKA; }
} }
return NULL; 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;
}

View file

@ -1,5 +1,5 @@
#pragma once #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);