summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2023-07-11 23:00:41 +0100
committerdavidovski <david@davidovski.xyz>2023-07-11 23:00:41 +0100
commit9675503933d488736bb59f81337ab103fc0b2f57 (patch)
tree4398fab135cf83f9e890b5af055f811aeab0eb24 /src
parentce892407370be42479bb05e9a2faa60a297c9886 (diff)
implement save on exit for editor
Diffstat (limited to 'src')
-rw-r--r--src/editor.c79
-rw-r--r--src/tiledfile.c47
-rw-r--r--src/tiledfile.h3
3 files changed, 104 insertions, 25 deletions
diff --git a/src/editor.c b/src/editor.c
index ac38891..b281300 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -1,17 +1,13 @@
#include <raylib.h>
#include <stdio.h>
+#include <stdlib.h>
#include "tiled.h"
+int lastSelectedTile[2] = {0, 0};
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;
-}
+int mode = -1;
void drawOverlay(Tiled tiled) {
Vector2 screenPos = translateTiledScreenPosition(tiled, (Vector2){selectedTile[0], selectedTile[1]});
@@ -24,18 +20,56 @@ void drawOverlay(Tiled tiled) {
}, tiled.zoom/16, GREEN);
}
-void modifyTile(Tiled *tiled, int i) {
- if (selectedTile[0] >= 0 && selectedTile[0] < tiled->tiledMap.width
- && selectedTile[1] >= 0 && selectedTile[1] < tiled->tiledMap.height) {
- int tile = getTiledMapTile(tiled->tiledMap, selectedTile);
- int tileCount = tiled->tiledMap.atlasSize[0] * tiled->tiledMap.atlasSize[1] + 1;
- tile = (tile + i) % (tileCount);
- setTiledMapTile(tiled->tiledMap, selectedTile, tile);
- redrawTiledMap(*tiled);
+void modifyTile(Tiled *tiled, int tile) {
+ setTiledMapTile(tiled->tiledMap, selectedTile, tile);
+ redrawTiledMap(*tiled);
+}
+
+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);
+}
+
+void handleInputs(Tiled *tiled) {
+ if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
+ setDrawMode(tiled, getTiledMapTile(tiled->tiledMap, selectedTile) + 1);
+
+ if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
+ setDrawMode(tiled, getTiledMapTile(tiled->tiledMap, selectedTile) - 1);
+
+ if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE))
+ setDrawMode(tiled, 0);
+
+ if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT) ||
+ IsMouseButtonReleased(MOUSE_BUTTON_RIGHT) ||
+ IsMouseButtonReleased(MOUSE_BUTTON_MIDDLE)) {
+ mode = -1;
+ }
+
+
+ if (!(selectedTile[0] == lastSelectedTile[0] &&
+ selectedTile[1] == lastSelectedTile[1])) {
+ if (mode != -1) modifyTile(tiled, mode);
+ }
+}
+
+void update(Tiled *tiled) {
+ updateTiledCamera(tiled);
+ Vector2 mousePos = GetMousePosition();
+ Vector2 mapPos = translateTiledPosition(*tiled, mousePos);
+ if (mapPos.x >= 0 && mapPos.x < tiled->tiledMap.width
+ && mapPos.y >= 0 && mapPos.y < tiled->tiledMap.height) {
+ lastSelectedTile[0] = selectedTile[0];
+ lastSelectedTile[1] = selectedTile[1];
+ selectedTile[0] = mapPos.x;
+ selectedTile[1] = mapPos.y;
}
+ handleInputs(tiled);
}
-int launchEditor(TiledMap tiledMap) {
+TiledMap launchEditor(TiledMap tiledMap) {
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(SCREEN_W, SCREEN_H, "tiled");
@@ -44,12 +78,6 @@ int launchEditor(TiledMap tiledMap) {
while (!WindowShouldClose()) {
update(&tiled);
- if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
- modifyTile(&tiled, 1);
-
- if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
- modifyTile(&tiled, -1);
-
BeginDrawing();
ClearBackground(LIGHTGRAY);
@@ -64,10 +92,13 @@ int launchEditor(TiledMap tiledMap) {
unloadTiled(&tiled);
CloseWindow();
- return 0;
+ return tiled.tiledMap;
}
int main() {
TiledMap tiledMap = loadTiledMap("map.tiles");
- launchEditor(tiledMap);
+ 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 64a1e0d..78f9c0e 100644
--- a/src/tiledfile.c
+++ b/src/tiledfile.c
@@ -1,6 +1,7 @@
#include <raylib.h>
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include "tiledfile.h"
@@ -17,7 +18,6 @@ void textureFromPixels(Texture2D *texOut, Color *pixels, int width, int height)
};
*texOut = LoadTextureFromImage(checkedIm);
- UnloadImage(checkedIm);
}
//! read rgba image from file
@@ -28,6 +28,23 @@ void readrgba(Texture2D *loc, int width, int height, FILE *file) {
}
+
+//! write a big endian bytes from file
+int writeb(char * in, size_t noBytes, FILE * file) {
+ if (!is_bigendian()) {
+ int tmp;
+ // reverse byte order
+ for(int i = 0; i < noBytes/2; i++) {
+ tmp = in[i];
+ in[i] = in[noBytes-i-1];
+ in[noBytes-i-1] = tmp;
+ }
+
+ }
+
+ return fwrite(in, (size_t)1, (size_t) noBytes, file);
+}
+
//! read a big endian bytes from file
int readb(char * out, size_t noBytes, FILE * file) {
if (!fread(out, (size_t)1, (size_t) noBytes, file))
@@ -88,7 +105,35 @@ TiledMap loadTiledMap(char * filename) {
tiledMap.atlasData = malloc(atlasSizeBytes);
fread(tiledMap.atlasData, atlasSizeBytes, (size_t) 1, file);
+ tiledMap.tileCount = tiledMap.atlasSize[0]*tiledMap.atlasSize[1] + 1;
+
fclose(file);
return tiledMap;
}
+void saveTiledMap(char * filename, TiledMap tiledMap) {
+ FILE * file;
+
+ if (!(file = fopen(filename, "wb"))) {
+ fprintf(stderr, "Failed to load %s\n", filename);
+ return;
+ }
+ size_t layoutSize = tiledMap.width*tiledMap.height;
+ size_t atlasSizeBytes = tiledMap.atlasSize[0]*tiledMap.tileSize*tiledMap.atlasSize[1]*tiledMap.tileSize*4;
+
+ fwrite("TILEFILEv2", 10, 1, file);
+
+ writeb((char *) &tiledMap.width, 4, file);
+ writeb((char *) &tiledMap.height, 4, file);
+
+ fwrite(tiledMap.tilelayout, 1, layoutSize, file);
+
+ writeb((char *) &tiledMap.tileSize, 4, file);
+ writeb((char *) &tiledMap.atlasSize[0], 4, file);
+ writeb((char *) &tiledMap.atlasSize[1], 4, file);
+
+ fwrite(tiledMap.atlasData, 1, atlasSizeBytes, file);
+
+ fclose(file);
+ fprintf(stderr, "Written tiledfiled to %s\n", filename);
+}
diff --git a/src/tiledfile.h b/src/tiledfile.h
index 906bfdc..d8c98e9 100644
--- a/src/tiledfile.h
+++ b/src/tiledfile.h
@@ -6,6 +6,7 @@ typedef struct TiledMap {
char * tilelayout;
int tileSize;
int atlasSize[2];
+ int tileCount;
Color * atlasData;
} TiledMap;
@@ -18,3 +19,5 @@ void setTiledMapTile(TiledMap tiledMap, int pos[2], char tile);
char getTiledMapTile(TiledMap tiledMap, int pos[2]);
TiledMap loadTiledMap(char * filename);
+
+void saveTiledMap(char * filename, TiledMap tiledMap);