From ce892407370be42479bb05e9a2faa60a297c9886 Mon Sep 17 00:00:00 2001 From: davidovski Date: Tue, 11 Jul 2023 21:37:38 +0100 Subject: make render changes on texture rather than image --- src/editor.c | 3 +-- src/tiled.c | 38 +++++++++++++++++++++++++++++++------- src/tiled.h | 4 +++- src/tiledfile.c | 11 ----------- 4 files changed, 35 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/editor.c b/src/editor.c index 52e653f..ac38891 100644 --- a/src/editor.c +++ b/src/editor.c @@ -27,12 +27,11 @@ void drawOverlay(Tiled tiled) { void modifyTile(Tiled *tiled, int i) { if (selectedTile[0] >= 0 && selectedTile[0] < tiled->tiledMap.width && selectedTile[1] >= 0 && selectedTile[1] < tiled->tiledMap.height) { - printf("%d,%d", selectedTile[0], selectedTile[1]); int tile = getTiledMapTile(tiled->tiledMap, selectedTile); int tileCount = tiled->tiledMap.atlasSize[0] * tiled->tiledMap.atlasSize[1] + 1; tile = (tile + i) % (tileCount); setTiledMapTile(tiled->tiledMap, selectedTile, tile); - renderTilemapTexture(&tiled->tilemapTexture, tiled->tiledMap); + redrawTiledMap(*tiled); } } diff --git a/src/tiled.c b/src/tiled.c index 65b9d1d..43afa7b 100644 --- a/src/tiled.c +++ b/src/tiled.c @@ -45,14 +45,34 @@ Vector2 translateTiledScreenPosition(Tiled tiled, Vector2 tiledPos) { }; } +void redrawTiledMap(Tiled tiled) { + BeginTextureMode(tiled.tilemapTexture); + for (int y = 0; y < tiled.tiledMap.height; y++) { + for (int x = 0; x < tiled.tiledMap.width; x++) { + int i = (tiled.tiledMap.height - y - 1)*tiled.tiledMap.width + x; + Color c = (Color){ + tiled.tiledMap.tilelayout[i], + 0, 0, 255 + }; + DrawPixel(x, y, c); + } + } + EndTextureMode(); + +} + + Tiled initTiled(TiledMap tiledMap) { Tiled tiled; tiled.tiledMap = tiledMap; + tiled.offset = (Vector2) {0, 0}; tiled.zoom = 64; - tiled.mapSize[0] = tiled.tiledMap.width; - tiled.mapSize[1] = tiled.tiledMap.height; + + tiled.mapSize[0] = tiledMap.width; + tiled.mapSize[1] = tiledMap.height; tiled.targetTexture = LoadRenderTexture(1, 1); + tiled.tilemapTexture = LoadRenderTexture(tiledMap.width, tiledMap.width); tiled.atlasSize[0] = tiledMap.atlasSize[0]; tiled.atlasSize[1] = tiledMap.atlasSize[1]; @@ -63,7 +83,7 @@ Tiled initTiled(TiledMap tiledMap) { tiledMap.atlasSize[1] * tiledMap.tileSize); - renderTilemapTexture(&tiled.tilemapTexture, tiled.tiledMap); + redrawTiledMap(tiled); initTiledShader(&tiled); return tiled; } @@ -73,20 +93,20 @@ void setTiledShaderUniforms(Tiled tiled) { 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); + SetShaderValue(tiled.shader, tiled.mapSizeLoc, &tiled.mapSize, SHADER_UNIFORM_IVEC2); SetShaderValueTexture(tiled.shader, tiled.atlasTextureLoc, tiled.atlasTexture); - SetShaderValueTexture(tiled.shader, tiled.tilemapTextureLoc, tiled.tilemapTexture); + SetShaderValueTexture(tiled.shader, tiled.tilemapTextureLoc, tiled.tilemapTexture.texture); } void unloadTiled(Tiled *tiled) { UnloadShader(tiled->shader); UnloadTexture(tiled->atlasTexture); - UnloadTexture(tiled->tilemapTexture); + UnloadRenderTexture(tiled->tilemapTexture); UnloadRenderTexture(tiled->targetTexture); } - void drawTiled(Tiled *tiled) { + //renderTiledMapTexture(tiled->tiledMap, tiled->tilemapTexture); BeginShaderMode(tiled->shader); setTiledShaderUniforms(*tiled); @@ -95,6 +115,10 @@ void drawTiled(Tiled *tiled) { (Vector2){0, 0}, WHITE); EndShaderMode(); + //DrawTextureRec(tiled->tilemapTexture.texture, + //(Rectangle){0, 0, GetScreenWidth(), GetScreenHeight()}, + //(Vector2){0, 0}, + //WHITE); } int launchTiledView() { diff --git a/src/tiled.h b/src/tiled.h index e1f1c79..735c3a9 100644 --- a/src/tiled.h +++ b/src/tiled.h @@ -14,8 +14,8 @@ typedef struct Tiled { int mapSize[2]; Texture2D atlasTexture; - Texture2D tilemapTexture; + RenderTexture2D tilemapTexture; RenderTexture2D targetTexture; Shader shader; @@ -36,3 +36,5 @@ Vector2 translateTiledScreenPosition(Tiled tiled, Vector2 tiledPos); Tiled initTiled(TiledMap tiledMap); void drawTiled(Tiled *tiled); void unloadTiled(Tiled *tiled); +void redrawTiledMap(Tiled tiled); + diff --git a/src/tiledfile.c b/src/tiledfile.c index bf0a828..64a1e0d 100644 --- a/src/tiledfile.c +++ b/src/tiledfile.c @@ -20,17 +20,6 @@ void textureFromPixels(Texture2D *texOut, Color *pixels, int width, int height) UnloadImage(checkedIm); } -void renderTilemapTexture(Texture2D *texOut, TiledMap tiledMap) { - Color *pixels = (Color*) malloc(tiledMap.width * tiledMap.height * sizeof(Color)); - - for (int i = 0; i < tiledMap.width*tiledMap.height; i++) { - pixels[i] = (Color){ tiledMap.tilelayout[i], 0, 0, 0 }; - } - - textureFromPixels(texOut, pixels, tiledMap.width, tiledMap.height); -} - - //! read rgba image from file void readrgba(Texture2D *loc, int width, int height, FILE *file) { Color *pixels = malloc(width*height*4); -- cgit v1.2.1