From a65880a11878a9b8e08174b7c897894b2a81a226 Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 10 Jul 2023 17:29:00 +0100 Subject: exit when map load fails --- makefile | 2 +- src/tiled.c | 6 ++++-- src/tiledfile.c | 16 ++++++++++------ src/tiledfile.h | 2 +- tools/createmap.py | 2 +- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/makefile b/makefile index 43709b3..f5a418e 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ CC=gcc -FLAGS=-lm -lraylib +FLAGS=-lm -lraylib -ggdb .DEFAULT_GOAL := build diff --git a/src/tiled.c b/src/tiled.c index b679e3c..95d3950 100644 --- a/src/tiled.c +++ b/src/tiled.c @@ -6,7 +6,6 @@ #define SCREEN_W 1280 #define SCREEN_H 720 - int main() { InitWindow(SCREEN_W, SCREEN_H, "tiled"); @@ -14,7 +13,10 @@ int main() { int atlasSize[2] = {0, 0}; Texture2D tilemap, atlas; - loadTileMap("map.tiles", &tilemap, &atlas, atlasSize); + + if (loadTileMap("map.tiles", &tilemap, &atlas, atlasSize)) { + return 1; + } RenderTexture2D target = LoadRenderTexture(SCREEN_W, SCREEN_H); diff --git a/src/tiledfile.c b/src/tiledfile.c index ada29e5..e215d60 100644 --- a/src/tiledfile.c +++ b/src/tiledfile.c @@ -18,7 +18,7 @@ void textureFromPixels(Texture2D *loc, Color *pixels, int width, int height) { UnloadImage(checkedIm); } -void processTilemapTexture(Texture2D *loc, int * tilelayout, int width, int height) { +void processTilemapTexture(Texture2D *loc, char * tilelayout, int width, int height) { Color *pixels = (Color*) malloc(width * height * sizeof(Color)); for (int i = 0; i < width*height; i++) { @@ -56,13 +56,17 @@ int readb(char * out, size_t noBytes, FILE * file) { } //! load tilemap data from file -void loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * atlasSize) { +int loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * atlasSize) { int width, height, tilebytes, tilesize, atlasWidth, atlasHeight; - int * tilelayout; + char * tilelayout; FILE * file; - file = fopen(filename, "rb"); + if (!(file = fopen(filename, "rb"))) { + fprintf(stderr, "Failed to load %s\n", filename); + return 1; + } + // skip header fseek(file, 10, SEEK_CUR); // 4 bytes for int width @@ -72,7 +76,6 @@ void loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * // 4 bytes saying how big each tile is readb((char *)&tilebytes, 4, file); - fprintf(stderr, "loading %d bytes per tile for %d (%dx%d) tiles\n", tilebytes, width*height, width, height); tilelayout = malloc(width*height*tilebytes); fread(tilelayout, tilebytes, width*height, file); @@ -89,5 +92,6 @@ void loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * // read the atlas itself readrgba(atlas, atlasWidth * tilesize, atlasHeight * tilesize, file); - fprintf(stderr, "Successfully loaded tilemap from %s\n", filename); + fclose(file); + return 0; } diff --git a/src/tiledfile.h b/src/tiledfile.h index 02342b3..8c47234 100644 --- a/src/tiledfile.h +++ b/src/tiledfile.h @@ -1,3 +1,3 @@ #include -void loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * atlasSize); +int loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * atlasSize); diff --git a/tools/createmap.py b/tools/createmap.py index 6deb0aa..1d6a852 100755 --- a/tools/createmap.py +++ b/tools/createmap.py @@ -9,7 +9,7 @@ ATLASFILE = "atlas.png" TILESIZE = 16 WIDTH = 2**10 -HEIGHT = 2**11 +HEIGHT = 2**10 # create atlas bytes image = Image.open(ATLASFILE).convert("RGBA") -- cgit v1.2.1