From 5f78a97620329b1b2859fe737ddc3a3ad92d3ee5 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 28 Jan 2025 01:50:51 +0500 Subject: feat: layers --- src/editor.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 11 deletions(-) (limited to 'src/editor.c') 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; } } -- cgit v1.2.3