summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-07-10 17:29:00 +0100
committerdavidovski <david@davidovski.xyz>2023-07-10 17:29:00 +0100
commita65880a11878a9b8e08174b7c897894b2a81a226 (patch)
treec6f8b4ca477a90bdac425f2b811deebd1ba8e3ce /src
parent9c8b526991b68439284bee0936b90079943b249b (diff)
exit when map load fails
Diffstat (limited to 'src')
-rw-r--r--src/tiled.c6
-rw-r--r--src/tiledfile.c16
-rw-r--r--src/tiledfile.h2
3 files changed, 15 insertions, 9 deletions
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 <raylib.h>
-void loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * atlasSize);
+int loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * atlasSize);