diff options
author | davidovski <david@davidovski.xyz> | 2023-07-23 16:46:05 +0200 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2023-07-23 16:46:05 +0200 |
commit | c726acd0467906afee74afb7051bb2e705e26565 (patch) | |
tree | e8b9422ba926f09b85c229d51d5a99a077ea69e8 | |
parent | 0d78ec0e53d9824a029840e74bf0721ed2187e6a (diff) |
add chunk offset to rendering
-rw-r--r-- | src/editor.c | 4 | ||||
-rw-r--r-- | src/tiled.c | 71 | ||||
-rw-r--r-- | src/tiled.h | 13 | ||||
-rw-r--r-- | src/tiledmap.c | 10 | ||||
-rw-r--r-- | src/tiledmap.h | 7 | ||||
-rw-r--r-- | tiled.glsl | 4 |
6 files changed, 62 insertions, 47 deletions
diff --git a/src/editor.c b/src/editor.c index 2c9de3a..73c1e5a 100644 --- a/src/editor.c +++ b/src/editor.c @@ -59,13 +59,11 @@ void update(Tiled *tiled) { updateTiledCamera(tiled); Vector2 mousePos = GetMousePosition(); Vector2 mapPos = translateTiledPosition(*tiled, mousePos); - if (mapPos.x >= 0 && mapPos.x < tiled->mapSize[0] - && mapPos.y >= 0 && mapPos.y < tiled->mapSize[1]) { + lastSelectedTile[0] = selectedTile[0]; lastSelectedTile[1] = selectedTile[1]; selectedTile[0] = mapPos.x; selectedTile[1] = mapPos.y; - } handleInputs(tiled); } diff --git a/src/tiled.c b/src/tiled.c index 0628523..d525027 100644 --- a/src/tiled.c +++ b/src/tiled.c @@ -5,22 +5,28 @@ int alpha =0; -void updateCamera(Vector2 *offset, float *zoom) { - if (IsKeyDown(KEY_UP)) offset->y += 16.0f / *zoom; - if (IsKeyDown(KEY_DOWN)) offset->y -= 16.0f/ *zoom; - if (IsKeyDown(KEY_RIGHT)) offset->x -= 16.0f / *zoom; - if (IsKeyDown(KEY_LEFT)) offset->x += 16.0f / *zoom; - - if (IsKeyDown(KEY_W)) *zoom += *zoom * 0.01f; - if (IsKeyDown(KEY_S)) *zoom -= *zoom * 0.01f; +void updateCamera(Tiled * tiled) { + if (IsKeyDown(KEY_UP)) tiled->offset.y += 4.0f / tiled->zoom; + if (IsKeyDown(KEY_DOWN)) tiled->offset.y -= 4.0f / tiled->zoom; + if (IsKeyDown(KEY_RIGHT)) tiled->offset.x -= 4.0f / tiled->zoom; + if (IsKeyDown(KEY_LEFT)) tiled->offset.x += 4.0f / tiled->zoom; + + if (IsKeyDown(KEY_W)) tiled->zoom += tiled->zoom * 0.01f; + if (IsKeyDown(KEY_S)) tiled->zoom -= tiled->zoom * 0.01f; + + tiled->chunkOffset[0] = tiled->offset.x / tiled->tiledMap.chunkWidth; + tiled->chunkOffset[1] = tiled->offset.y / tiled->tiledMap.chunkHeight; + + tiled->renderOffset.x = tiled->offset.x - tiled->chunkOffset[0]*tiled->tiledMap.chunkWidth; + tiled->renderOffset.y = tiled->offset.y - tiled->chunkOffset[1]*tiled->tiledMap.chunkHeight; + redrawTiledMap(*tiled); } void updateTiledCamera(Tiled *tiled) { - updateCamera(&tiled->offset, &tiled->zoom); + updateCamera(tiled); alpha++; } - void initTiledShader(Tiled *tiled) { tiled->shader = LoadShader(0, "tiled.glsl"); @@ -28,7 +34,7 @@ void initTiledShader(Tiled *tiled) { tiled->zoomLoc = GetShaderLocation(tiled->shader, "zoom"); tiled->atlasSizeLoc = GetShaderLocation(tiled->shader, "atlasSize"); - tiled->mapSizeLoc = GetShaderLocation(tiled->shader, "mapSize"); + tiled->renderAreaLoc = GetShaderLocation(tiled->shader, "renderArea"); tiled->atlasTextureLoc = GetShaderLocation(tiled->shader, "atlasTexture"); tiled->tilemapTextureLoc = GetShaderLocation(tiled->shader, "tilemapTexture"); @@ -43,37 +49,35 @@ Vector2 translateTiledPosition(Tiled tiled, Vector2 screenPos) { Vector2 translateTiledScreenPosition(Tiled tiled, Vector2 tiledPos) { return (Vector2) { - (tiledPos.x + tiled.offset.x) * tiled.zoom, - (tiledPos.y + tiled.offset.y) * tiled.zoom + (tiledPos.x + tiled.renderOffset.x + tiled.chunkOffset[0]*tiled.tiledMap.chunkWidth) * tiled.zoom, + (tiledPos.y + tiled.renderOffset.y + tiled.chunkOffset[1]*tiled.tiledMap.chunkHeight) * tiled.zoom }; } +Tile getOffsetTile(TiledMap * tiledMap, int chunkOffset[2], int x, int y) { + return getChunkedTile(tiledMap, + x - chunkOffset[0]*tiledMap->chunkWidth, + y - chunkOffset[1]*tiledMap->chunkHeight); +} + void redrawTile(Tiled tiled, int x, int y) { BeginTextureMode(tiled.tilemapTexture); - unsigned char v = getChunkedTile(&tiled.tiledMap, x, y); - Color c = (Color){ - v, 0, 0, 255 - }; - DrawPixel(x, tiled.mapSize[1] - y - 1, c); + Tile v = getOffsetTile(&tiled.tiledMap, tiled.chunkOffset, x, y); + DrawPixel(x, tiled.renderArea[1] - y - 1, (Color){ v, 0, 0, 255 }); EndTextureMode(); } void redrawTiledMap(Tiled tiled) { BeginTextureMode(tiled.tilemapTexture); - for (int y = 0; y < tiled.mapSize[1]; y++) { - for (int x = 0; x < tiled.mapSize[0]; x++) { - unsigned char v = getChunkedTile(&tiled.tiledMap, x, y); - Color c = (Color){ - v, 0, 0, 255 - }; - DrawPixel(x, tiled.mapSize[1] - y - 1, c); + for (int y = 0; y < tiled.renderArea[1]; y++) { + for (int x = 0; x < tiled.renderArea[0]; x++) { + Tile v = getOffsetTile(&tiled.tiledMap, tiled.chunkOffset, x, y); + DrawPixel(x, tiled.renderArea[1] - y - 1, (Color){ v, 0, 0, 255 }); } } EndTextureMode(); - } - Tiled initTiled(TiledMap tiledMap) { Tiled tiled; tiled.tiledMap = tiledMap; @@ -81,11 +85,11 @@ Tiled initTiled(TiledMap tiledMap) { tiled.offset = (Vector2) {0, 0}; tiled.zoom = 64; - // TODO mapSize is obsolete, should be visible map size - tiled.mapSize[0] = tiledMap.chunkWidth * 20; - tiled.mapSize[1] = tiledMap.chunkHeight * 20; + // TODO renderArea is obsolete, should be visible map size + tiled.renderArea[0] = tiledMap.chunkWidth * RENDER_DISTANCE; + tiled.renderArea[1] = tiledMap.chunkHeight * RENDER_DISTANCE; tiled.targetTexture = LoadRenderTexture(1, 1); - tiled.tilemapTexture = LoadRenderTexture(tiled.mapSize[0], tiled.mapSize[1]); + tiled.tilemapTexture = LoadRenderTexture(tiled.renderArea[0], tiled.renderArea[1]); tiled.atlasSize[0] = tiledMap.atlasSize[0]; tiled.atlasSize[1] = tiledMap.atlasSize[1]; @@ -102,11 +106,12 @@ Tiled initTiled(TiledMap tiledMap) { } void setTiledShaderUniforms(Tiled tiled) { - SetShaderValue(tiled.shader, tiled.offsetLoc, &tiled.offset, SHADER_UNIFORM_VEC2); + SetShaderValue(tiled.shader, tiled.offsetLoc, &tiled.renderOffset, 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.mapSize, SHADER_UNIFORM_IVEC2); + SetShaderValue(tiled.shader, tiled.renderAreaLoc, &tiled.renderArea, SHADER_UNIFORM_IVEC2); + SetShaderValueTexture(tiled.shader, tiled.atlasTextureLoc, tiled.atlasTexture); SetShaderValueTexture(tiled.shader, tiled.tilemapTextureLoc, tiled.tilemapTexture.texture); diff --git a/src/tiled.h b/src/tiled.h index c3d0950..de469be 100644 --- a/src/tiled.h +++ b/src/tiled.h @@ -5,13 +5,22 @@ #define SCREEN_W 1280 #define SCREEN_H 720 +#define RENDER_DISTANCE 16 + typedef struct Tiled { TiledMap tiledMap; float zoom; + + // main viewport offset Vector2 offset; + // offset passed to shader for rendering + Vector2 renderOffset; + // offset used to calculate which chunks to render + int chunkOffset[2]; + int atlasSize[2]; - int mapSize[2]; + int renderArea[2]; Texture2D atlasTexture; @@ -23,7 +32,7 @@ typedef struct Tiled { int offsetLoc; int atlasSizeLoc; - int mapSizeLoc; + int renderAreaLoc; int atlasTextureLoc; int tilemapTextureLoc; diff --git a/src/tiledmap.c b/src/tiledmap.c index 1c6964b..6d33463 100644 --- a/src/tiledmap.c +++ b/src/tiledmap.c @@ -133,6 +133,7 @@ void unloadChunk(TiledMap * tiledMap, CachedChunk * cached) { // free memory free(cached->chunk); cached->chunk = NULL; + } //! load a chunk into the cache and return it @@ -151,6 +152,7 @@ CachedChunk * loadChunk(TiledMap * tiledMap, int x, int y) { cached->chunk = malloc(chunkSizeBytes); fseek(tiledMap->file, cached->filePos, SEEK_SET); fread(cached->chunk, 1, chunkSizeBytes, tiledMap->file); + printf("loading chunk %d,%d, from %ld\n", x, y, cached->filePos); } return cached; @@ -182,7 +184,7 @@ CachedChunk * createEmptyChunk(TiledMap * tiledMap, int x, int y) { return createChunk(tiledMap, x, y, chunk); } -char getChunkedTile(TiledMap *tiledMap, int x, int y) { +Tile getChunkedTile(TiledMap *tiledMap, int x, int y) { // TODO put this calculation in function int inChunkX = x % tiledMap->chunkWidth; int inChunkY = y % tiledMap->chunkHeight; @@ -196,11 +198,11 @@ char getChunkedTile(TiledMap *tiledMap, int x, int y) { if (cached->chunk == NULL) return 0; - char v = cached->chunk[inChunkY * tiledMap->chunkWidth + inChunkX]; + Tile v = cached->chunk[inChunkY * tiledMap->chunkWidth + inChunkX]; return v; } -char setChunkedTile(TiledMap * tiledMap, int x, int y, char value) { +Tile setChunkedTile(TiledMap * tiledMap, int x, int y, Tile value) { int inChunkX = x % tiledMap->chunkWidth; int inChunkY = y % tiledMap->chunkHeight; int chunkX = (x - inChunkX) / tiledMap->chunkWidth; @@ -260,7 +262,7 @@ TiledMap openNewTiledMap(char * filename, Image atlas, int tileSize, int chunkWi writeTiledMapHeader(tiledMap); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { - char * chunk = calloc(chunkWidth*chunkHeight, 1); + Chunk chunk = calloc(chunkWidth*chunkHeight, 1); createChunk(&tiledMap, x, y, chunk); } } diff --git a/src/tiledmap.h b/src/tiledmap.h index 2c4cf94..972a74d 100644 --- a/src/tiledmap.h +++ b/src/tiledmap.h @@ -3,7 +3,8 @@ #define CHUNK_CACHE_SIZE 32 -typedef char * Chunk; +typedef unsigned char Tile; +typedef Tile * Chunk; typedef struct CachedChunk { long filePos; @@ -24,8 +25,8 @@ typedef struct ChunkedTiledMap { void textureFromPixels(Texture2D *texOut, Color *pixels, int width, int height); TiledMap openTiledMap(char * filename); CachedChunk * loadChunk(TiledMap *tiledMap, int x, int y); -char getChunkedTile(TiledMap *tiledMap, int x, int y); -char setChunkedTile(TiledMap * tiledMap, int x, int y, char value); +Tile getChunkedTile(TiledMap *tiledMap, int x, int y); +Tile setChunkedTile(TiledMap * tiledMap, int x, int y, Tile value); CachedChunk * createChunk(TiledMap * tiledMap, int x, int y, Chunk chunk); void writeTiledMapHeader(TiledMap tiledMap); TiledMap openNewTiledMap(char * filename, Image atlas, int tileSize, int chunkWidth, int chunkHeight, int width, int height); @@ -13,7 +13,7 @@ uniform sampler2D tilemapTexture; uniform vec2 offset; uniform float zoom; uniform ivec2 atlasSize; -uniform ivec2 mapSize; +uniform ivec2 renderArea; out vec4 finalColor; @@ -68,7 +68,7 @@ void main() { // get position in tiled world wow ivec2 tilemapPos = ivec2(floor(uv)); - if (outBounds(uv, mapSize)) { + if (outBounds(uv, renderArea)) { finalColor = none; } else { vec2 position = mod(uv, 1); |