From 50a8424269b868bc89be72fe19756bab8f6d12e9 Mon Sep 17 00:00:00 2001 From: davidovski Date: Sun, 23 Apr 2023 03:38:03 +0100 Subject: Render map based on passed tilemap --- tiled.c | 15 ++++++++------- tiled.glsl | 36 ++++++++++++++++++++++++------------ tiles.png | Bin 875 -> 875 bytes 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/tiled.c b/tiled.c index f3e34f2..0c9954e 100644 --- a/tiled.c +++ b/tiled.c @@ -1,17 +1,19 @@ #include +#include #define SCREEN_W 1280 #define SCREEN_H 720 -#define MAP_W 4 +#define MAP_W 8 #define MAP_H 4 const int tilemap[MAP_H][MAP_W] = { - {0, 0, 0, 0}, - {0, 0, 3, 3}, - {3, 3, 1, 1}, - {1, 1, 2, 2} + {0, 0, 0, 0, 0, 0, 3, 3}, + {0, 3, 3, 0, 0, 3, 1, 1}, + {3, 1, 1, 3, 3, 1, 2, 2}, + {1, 2, 2, 1, 1, 2, 4, 4} }; +const int atlasSize[2] = {2, 2}; int main() { @@ -26,14 +28,13 @@ int main() { float zoom = 1.0f; Texture atlas = LoadTexture("tiles.png"); // size of the tile atlas grid in tiles - int atlasSize[2] = {2, 2}; int resolutionLoc = GetShaderLocation(shader, "resolution"); int locationLoc = GetShaderLocation(shader, "offset"); int zoomLoc = GetShaderLocation(shader, "zoom"); int atlasSizeLoc = GetShaderLocation(shader, "atlasSize"); - int tilemapLoc[MAP_W][MAP_H]= {}; + int tilemapLoc[MAP_H][MAP_W]= {}; for (int x = 0; x < MAP_W; x++) { for (int y = 0; y < MAP_H; y++) { tilemapLoc[y][x] = GetShaderLocation(shader, TextFormat("tilemap[%d][%d]", y, x)); diff --git a/tiled.glsl b/tiled.glsl index 2a0e5d9..9e59b65 100644 --- a/tiled.glsl +++ b/tiled.glsl @@ -12,10 +12,12 @@ uniform vec2 resolution; uniform vec2 offset; uniform float zoom; uniform ivec2 atlasSize; -uniform int tilemap[4][4]; +uniform int tilemap[4][8]; out vec4 finalColor; +const vec4 none = vec4(0.0f, 0.0f, 0.0f, 0.0f); + ivec2 calcTileOffset(int tileIndex) { tileIndex -= 1; int x = tileIndex % atlasSize.x; @@ -23,35 +25,45 @@ ivec2 calcTileOffset(int tileIndex) { return ivec2(x, y); } -vec4 tile(vec2 coords, int tileIndex) { - ivec2 texSize = textureSize(texture1, 1); - - // for now just render the first tile - - - vec2 tileSize = texSize / atlasSize; +vec4 tile(vec2 coords, int tileIndex, vec2 tileSize) { if (tileIndex == 0 || coords.x > tileSize.x || coords.x < 0 || coords.y > tileSize.y || coords.y < 0) { - return vec4(0.0f, 0.0f, 0.0f, 0.0f); + return none; } else { ivec2 tileOffset = calcTileOffset(tileIndex); vec2 texCoords = coords + (tileOffset*tileSize); - texCoords /= texSize; + texCoords /= tileSize; + texCoords /= atlasSize; return texture(texture1, texCoords); } } void main() { - vec2 uv = fragTexCoord; + ivec2 texSize = textureSize(texture1, 1); + vec2 tileSize = texSize / atlasSize; + + vec2 uv = fragTexCoord * tileSize; uv.x *= resolution.x / resolution.y; uv *= zoom; uv -= offset; - finalColor = tile(uv, tilemap[2][2]); + // get position in tiled world wow + ivec2 tilemapPos = ivec2(floor(uv.x / tileSize.x), floor(uv.y / tileSize.y)); + + if (uv.x < 0 + || uv.y < 0 + || uv.x > 8*tileSize.x + || uv.y > 8*tileSize.y) { + + finalColor = none; + } else { + vec2 position = mod(uv,tileSize); + finalColor = tile(position, tilemap[tilemapPos.y][tilemapPos.x], tileSize); + } } diff --git a/tiles.png b/tiles.png index 3a86dc4..455750a 100644 Binary files a/tiles.png and b/tiles.png differ -- cgit v1.2.1