diff options
author | davidovski <david@davidovski.xyz> | 2023-07-10 17:01:01 +0100 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2023-07-10 17:01:01 +0100 |
commit | 9c8b526991b68439284bee0936b90079943b249b (patch) | |
tree | ede9b93c28ff38e206eecff2b3d5b44bfd51716e /src/tiledfile.c | |
parent | 12a53d83e062fc7ec5838c94d575b94a7310d6cd (diff) |
tidied code for tilefile
Diffstat (limited to 'src/tiledfile.c')
-rw-r--r-- | src/tiledfile.c | 46 |
1 files changed, 17 insertions, 29 deletions
diff --git a/src/tiledfile.c b/src/tiledfile.c index 0b37bae..ada29e5 100644 --- a/src/tiledfile.c +++ b/src/tiledfile.c @@ -5,19 +5,7 @@ const int i = 1; #define is_bigendian() ( (*(char*)&i) == 0 ) -void processTilemapTexture(Texture2D *loc, int * tilelayout, int width, int height) { - Color *pixels = (Color*) malloc(width * height * sizeof(Color)); - printf("%d x %d\n", width, height); - - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int tile = tilelayout[y*width + x]; - pixels[y*width + x] = (Color){ - (int) tile, 0, 0, 0 - }; - } - } - +void textureFromPixels(Texture2D *loc, Color *pixels, int width, int height) { Image checkedIm = { .data = pixels, .width = width, @@ -28,25 +16,23 @@ void processTilemapTexture(Texture2D *loc, int * tilelayout, int width, int heig *loc = LoadTextureFromImage(checkedIm); UnloadImage(checkedIm); - } -void readrgba(Texture2D *loc, int width, int height, FILE *file) { - char *pixels = malloc(width*height*4); - printf("%d\n", width*height*4); - fread(pixels, (size_t) width*height*4, (size_t) 1, file); +void processTilemapTexture(Texture2D *loc, int * tilelayout, int width, int height) { + Color *pixels = (Color*) malloc(width * height * sizeof(Color)); - Image checkedIm = { - .data = pixels, - .width = width, - .height = height, - .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, - .mipmaps = 1 - }; + for (int i = 0; i < width*height; i++) { + pixels[i] = (Color){ tilelayout[i], 0, 0, 0 }; + } - *loc = LoadTextureFromImage(checkedIm); - UnloadImage(checkedIm); + textureFromPixels(loc, pixels, width, height); +} +//! read rgba image from file +void readrgba(Texture2D *loc, int width, int height, FILE *file) { + Color *pixels = malloc(width*height*4); + fread(pixels, (size_t) width*height*4, (size_t) 1, file); + textureFromPixels(loc, pixels, width, height); } @@ -69,7 +55,7 @@ int readb(char * out, size_t noBytes, FILE * file) { return 0; } - +//! load tilemap data from file void loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * atlasSize) { int width, height, tilebytes, tilesize, atlasWidth, atlasHeight; int * tilelayout; @@ -86,9 +72,11 @@ 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 tiles\n", tilebytes, width*height); + 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); + + // create a texture from the tilelayout data processTilemapTexture(tilemap, tilelayout, width, height); // read the pixel size of each tile |