diff options
| -rw-r--r-- | tiled.c | 15 | ||||
| -rw-r--r-- | tiled.glsl | 36 | ||||
| -rw-r--r-- | tiles.png | bin | 875 -> 875 bytes | 
3 files changed, 32 insertions, 19 deletions
| @@ -1,17 +1,19 @@  #include <raylib.h> +#include <wctype.h>  #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)); @@ -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); +    }  }Binary files differ | 
