чување fdc9cd37cfd4f2132b151a9612b2ba03bc6fbba0
родитељ 6fda5d1a4a1a4e05dbe70fc4b3e612bd4081ffbc
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Tue, 5 Sep 2023 16:32:40 +0200
Smarter texture and datafile loading (DATADIR -> . -> fail)
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | lav-sdl.c | | | 90 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- |
| M | local.h.in | | | 7 | ++++--- |
измењених датотека: 2, додавања: 68(+), брисања: 29(-)
diff --git a/lav-sdl.c b/lav-sdl.c
@@ -127,8 +127,8 @@ void labyrinth_coord_to_screen_coord(const int x, const int y,
const float startx, const float starty, float* to_x, float* to_y);
void labyrinth_coord_to_screen_coordF(const float x, const float y,
const float startx, const float starty, float* to_x, float* to_y);
-void load_textures(void);
-void load_labyrinth(const char* pathname);
+int load_textures(void);
+int load_labyrinth(const char* pathname);
int location_pop(SDL_Point* location);
void location_push(const SDL_Point* location);
void next_step(void);
@@ -137,6 +137,7 @@ void print_debug(const char* fmt, ...);
void print_error(const char* fmt, ...);
void reset(void);
void step_animation(void);
+SDL_Surface* try_load(const char* filename);
void
assign_pointIF(const SDL_Point* from, SDL_FPoint* to)
@@ -571,19 +572,14 @@ labyrinth_coord_to_screen_coordF(const float x, const float y,
*to_y = starty + y * CELL_SIZE;
}
-void
+int
load_textures(void)
{
SDL_Surface* surface = NULL;
- /* TODO: Tre DATADIR, then ., then fail */
- surface = IMG_Load(SPRITES_PNG);
+ surface = try_load(SPRITES_PNG);
if (!surface)
- {
- print_error("IMG_Load failed: %s", IMG_GetError());
- exit(1);
- }
-
+ return -1;
sprites = SDL_CreateTextureFromSurface(renderer, surface);
if (!sprites)
{
@@ -592,15 +588,10 @@ load_textures(void)
exit(1);
}
SDL_FreeSurface(surface);
- surface = NULL;
- surface = IMG_Load(TER16_PNG);
+ surface = try_load(TER16_PNG);
if (!surface)
- {
- print_error("IMG_Load failed: %s", IMG_GetError());
- exit(1);
- }
-
+ return -1;
ter16_texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!ter16_texture)
{
@@ -610,9 +601,11 @@ load_textures(void)
}
SDL_FreeSurface(surface);
+
+ return 0;
}
-void
+int
load_labyrinth(const char* pathname)
{
FILE* input = NULL;
@@ -621,11 +614,7 @@ load_labyrinth(const char* pathname)
input = fopen(pathname, "rt");
if (!input)
- {
- print_error("load_labyrinth: fopen failed: %s (%s)",
- strerror(errno), pathname);
- exit(1);
- }
+ return -1;
labyrinth.cells = (char**)calloc(LABYRINTH_MAXHEIGHT, sizeof(char*));
if (!labyrinth.cells)
@@ -696,6 +685,8 @@ load_labyrinth(const char* pathname)
labyrinth.cells[end.y][end.x] = CELL_END;
fclose(input);
+
+ return 0;
}
int
@@ -868,11 +859,38 @@ step_animation(void)
}
}
+SDL_Surface*
+try_load(const char* filename)
+{
+ char tex_pathname[4096];
+ SDL_Surface* surface = NULL;
+
+ sprintf(tex_pathname, "%s/%s", DATADIR, filename);
+ surface = IMG_Load(tex_pathname);
+ if (!surface)
+ {
+ print_debug("try_load: IMG_Load failed (%s): %s", tex_pathname,
+ IMG_GetError());
+ sprintf(tex_pathname, "./%s", filename);
+ print_debug("try_load: trying %s", tex_pathname);
+ surface = IMG_Load(tex_pathname);
+ if (!surface)
+ {
+ print_error("try_load: IMG_Load failed (%s): %s",
+ tex_pathname, IMG_GetError());
+ exit(1);
+ }
+ }
+
+ return surface;
+}
+
int
main(int argc, char** argv)
{
SDL_Event event;
int px, py;
+ char datafile_pathname[4096];
atexit(&cleanup);
@@ -898,11 +916,31 @@ main(int argc, char** argv)
SDL_SetHintWithPriority(SDL_HINT_RENDER_VSYNC, "1", SDL_HINT_OVERRIDE);
SDL_RenderSetLogicalSize(renderer, DEFAULT_WIDTH, DEFAULT_HEIGHT);
- load_textures();
+ if (load_textures() < 0)
+ exit(1);
+
if (argc > 1)
- load_labyrinth(*(argv + 1));
+ {
+ if (load_labyrinth(*(argv + 1)) < 0)
+ exit(1);
+ }
else
- load_labyrinth(DEFAULT_DATAFILE);
+ {
+ sprintf(datafile_pathname, "%s/%s", DATADIR, DEFAULT_DATAFILE);
+ if (load_labyrinth(datafile_pathname) < 0)
+ {
+ print_debug("fopen failed: %s (%s)", strerror(errno),
+ datafile_pathname);
+ sprintf(datafile_pathname, "./%s", DEFAULT_DATAFILE);
+ print_debug("trying %s", datafile_pathname);
+ if (load_labyrinth(datafile_pathname) < 0)
+ {
+ print_error("fopen failed: %s (%s)",
+ strerror(errno), datafile_pathname);
+ exit(1);
+ }
+ }
+ }
location_stack
= (SDL_Point*)calloc(LOCATION_STACK_MAX, sizeof(SDL_Point));
animation.active = 0;
diff --git a/local.h.in b/local.h.in
@@ -2,6 +2,7 @@
* any later version. Copyright (C) 2023 Страхиња Радић.
* See the file LICENSE for exact copyright and license details. */
-#define DEFAULT_DATAFILE "%DATADIR%/lavirint.dat"
-#define TER16_PNG "%DATADIR%/ter16.png"
-#define SPRITES_PNG "%DATADIR%/sprites.png"
+#define DATADIR "%DATADIR%"
+#define DEFAULT_DATAFILE "lavirint.dat"
+#define TER16_PNG "ter16.png"
+#define SPRITES_PNG "sprites.png"