summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tiled.c28
-rw-r--r--tiled.glsl41
2 files changed, 35 insertions, 34 deletions
diff --git a/tiled.c b/tiled.c
index 0c9954e..fb51183 100644
--- a/tiled.c
+++ b/tiled.c
@@ -4,17 +4,17 @@
#define SCREEN_W 1280
#define SCREEN_H 720
-#define MAP_W 8
+#define MAP_W 16
#define MAP_H 4
const int tilemap[MAP_H][MAP_W] = {
- {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};
+ {0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0,},
+ {0, 3, 3, 0, 0, 3, 1, 1, 1, 3, 0, 0, 0, 0, 0, 0,},
+ {3, 1, 1, 3, 3, 1, 2, 2, 2, 1, 3, 3, 3, 0, 0, 3,},
+ {1, 2, 2, 1, 1, 2, 4, 4, 4, 2, 1, 1, 1, 3, 3, 1,}
+};
+const int atlasSize[2] = {2, 2};
int main() {
InitWindow(SCREEN_W, SCREEN_H, "tiled");
@@ -27,20 +27,18 @@ int main() {
float offset[2] = {0, 0};
float zoom = 1.0f;
Texture atlas = LoadTexture("tiles.png");
- // size of the tile atlas grid in tiles
-
int resolutionLoc = GetShaderLocation(shader, "resolution");
int locationLoc = GetShaderLocation(shader, "offset");
int zoomLoc = GetShaderLocation(shader, "zoom");
int atlasSizeLoc = GetShaderLocation(shader, "atlasSize");
- int tilemapLoc[MAP_H][MAP_W]= {};
+ 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));
+ for (int y = 0; y < MAP_H; y++) {
+ tilemapLoc[y][x] =
+ GetShaderLocation(shader, TextFormat("tilemap[%d][%d]", y, x));
+ }
}
- }
-
int textureLoc = GetShaderLocation(shader, "texture1");
@@ -78,6 +76,8 @@ int main() {
DrawTexture(target.texture, 0, 0, WHITE);
EndShaderMode();
+ DrawText(TextFormat("FPS: %d", GetFPS()), 12, 12, 24, DARKGRAY);
+
EndDrawing();
}
diff --git a/tiled.glsl b/tiled.glsl
index 9e59b65..9d00690 100644
--- a/tiled.glsl
+++ b/tiled.glsl
@@ -1,4 +1,7 @@
#version 430
+
+#define MAP_H 4
+#define MAP_W 16
precision highp float;
in vec3 vertexPos;
@@ -12,12 +15,19 @@ uniform vec2 resolution;
uniform vec2 offset;
uniform float zoom;
uniform ivec2 atlasSize;
-uniform int tilemap[4][8];
+uniform int tilemap[MAP_H][MAP_W];
out vec4 finalColor;
const vec4 none = vec4(0.0f, 0.0f, 0.0f, 0.0f);
+bool inBounds(vec2 coords, vec2 area) {
+ return coords.x > area.x
+ || coords.x < 0
+ || coords.y > area.y
+ || coords.y < 0;
+}
+
ivec2 calcTileOffset(int tileIndex) {
tileIndex -= 1;
int x = tileIndex % atlasSize.x;
@@ -25,18 +35,14 @@ ivec2 calcTileOffset(int tileIndex) {
return ivec2(x, y);
}
-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) {
+vec4 tile(vec2 coords, int tileIndex) {
+ if (
+ tileIndex == 0
+ || inBounds(coords, vec2(1, 1))) {
return none;
} else {
ivec2 tileOffset = calcTileOffset(tileIndex);
- vec2 texCoords = coords + (tileOffset*tileSize);
- texCoords /= tileSize;
+ vec2 texCoords = coords + (tileOffset);
texCoords /= atlasSize;
return texture(texture1, texCoords);
@@ -45,25 +51,20 @@ vec4 tile(vec2 coords, int tileIndex, vec2 tileSize) {
void main() {
ivec2 texSize = textureSize(texture1, 1);
- vec2 tileSize = texSize / atlasSize;
- vec2 uv = fragTexCoord * tileSize;
+ vec2 uv = fragTexCoord;
uv.x *= resolution.x / resolution.y;
uv *= zoom;
uv -= offset;
// 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) {
+ ivec2 tilemapPos = ivec2(floor(uv));
+ if (inBounds(uv, vec2(MAP_W, MAP_H))) {
finalColor = none;
} else {
- vec2 position = mod(uv,tileSize);
- finalColor = tile(position, tilemap[tilemapPos.y][tilemapPos.x], tileSize);
+ vec2 position = mod(uv, 1);
+ finalColor = tile(position, tilemap[tilemapPos.y][tilemapPos.x]);
}
}