summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-07-11 23:24:54 +0100
committerdavidovski <david@davidovski.xyz>2023-07-11 23:24:54 +0100
commit3f2113f823f94ca91e93182cc5a2ab6b0ac5cfb6 (patch)
tree3921730af4301b705682d14d1f51b3ba545b6418
parent9675503933d488736bb59f81337ab103fc0b2f57 (diff)
tidy saving code
-rw-r--r--src/editor.c3
-rw-r--r--src/tiledfile.c16
-rw-r--r--src/tiledfile.h4
-rw-r--r--tiled.glsl18
4 files changed, 32 insertions, 9 deletions
diff --git a/src/editor.c b/src/editor.c
index b281300..69162fd 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -28,7 +28,6 @@ void modifyTile(Tiled *tiled, int tile) {
void setDrawMode(Tiled *tiled, int tile) {
mode = tile % tiled->tiledMap.tileCount;
if (mode < 0) mode += tiled->tiledMap.tileCount;
- printf("mode: %d\n", tiled->tiledMap.tileCount);
modifyTile(tiled, mode);
}
@@ -97,8 +96,6 @@ TiledMap launchEditor(TiledMap tiledMap) {
int main() {
TiledMap tiledMap = loadTiledMap("map.tiles");
- printf("the top left is %d\n", tiledMap.tilelayout[0]);
TiledMap editedTiledMap = launchEditor(tiledMap);
- printf("the top left is %d\n", editedTiledMap.tilelayout[0]);
saveTiledMap("map.tiles", tiledMap);
}
diff --git a/src/tiledfile.c b/src/tiledfile.c
index 78f9c0e..eaeb814 100644
--- a/src/tiledfile.c
+++ b/src/tiledfile.c
@@ -111,6 +111,22 @@ TiledMap loadTiledMap(char * filename) {
return tiledMap;
}
+TiledMap newTiledMap(Image atlas, int tileSize, int width, int height) {
+ TiledMap tiledMap;
+ tiledMap.width = width;
+ tiledMap.height = height;
+ tiledMap.tilelayout = malloc(width * height);
+
+ tiledMap.tileSize = tileSize;
+
+ tiledMap.atlasSize[0] = atlas.width / tileSize;
+ tiledMap.atlasSize[1] = atlas.height / tileSize;
+
+ tiledMap.atlasData = LoadImageColors(atlas);
+
+ return tiledMap;
+}
+
void saveTiledMap(char * filename, TiledMap tiledMap) {
FILE * file;
diff --git a/src/tiledfile.h b/src/tiledfile.h
index d8c98e9..fb9c789 100644
--- a/src/tiledfile.h
+++ b/src/tiledfile.h
@@ -12,8 +12,6 @@ typedef struct TiledMap {
void textureFromPixels(Texture2D *texOut, Color *pixels, int width, int height);
-void renderTilemapTexture(Texture2D *texOut, TiledMap tiledMap);
-
void setTiledMapTile(TiledMap tiledMap, int pos[2], char tile);
char getTiledMapTile(TiledMap tiledMap, int pos[2]);
@@ -21,3 +19,5 @@ char getTiledMapTile(TiledMap tiledMap, int pos[2]);
TiledMap loadTiledMap(char * filename);
void saveTiledMap(char * filename, TiledMap tiledMap);
+
+TiledMap newTiledMap(Image atlas, int tileSize, int width, int height);
diff --git a/tiled.glsl b/tiled.glsl
index c5f762d..ce7ebea 100644
--- a/tiled.glsl
+++ b/tiled.glsl
@@ -18,8 +18,9 @@ uniform ivec2 mapSize;
out vec4 finalColor;
const vec4 none = vec4(0.0f, 0.0f, 0.0f, 0.0f);
+const vec4 gridColor = vec4(0.0f, 0.0f, 0.0f, 0.2f);
-bool inBounds(vec2 coords, vec2 area) {
+bool outBounds(vec2 coords, vec2 area) {
return coords.x > area.x
|| coords.x < 0
|| coords.y > area.y
@@ -33,11 +34,20 @@ ivec2 calcTileOffset(int tileIndex) {
return ivec2(x, y);
}
+vec4 drawGrid(vec2 coords) {
+ if (abs(floor(coords.x) - coords.x) < 1/16
+ || abs(floor(coords.y) - coords.y) < 1/16) {
+ return gridColor;
+ } else {
+ return none;
+ }
+}
+
vec4 tile(vec2 coords, int tileIndex) {
if (
tileIndex == 0
- || inBounds(coords, vec2(1, 1))) {
- return none;
+ || outBounds(coords, vec2(1, 1))) {
+ return drawGrid(coords);
} else {
ivec2 tileOffset = calcTileOffset(tileIndex);
vec2 texCoords = coords + (tileOffset);
@@ -58,7 +68,7 @@ void main() {
// get position in tiled world wow
ivec2 tilemapPos = ivec2(floor(uv));
- if (inBounds(uv, mapSize)) {
+ if (outBounds(uv, mapSize)) {
finalColor = none;
} else {
vec2 position = mod(uv, 1);