diff options
| author | ilotterytea <iltsu@alright.party> | 2025-01-26 17:11:56 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-01-26 17:11:56 +0500 |
| commit | 2d7f199341bc2732cc2a238b6caf57424fcc35ac (patch) | |
| tree | 884e01a4a4032b1e9637028968df8bbe54bebb1a /src | |
| parent | 9f5b2b1ebd4bb46163b7f00912db0578aca57d25 (diff) | |
feat: draw tiles
Diffstat (limited to 'src')
| -rw-r--r-- | src/editor.c | 52 | ||||
| -rw-r--r-- | src/editor.h | 14 |
2 files changed, 54 insertions, 12 deletions
diff --git a/src/editor.c b/src/editor.c index 118b1f9..0bb31b2 100644 --- a/src/editor.c +++ b/src/editor.c @@ -1,6 +1,7 @@ #include "editor.h" #include <stdbool.h> +#include <stddef.h> #include <stdio.h> #include <stdlib.h> @@ -30,12 +31,49 @@ void SE_DrawEditor(Editor *editor, Camera2D *camera) { (ry < mousePos.y && mousePos.y < ry + zoom)) { innerColor = SKYBLUE; borderColor = BLUE; + + // placing tiles + if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && + editor->state.selectedTile != NULL) { + 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]; + + if (tt == NULL) continue; + + if (tt->position.x == x && tt->position.y == y) { + t = tt; + break; + } + } + + 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++; + } + + t->tile = editor->state.selectedTile; + } } DrawRectangle(rx, ry, zoom, zoom, innerColor); DrawRectangleLines(rx, ry, zoom, zoom, borderColor); } } + + // rendering placed tiles + for (int i = 0; i < editor->state.cache.tilesCount; i++) { + EditorTile *t = editor->state.cache.tiles[i]; + 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); + } } void drawBuildTab(Editor *editor, int x, int y, int width, int height) { @@ -83,10 +121,8 @@ void drawBuildTab(Editor *editor, int x, int y, int width, int height) { // rendering floor tiles in grid int row = 0, column = 0, texturesPerRow = 10; - for (int i = 0; i < sizeof(editor->state.cache.tiles) / - sizeof(editor->state.cache.tiles[0]); - i++) { - EditorTile *tile = editor->state.cache.tiles[i]; + for (int i = 0; i < editor->state.cache.tileDataSize; i++) { + EditorTileData *tile = editor->state.cache.tileData[i]; if (tile == NULL) continue; row = i / texturesPerRow; @@ -308,10 +344,10 @@ void drawCreatingNewBlock(Editor *editor) { close = true; if (state->isFloor) { - int id = editor->state.cache.tileCacheSize; + int id = editor->state.cache.tileDataSize; - EditorTile *tile = malloc(sizeof(EditorTile)); - editor->state.cache.tiles[id] = tile; + EditorTileData *tile = malloc(sizeof(EditorTileData)); + editor->state.cache.tileData[id] = tile; tile->data = (XdTileData){}; tile->data.id = id; @@ -319,7 +355,7 @@ void drawCreatingNewBlock(Editor *editor) { tile->texture = state->upTexture; - editor->state.cache.tileCacheSize++; + editor->state.cache.tileDataSize++; } } diff --git a/src/editor.h b/src/editor.h index 02966f1..5e51682 100644 --- a/src/editor.h +++ b/src/editor.h @@ -17,12 +17,18 @@ typedef struct { typedef struct { XdTileData data; Texture2D texture; +} EditorTileData; + +typedef struct { + EditorTileData *tile; + Vector2 position; } EditorTile; typedef struct { - int tileCacheSize; - EditorTile *tiles[200]; - Texture2D *textureCache[]; + int tileDataSize; + int tilesCount; + EditorTileData *tileData[200]; + EditorTile *tiles[]; } EditorCache; typedef struct { @@ -30,8 +36,8 @@ typedef struct { EditorCreateBlockState *createBlockState; Rectangle panelView; Vector2 panelScroll; + EditorTileData *selectedTile; EditorCache cache; - EditorTile *selectedTile; } EditorState; typedef struct { |
