summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-07-11 18:28:48 +0100
committerdavidovski <david@davidovski.xyz>2023-07-11 19:29:05 +0100
commit625cb961a3b35afdaf6d5c1e8bf4966a522a022a (patch)
treecf2cafdd8d7ca670712f930ab9175de71be3527c /src
parente8b6b1f0ee8b825443972b99b191fd5a85437008 (diff)
add editor
Diffstat (limited to 'src')
-rw-r--r--src/editor.c70
-rw-r--r--src/tiled.c58
-rw-r--r--src/tiled.h9
-rw-r--r--src/tiledfile.c9
-rw-r--r--src/tiledfile.h4
5 files changed, 125 insertions, 25 deletions
diff --git a/src/editor.c b/src/editor.c
new file mode 100644
index 0000000..2cf2f84
--- /dev/null
+++ b/src/editor.c
@@ -0,0 +1,70 @@
+#include <raylib.h>
+#include <stdio.h>
+
+#include "tiled.h"
+
+int selectedTile[2] = {0, 0};
+
+void update(Tiled *tiled) {
+ updateTiledCamera(tiled);
+ Vector2 mousePos = GetMousePosition();
+ Vector2 mapPos = translateTiledPosition(*tiled, mousePos);
+ selectedTile[0] = mapPos.x;
+ selectedTile[1] = mapPos.y;
+}
+
+void drawOverlay(Tiled tiled) {
+ Vector2 screenPos = translateTiledScreenPosition(tiled, (Vector2){selectedTile[0], selectedTile[1]});
+
+ DrawRectangleLinesEx((Rectangle) {
+ screenPos.x,
+ screenPos.y,
+ tiled.zoom,
+ tiled.zoom
+ }, tiled.zoom/16, GREEN);
+}
+
+void modifyTile(Tiled *tiled, int x) {
+ int tile = getTiledMapTile(tiled->tiledMap, selectedTile);
+ int tileCount = tiled->tiledMap.atlasSize[0] * tiled->tiledMap.atlasSize[1] + 1;
+ tile = (tile + x) % (tileCount);
+ setTiledMapTile(tiled->tiledMap, selectedTile, tile);
+ renderTilemapTexture(&tiled->tilemapTexture, tiled->tiledMap);
+}
+
+int launchEditor(TiledMap tiledMap) {
+ SetConfigFlags(FLAG_WINDOW_RESIZABLE);
+ InitWindow(SCREEN_W, SCREEN_H, "tiled");
+
+ Tiled tiled = initTiled(tiledMap);
+
+ while (!WindowShouldClose()) {
+ update(&tiled);
+
+ if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
+ modifyTile(&tiled, 1);
+
+ if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
+ modifyTile(&tiled, -1);
+
+ BeginDrawing();
+
+ ClearBackground(LIGHTGRAY);
+
+ drawTiled(&tiled);
+ drawOverlay(tiled);
+ DrawFPS(16, 16);
+
+ EndDrawing();
+ }
+
+ unloadTiled(&tiled);
+
+ CloseWindow();
+ return 0;
+}
+
+int main() {
+ TiledMap tiledMap = loadTiledMap("map.tiles");
+ launchEditor(tiledMap);
+}
diff --git a/src/tiled.c b/src/tiled.c
index 49f957f..82b29e0 100644
--- a/src/tiled.c
+++ b/src/tiled.c
@@ -3,9 +3,6 @@
#include "tiled.h"
-#define SCREEN_W 1280
-#define SCREEN_H 720
-
void updateCamera(Vector2 *offset, float *zoom) {
if (IsKeyDown(KEY_UP)) offset->y += 16.0f / *zoom;
if (IsKeyDown(KEY_DOWN)) offset->y -= 16.0f/ *zoom;
@@ -16,8 +13,8 @@ void updateCamera(Vector2 *offset, float *zoom) {
if (IsKeyDown(KEY_S)) *zoom -= *zoom * 0.01f;
}
-void drawOverlay() {
- DrawText(TextFormat("FPS: %d", GetFPS()), 12, 12, 24, DARKGRAY);
+void updateTiledCamera(Tiled *tiled) {
+ updateCamera(&tiled->offset, &tiled->zoom);
}
@@ -34,24 +31,41 @@ void initTiledShader(Tiled *tiled) {
tiled->tilemapTextureLoc = GetShaderLocation(tiled->shader, "tilemapTexture");
}
-void initTiled(Tiled *tiled, TiledMap tiledMap) {
- tiled->offset = (Vector2) {0, 0};
- tiled->zoom = 64;
- tiled->mapSize[0] = tiled->tilemapTexture.width;
- tiled->mapSize[1] = tiled->tilemapTexture.height;
- tiled->targetTexture = LoadRenderTexture(1, 1);
+Vector2 translateTiledPosition(Tiled tiled, Vector2 screenPos) {
+ return (Vector2) {
+ screenPos.x / tiled.zoom - tiled.offset.x,
+ screenPos.y / tiled.zoom - tiled.offset.y
+ };
+}
+
+Vector2 translateTiledScreenPosition(Tiled tiled, Vector2 tiledPos) {
+ return (Vector2) {
+ (tiledPos.x + tiled.offset.x) * tiled.zoom,
+ (tiledPos.y + tiled.offset.y) * tiled.zoom
+ };
+}
+
+Tiled initTiled(TiledMap tiledMap) {
+ Tiled tiled;
+ tiled.tiledMap = tiledMap;
+ tiled.offset = (Vector2) {0, 0};
+ tiled.zoom = 64;
+ tiled.mapSize[0] = tiled.tilemapTexture.width;
+ tiled.mapSize[1] = tiled.tilemapTexture.height;
+ tiled.targetTexture = LoadRenderTexture(1, 1);
- tiled->atlasSize[0] = tiledMap.atlasSize[0];
- tiled->atlasSize[1] = tiledMap.atlasSize[1];
+ tiled.atlasSize[0] = tiledMap.atlasSize[0];
+ tiled.atlasSize[1] = tiledMap.atlasSize[1];
- textureFromPixels(&tiled->atlasTexture,
+ textureFromPixels(&tiled.atlasTexture,
tiledMap.atlasData,
tiledMap.atlasSize[0] * tiledMap.tileSize,
tiledMap.atlasSize[1] * tiledMap.tileSize);
- renderTilemapTexture(&tiled->tilemapTexture, tiledMap);
- initTiledShader(tiled);
+ renderTilemapTexture(&tiled.tilemapTexture, tiled.tiledMap);
+ initTiledShader(&tiled);
+ return tiled;
}
void setTiledShaderUniforms(Tiled tiled) {
@@ -73,8 +87,6 @@ void unloadTiled(Tiled *tiled) {
}
void drawTiled(Tiled *tiled) {
- updateCamera(&tiled->offset, &tiled->zoom);
-
BeginShaderMode(tiled->shader);
setTiledShaderUniforms(*tiled);
@@ -85,24 +97,22 @@ void drawTiled(Tiled *tiled) {
EndShaderMode();
}
-int main() {
+int launchTiledView() {
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(SCREEN_W, SCREEN_H, "tiled");
- Tiled tiled;
TiledMap tiledMap = loadTiledMap("map.tiles");
- initTiled(&tiled, tiledMap);
-
-
+ Tiled tiled = initTiled(tiledMap);
while (!WindowShouldClose()) {
+ updateTiledCamera(&tiled);
BeginDrawing();
ClearBackground(LIGHTGRAY);
drawTiled(&tiled);
- drawOverlay();
+ DrawFPS(16, 16);
EndDrawing();
}
diff --git a/src/tiled.h b/src/tiled.h
index f496dc6..e1f1c79 100644
--- a/src/tiled.h
+++ b/src/tiled.h
@@ -2,7 +2,11 @@
#include "tiledfile.h"
+#define SCREEN_W 1280
+#define SCREEN_H 720
+
typedef struct Tiled {
+ TiledMap tiledMap;
float zoom;
Vector2 offset;
@@ -26,6 +30,9 @@ typedef struct Tiled {
} Tiled;
-void initTiled(Tiled *tiled, TiledMap tiledMap);
+void updateTiledCamera(Tiled *tiled);
+Vector2 translateTiledPosition(Tiled tiled, Vector2 screenPos);
+Vector2 translateTiledScreenPosition(Tiled tiled, Vector2 tiledPos);
+Tiled initTiled(TiledMap tiledMap);
void drawTiled(Tiled *tiled);
void unloadTiled(Tiled *tiled);
diff --git a/src/tiledfile.c b/src/tiledfile.c
index d6d4b1b..bf0a828 100644
--- a/src/tiledfile.c
+++ b/src/tiledfile.c
@@ -30,6 +30,7 @@ void renderTilemapTexture(Texture2D *texOut, TiledMap tiledMap) {
textureFromPixels(texOut, pixels, tiledMap.width, tiledMap.height);
}
+
//! read rgba image from file
void readrgba(Texture2D *loc, int width, int height, FILE *file) {
Color *pixels = malloc(width*height*4);
@@ -57,6 +58,14 @@ int readb(char * out, size_t noBytes, FILE * file) {
return 0;
}
+char getTiledMapTile(TiledMap tiledMap, int pos[2]) {
+ return tiledMap.tilelayout[pos[1]*tiledMap.width + pos[0]];
+}
+
+void setTiledMapTile(TiledMap tiledMap, int pos[2], char tile) {
+ tiledMap.tilelayout[pos[1]*tiledMap.width + pos[0]] = tile;
+}
+
//! load tilemap data from file
TiledMap loadTiledMap(char * filename) {
TiledMap tiledMap;
diff --git a/src/tiledfile.h b/src/tiledfile.h
index 929dabc..906bfdc 100644
--- a/src/tiledfile.h
+++ b/src/tiledfile.h
@@ -13,4 +13,8 @@ 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]);
+
TiledMap loadTiledMap(char * filename);