copy data & load from target or bundle resource folder

This commit is contained in:
2024-10-08 11:12:01 +11:00
parent a934503ec9
commit 8d6645e55c
3 changed files with 64 additions and 8 deletions

View File

@@ -75,6 +75,8 @@ typedef struct tagAPPSTATE
SDL_Window *win;
SDL_GLContext ctx;
const char *resdir;
bool fullscreen, blend;
CAMERA camera;
@@ -84,6 +86,36 @@ typedef struct tagAPPSTATE
SECTOR sector1;
} APPSTATE;
static char * resourcePath(const APPSTATE *restrict state, const char *restrict name)
{
if (!state || !name)
{
return NULL;
}
size_t resdirLen = strlen(state->resdir), nameLen = strlen(name);
char *path = malloc(resdirLen + nameLen + 1);
if (!path)
{
return NULL;
}
memcpy(path, state->resdir, resdirLen);
memcpy(&path[resdirLen], name, nameLen);
path[resdirLen + nameLen] = '\0';
return path;
}
static FILE * fopenResource(const APPSTATE *restrict state, const char *restrict name, const char* restrict mode)
{
char *path = NULL;
if (!mode || !(path = resourcePath(state, name)))
{
return NULL;
}
FILE *f = fopen(path, mode);
free(path);
return f;
}
static void readstr(FILE *f, char *string)
{
do
@@ -99,7 +131,7 @@ static void SetupWorld(APPSTATE *state)
int numtriangles;
FILE *filein;
char oneline[255];
filein = fopen("Data/World.txt", "r"); // File to load world data from
filein = fopenResource(state, "Data/World.txt", "r"); // File to load world data from
readstr(filein, oneline);
sscanf(oneline, "NUMPOLLIES %d\n", &numtriangles);
@@ -155,13 +187,19 @@ static bool FlipSurface(SDL_Surface *surface)
static bool LoadGLTextures(APPSTATE *state)
{
SDL_Surface *TextureImage = NULL;
// Load & flip the bitmap
if (!(TextureImage = SDL_LoadBMP("Data/Mud.bmp")) || !FlipSurface(TextureImage))
char *path = resourcePath(state, "Data/Mud.bmp");
if (!path)
{
return false;
}
SDL_Surface *TextureImage = SDL_LoadBMP(path);
free(path);
if (!TextureImage || !FlipSurface(TextureImage))
{
SDL_DestroySurface(TextureImage);
return false;
}
glGenTextures(3, &state->texture[0]); // Create three textures
GLint params[3][3] =
@@ -555,6 +593,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
.win = NULL,
.ctx = NULL,
.resdir = SDL_GetBasePath(),
.fullscreen = false,
.blend = false, // Blending off