diff options
author | davidovski <david@davidovski.xyz> | 2023-07-11 01:36:53 +0100 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2023-07-11 01:36:53 +0100 |
commit | 86da5bd3b0b5e0a8d9e68534297ca0446d2f735d (patch) | |
tree | 09c0a666674cfdd3703ea01beff6064d09349acd | |
parent | da40b1083161b1a53e2764ffac3a3a1f8568e24c (diff) |
refactor tiled
-rw-r--r-- | src/tiled.c | 99 | ||||
-rw-r--r-- | src/tiled.h | 12 | ||||
-rw-r--r-- | src/tiledfile.c | 54 | ||||
-rw-r--r-- | src/tiledfile.h | 15 | ||||
-rwxr-xr-x | tools/createmap.py | 2 |
5 files changed, 107 insertions, 75 deletions
diff --git a/src/tiled.c b/src/tiled.c index 1dd1d43..0cdd8f1 100644 --- a/src/tiled.c +++ b/src/tiled.c @@ -1,7 +1,6 @@ #include <raylib.h> #include <stdio.h> -#include "tiledfile.h" #include "tiled.h" #define SCREEN_W 1280 @@ -21,80 +20,94 @@ void drawOverlay() { DrawText(TextFormat("FPS: %d", GetFPS()), 12, 12, 24, DARKGRAY); } -Shader initTiledShader(TiledUniforms *uniforms) { - Shader shader = LoadShader(0, "tiled.glsl"); - uniforms->offsetLoc = GetShaderLocation(shader, "offset"); - uniforms->zoomLoc = GetShaderLocation(shader, "zoom"); +void initTiledShader(Tiled *tiled) { + tiled->shader = LoadShader(0, "tiled.glsl"); - uniforms->atlasSizeLoc = GetShaderLocation(shader, "atlasSize"); - uniforms->mapSizeLoc = GetShaderLocation(shader, "mapSize"); + tiled->offsetLoc = GetShaderLocation(tiled->shader, "offset"); + tiled->zoomLoc = GetShaderLocation(tiled->shader, "zoom"); - uniforms->atlasTextureLoc = GetShaderLocation(shader, "atlasTexture"); - uniforms->tilemapTextureLoc = GetShaderLocation(shader, "tilemapTexture"); + tiled->atlasSizeLoc = GetShaderLocation(tiled->shader, "atlasSize"); + tiled->mapSizeLoc = GetShaderLocation(tiled->shader, "mapSize"); - return shader; + tiled->atlasTextureLoc = GetShaderLocation(tiled->shader, "atlasTexture"); + tiled->tilemapTextureLoc = GetShaderLocation(tiled->shader, "tilemapTexture"); } -void setTiledShaderUniforms(Shader shader, TiledUniforms uniforms) { - SetShaderValue(shader, uniforms.offsetLoc, &uniforms.offset, SHADER_UNIFORM_VEC2); - SetShaderValue(shader, uniforms.zoomLoc, &uniforms.zoom, SHADER_UNIFORM_FLOAT); +void initTiled(Tiled *tiled, TiledMap tiledMap) { + tiled->offset = (Vector2) {0, 0}; + tiled->zoom = 64; + tiled->mapSize[0] = tiled->tilemapTexture.width; + tiled->mapSize[1] = tiled->tilemapTexture.height; + tiled->targetTexture = LoadRenderTexture(1, 1); - SetShaderValue(shader, uniforms.atlasSizeLoc, &uniforms.atlasSize, SHADER_UNIFORM_IVEC2); - SetShaderValue(shader, uniforms.mapSizeLoc, &uniforms.tilemapTexture.width, SHADER_UNIFORM_IVEC2); + tiled->atlasSize[0] = tiledMap.atlasSize[0]; + tiled->atlasSize[1] = tiledMap.atlasSize[1]; + textureFromPixels(&tiled->atlasTexture, + tiledMap.atlasData, + tiledMap.atlasSize[0] * tiledMap.tilesize, + tiledMap.atlasSize[1] * tiledMap.tilesize); - SetShaderValueTexture(shader, uniforms.atlasTextureLoc, uniforms.atlasTexture); - SetShaderValueTexture(shader, uniforms.tilemapTextureLoc, uniforms.tilemapTexture); + renderTilemapTexture(&tiled->tilemapTexture, tiledMap); + + initTiledShader(tiled); } -void unloadTiledShader(Shader shader, TiledUniforms uniforms) { - UnloadShader(shader); - UnloadTexture(uniforms.atlasTexture); - UnloadTexture(uniforms.tilemapTexture); +void setTiledShaderUniforms(Tiled tiled) { + SetShaderValue(tiled.shader, tiled.offsetLoc, &tiled.offset, SHADER_UNIFORM_VEC2); + SetShaderValue(tiled.shader, tiled.zoomLoc, &tiled.zoom, SHADER_UNIFORM_FLOAT); + + SetShaderValue(tiled.shader, tiled.atlasSizeLoc, &tiled.atlasSize, SHADER_UNIFORM_IVEC2); + SetShaderValue(tiled.shader, tiled.mapSizeLoc, &tiled.tilemapTexture.width, SHADER_UNIFORM_IVEC2); + + SetShaderValueTexture(tiled.shader, tiled.atlasTextureLoc, tiled.atlasTexture); + SetShaderValueTexture(tiled.shader, tiled.tilemapTextureLoc, tiled.tilemapTexture); +} +void unloadTiled(Tiled *tiled) { + UnloadShader(tiled->shader); + UnloadTexture(tiled->atlasTexture); + UnloadTexture(tiled->tilemapTexture); + UnloadRenderTexture(tiled->targetTexture); +} + +void drawTiled(Tiled *tiled) { + updateCamera(&tiled->offset, &tiled->zoom); + + BeginShaderMode(tiled->shader); + setTiledShaderUniforms(*tiled); + + DrawTextureRec(tiled->targetTexture.texture, + (Rectangle){0, 0, GetScreenWidth(), GetScreenHeight()}, + (Vector2){0, 0}, + WHITE); + EndShaderMode(); } int main() { SetConfigFlags(FLAG_WINDOW_RESIZABLE); InitWindow(SCREEN_W, SCREEN_H, "tiled"); - TiledUniforms uniforms; - if (loadTileMap("map.tiles", &uniforms.tilemapTexture, &uniforms.atlasTexture, uniforms.atlasSize)) - return 1; - - uniforms.offset = (Vector2) {0, 0}; - uniforms.zoom = 64; - uniforms.mapSize[0] = uniforms.tilemapTexture.width; - uniforms.mapSize[1] = uniforms.tilemapTexture.height; + Tiled tiled; + TiledMap tiledMap = loadTiledMap("map.tiles"); + initTiled(&tiled, tiledMap); - RenderTexture2D target = LoadRenderTexture(1, 1); - Shader shader = initTiledShader(&uniforms); while (!WindowShouldClose()) { - updateCamera(&uniforms.offset, &uniforms.zoom); BeginDrawing(); ClearBackground(LIGHTGRAY); - BeginShaderMode(shader); - setTiledShaderUniforms(shader, uniforms); - - DrawTextureRec(target.texture, - (Rectangle){0, 0, GetScreenWidth(), GetScreenHeight()}, - (Vector2){0, 0}, - WHITE); - EndShaderMode(); - + drawTiled(&tiled); drawOverlay(); EndDrawing(); } - UnloadRenderTexture(target); - unloadTiledShader(shader, uniforms); + unloadTiled(&tiled); CloseWindow(); return 0; diff --git a/src/tiled.h b/src/tiled.h index 5791dc0..f496dc6 100644 --- a/src/tiled.h +++ b/src/tiled.h @@ -1,6 +1,8 @@ #include <raylib.h> -typedef struct TiledUniforms { +#include "tiledfile.h" + +typedef struct Tiled { float zoom; Vector2 offset; @@ -10,6 +12,8 @@ typedef struct TiledUniforms { Texture2D atlasTexture; Texture2D tilemapTexture; + RenderTexture2D targetTexture; + Shader shader; int zoomLoc; int offsetLoc; @@ -20,4 +24,8 @@ typedef struct TiledUniforms { int atlasTextureLoc; int tilemapTextureLoc; -} TiledUniforms; +} Tiled; + +void initTiled(Tiled *tiled, TiledMap tiledMap); +void drawTiled(Tiled *tiled); +void unloadTiled(Tiled *tiled); diff --git a/src/tiledfile.c b/src/tiledfile.c index e215d60..a8de9cd 100644 --- a/src/tiledfile.c +++ b/src/tiledfile.c @@ -2,10 +2,12 @@ #include <stdlib.h> #include <stdio.h> +#include "tiledfile.h" + const int i = 1; #define is_bigendian() ( (*(char*)&i) == 0 ) -void textureFromPixels(Texture2D *loc, Color *pixels, int width, int height) { +void textureFromPixels(Texture2D *texOut, Color *pixels, int width, int height) { Image checkedIm = { .data = pixels, .width = width, @@ -14,18 +16,18 @@ void textureFromPixels(Texture2D *loc, Color *pixels, int width, int height) { .mipmaps = 1 }; - *loc = LoadTextureFromImage(checkedIm); + *texOut = LoadTextureFromImage(checkedIm); UnloadImage(checkedIm); } -void processTilemapTexture(Texture2D *loc, char * tilelayout, int width, int height) { - Color *pixels = (Color*) malloc(width * height * sizeof(Color)); +void renderTilemapTexture(Texture2D *texOut, TiledMap tiledMap) { + Color *pixels = (Color*) malloc(tiledMap.width * tiledMap.height * sizeof(Color)); - for (int i = 0; i < width*height; i++) { - pixels[i] = (Color){ tilelayout[i], 0, 0, 0 }; + for (int i = 0; i < tiledMap.width*tiledMap.height; i++) { + pixels[i] = (Color){ tiledMap.tilelayout[i], 0, 0, 0 }; } - textureFromPixels(loc, pixels, width, height); + textureFromPixels(texOut, pixels, tiledMap.width, tiledMap.height); } //! read rgba image from file @@ -56,42 +58,38 @@ int readb(char * out, size_t noBytes, FILE * file) { } //! load tilemap data from file -int loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * atlasSize) { - int width, height, tilebytes, tilesize, atlasWidth, atlasHeight; - char * tilelayout; - +TiledMap loadTiledMap(char * filename) { + TiledMap tiledmap; FILE * file; if (!(file = fopen(filename, "rb"))) { fprintf(stderr, "Failed to load %s\n", filename); - return 1; + return tiledmap; } // skip header fseek(file, 10, SEEK_CUR); // 4 bytes for int width - readb((char *)&width, 4, file); + readb((char *)&tiledmap.width, 4, file); // 4 bytes for int height - readb((char *)&height, 4, file); - // 4 bytes saying how big each tile is - readb((char *)&tilebytes, 4, file); + readb((char *)&tiledmap.height, 4, file); - tilelayout = malloc(width*height*tilebytes); - fread(tilelayout, tilebytes, width*height, file); - - // create a texture from the tilelayout data - processTilemapTexture(tilemap, tilelayout, width, height); + size_t layoutSize = tiledmap.width*tiledmap.height; + tiledmap.tilelayout = malloc(layoutSize); + fread(tiledmap.tilelayout, layoutSize, 1, file); // read the pixel size of each tile - readb((char *)&tilesize, 4, file); + readb((char *)&tiledmap.tilesize, 4, file); + // read the atlas size - readb((char *)&atlasWidth, 4, file); - readb((char *)&atlasHeight, 4, file); - atlasSize[0] = atlasWidth; - atlasSize[1] = atlasHeight; + readb((char *)&tiledmap.atlasSize[0], 4, file); + readb((char *)&tiledmap.atlasSize[1], 4, file); // read the atlas itself - readrgba(atlas, atlasWidth * tilesize, atlasHeight * tilesize, file); + size_t atlasSizeBytes = tiledmap.atlasSize[0]*tiledmap.tilesize*tiledmap.atlasSize[1]*tiledmap.tilesize*4; + tiledmap.atlasData = malloc(atlasSizeBytes); + fread(tiledmap.atlasData, atlasSizeBytes, (size_t) 1, file); + fclose(file); - return 0; + return tiledmap; } diff --git a/src/tiledfile.h b/src/tiledfile.h index 8c47234..c9bd573 100644 --- a/src/tiledfile.h +++ b/src/tiledfile.h @@ -1,3 +1,16 @@ #include <raylib.h> -int loadTileMap(char * filename, Texture2D * tilemap, Texture2D * atlas, int * atlasSize); +typedef struct TiledMap { + int width; + int height; + char * tilelayout; + int tilesize; + int atlasSize[2]; + Color * atlasData; +} TiledMap; + +void textureFromPixels(Texture2D *texOut, Color *pixels, int width, int height); + +void renderTilemapTexture(Texture2D *texOut, TiledMap tiledMap); + +TiledMap loadTiledMap(char * filename); diff --git a/tools/createmap.py b/tools/createmap.py index 1d6a852..b5c5b2e 100755 --- a/tools/createmap.py +++ b/tools/createmap.py @@ -35,7 +35,7 @@ with open(outfile, "wb") as file: file.write(bytes(HEADER, "ascii")) file.write(WIDTH.to_bytes(4, 'big')); file.write(HEIGHT.to_bytes(4, 'big')); - file.write(tilebytes.to_bytes(4, 'big')); + #file.write(tilebytes.to_bytes(4, 'big')); for y in range(HEIGHT): |