summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/editor.c59
-rw-r--r--src/editor.h11
-rw-r--r--src/main.c2
3 files changed, 59 insertions, 13 deletions
diff --git a/src/editor.c b/src/editor.c
index 5c7de0b..0f637b5 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -19,8 +19,10 @@ const int EDITOR_MAIN_TABS_SIZE =
void removeNullTiles(Editor *editor) {
int i, j = 0;
- EditorTile **tiles = editor->state.cache.tiles;
- int *count = &editor->state.cache.tilesCount;
+ EditorTileLayer *layer =
+ editor->state.cache.layers[editor->state.cache.selectedLayer];
+ EditorTile **tiles = layer->tiles;
+ int *count = &layer->tilesCount;
for (i = 0; i < *count; i++) {
if (tiles[i] != NULL) {
@@ -35,6 +37,32 @@ void removeNullTiles(Editor *editor) {
}
}
+void SE_UpdateEditor(Editor *editor) {
+ EditorCache *cache = &editor->state.cache;
+
+ // update selected layer
+ if (editor->state.selectedTile != NULL &&
+ editor->state.selectedTile->data.type != cache->selectedLayer) {
+ cache->selectedLayer = editor->state.selectedTile->data.type;
+ SE_LOG_INFO("Changed selected layer");
+ }
+
+ // creating a new layer
+ EditorTileLayer **layers = cache->layers;
+
+ int selectedLayer = cache->selectedLayer;
+
+ if (layers[selectedLayer] == NULL || selectedLayer >= cache->layerCount) {
+ cache->layerCount++;
+ EditorTileLayer *layer = malloc(sizeof(EditorTileLayer));
+ layer->index = selectedLayer;
+ layer->tilesCount = 0;
+
+ layers[selectedLayer] = layer;
+ SE_LOG_INFO("Created a new layer");
+ }
+}
+
void SE_DrawEditor(Editor *editor, Camera2D *camera) {
Vector2 mousePos = GetScreenToWorld2D(GetMousePosition(), *camera);
float zoom = camera->zoom;
@@ -43,14 +71,19 @@ void SE_DrawEditor(Editor *editor, Camera2D *camera) {
XdFloor *floor = level->floors[editor->state.currentLayer];
// rendering placed tiles
- for (int i = 0; i < editor->state.cache.tilesCount; i++) {
- EditorTile *t = editor->state.cache.tiles[i];
+ for (int li = 0; li < editor->state.cache.layerCount; li++) {
+ EditorTileLayer *layer = editor->state.cache.layers[li];
+ if (layer == NULL) continue;
+
+ for (int ti = 0; ti < layer->tilesCount; ti++) {
+ EditorTile *t = layer->tiles[ti];
if (t == NULL) continue;
Rectangle s = {0, 0, 16, 16};
Rectangle d = {t->position.x * zoom, t->position.y * zoom, zoom, zoom};
DrawTexturePro(t->tile->texture, s, d, (Vector2){0, 0}, 0.0f, WHITE);
+ }
}
for (int x = 0; x < floor->width; x++) {
@@ -70,12 +103,16 @@ void SE_DrawEditor(Editor *editor, Camera2D *camera) {
if (!IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
DrawTexturePro(t->texture, s, d, (Vector2){0, 0}, 0.0f, WHITE);
+ EditorTileLayer *layer =
+ editor->state.cache.layers[editor->state.cache.selectedLayer];
+
// placing tiles
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
EditorTile *t = NULL;
+
// checking for existing tiles on the position
- for (int i = 0; i < editor->state.cache.tilesCount; i++) {
- EditorTile *tt = editor->state.cache.tiles[i];
+ for (int i = 0; i < layer->tilesCount; i++) {
+ EditorTile *tt = layer->tiles[i];
if (tt == NULL) continue;
@@ -88,8 +125,8 @@ void SE_DrawEditor(Editor *editor, Camera2D *camera) {
if (t == NULL) {
t = malloc(sizeof(EditorTile));
t->position = (Vector2){x, y};
- editor->state.cache.tiles[editor->state.cache.tilesCount] = t;
- editor->state.cache.tilesCount++;
+ layer->tiles[layer->tilesCount] = t;
+ layer->tilesCount++;
}
t->tile = editor->state.selectedTile;
@@ -97,13 +134,13 @@ void SE_DrawEditor(Editor *editor, Camera2D *camera) {
// removing tiles
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
- for (int i = 0; i < editor->state.cache.tilesCount; i++) {
- EditorTile *tt = editor->state.cache.tiles[i];
+ for (int i = 0; i < layer->tilesCount; i++) {
+ EditorTile *tt = layer->tiles[i];
if (tt == NULL) continue;
if (tt->position.x == x && tt->position.y == y) {
free(tt);
- editor->state.cache.tiles[i] = NULL;
+ layer->tiles[i] = NULL;
break;
}
}
diff --git a/src/editor.h b/src/editor.h
index 0c32cdf..65e57e8 100644
--- a/src/editor.h
+++ b/src/editor.h
@@ -30,10 +30,16 @@ typedef struct {
} EditorTile;
typedef struct {
+ int index, tilesCount;
+ EditorTile *tiles[];
+} EditorTileLayer;
+
+typedef struct {
int tileDataSize;
- int tilesCount;
+ int layerCount;
+ int selectedLayer;
EditorTileData *tileData[200];
- EditorTile *tiles[];
+ EditorTileLayer *layers[];
} EditorCache;
typedef struct {
@@ -50,6 +56,7 @@ typedef struct {
EditorState state;
} Editor;
+void SE_UpdateEditor(Editor *editor);
void SE_DrawEditor(Editor *editor, Camera2D *camera);
void SE_DrawEditorToolkit(Editor *editor);
void SE_RebuildEditorCache(Editor *editor);
diff --git a/src/main.c b/src/main.c
index 981a7a9..dd59a11 100644
--- a/src/main.c
+++ b/src/main.c
@@ -46,6 +46,8 @@ int main() {
camera.zoom = 4.0f;
while (!WindowShouldClose()) {
+ SE_UpdateEditor(&editor);
+
// interact with the map if the mouse is outside build tab
if (GetMousePosition().x < EDITOR_TOOLKIT_X) {
if (GetMouseWheelMove() != 0.0) {