summaryrefslogtreecommitdiff
path: root/src/editor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/editor.c')
-rw-r--r--src/editor.c210
1 files changed, 36 insertions, 174 deletions
diff --git a/src/editor.c b/src/editor.c
index d445ad3..73164f5 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -11,161 +11,33 @@
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
-#include "xd.h"
const char *EDITOR_MAIN_TABS[] = {"Build"};
const int EDITOR_MAIN_TABS_SIZE =
sizeof(EDITOR_MAIN_TABS) / sizeof(EDITOR_MAIN_TABS[0]);
-void removeNullTiles(Editor *editor) {
- int i, j = 0;
- 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) {
- tiles[j++] = tiles[i];
- }
- }
-
- *count = j;
-
- for (i = j; i < *count; i++) {
- tiles[i] = NULL;
- }
-}
-
void SE_UpdateEditor(Editor *editor) {
- EditorCache *cache = &editor->state.cache;
+ EditorState *state = &editor->state;
// update selected layer
- if (editor->state.selectedTile != NULL &&
- editor->state.selectedTile->data.type != cache->selectedLayer) {
- cache->selectedLayer = editor->state.selectedTile->data.type;
+ if (state->activeTilesetTile != NULL &&
+ state->activeTilesetTile->type != state->activeTileLayerId) {
+ state->activeTileLayerId = state->activeTilesetTile->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++;
- // TODO: allocate memory based on active tiles.
- // remove 1000 and you will get "access violation"
- EditorTileLayer *layer = malloc(sizeof(EditorTileLayer) * 1000);
- 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;
-
- XdLevel *level = editor->data->levels[editor->state.currentLevel];
- XdFloor *floor = level->floors[editor->state.currentLayer];
-
- // rendering placed tiles
- 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++) {
- for (int y = 0; y < floor->height; y++) {
- float rx = x * zoom, ry = y * zoom;
- DrawRectangleLines(rx, ry, zoom, zoom, BLACK);
-
- if ((rx < mousePos.x && mousePos.x < rx + zoom) &&
- (ry < mousePos.y && mousePos.y < ry + zoom) &&
- GetMousePosition().x < EDITOR_TOOLKIT_X) {
- // rendering selected tile
- if (editor->state.selectedTile != NULL) {
- EditorTileData *t = editor->state.selectedTile;
- Rectangle s = {0, 0, 16, 16};
- Rectangle d = {rx, ry, zoom, zoom};
-
- 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 < layer->tilesCount; i++) {
- EditorTile *tt = layer->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};
- layer->tiles[layer->tilesCount] = t;
- layer->tilesCount++;
- }
-
- t->tile = editor->state.selectedTile;
- }
-
- // removing tiles
- if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
- 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);
- layer->tiles[i] = NULL;
- break;
- }
- }
- }
- }
- }
- }
- }
-
- removeNullTiles(editor);
-}
-
-void drawBuildTab(Editor *editor, int x, int y, int width, int height,
- int padding) {
+void drawBuildTab(Editor *editor, Tileset *tileset, int x, int y, int width,
+ int height, int padding) {
GuiPanel((Rectangle){x, y + 24.0f, width, height}, NULL);
// Displaying what tile is selected right now
- EditorTileData *selectedTile = editor->state.selectedTile;
+ TilesetTile *activeTilesetTile = editor->state.activeTilesetTile;
const char *selectedTileLabel;
- if (editor->state.selectedTile != NULL) {
- int type = editor->state.selectedTile->data.type;
+ if (activeTilesetTile != NULL) {
+ int type = activeTilesetTile->type;
if (type == TILE_FLOOR) {
selectedTileLabel = "FLOOR";
} else if (type == TILE_WALL) {
@@ -174,14 +46,14 @@ void drawBuildTab(Editor *editor, int x, int y, int width, int height,
selectedTileLabel = "SMTH";
}
- DrawTextureEx(selectedTile->texture,
+ DrawTextureEx(activeTilesetTile->texture,
(Vector2){x + padding, y + padding * 3.0f}, 0.0f, 6.0f,
WHITE);
} else {
selectedTileLabel = "???";
- DrawRectangle(x + padding, y + padding * 3.0f, TEXTURE_WIDTH * 6.0f,
- TEXTURE_HEIGHT * 6.0f, LIGHTGRAY);
+ DrawRectangle(x + padding, y + padding * 3.0f, TILE_WIDTH * 6.0f,
+ TILE_HEIGHT * 6.0f, LIGHTGRAY);
}
GuiLabel((Rectangle){x + padding, y + padding * 2.0f, width - padding * 2.0f,
@@ -192,7 +64,7 @@ void drawBuildTab(Editor *editor, int x, int y, int width, int height,
Rectangle controlBounds = {x + padding, y + padding + 20.0f * 8.0f,
width - padding * 2.0f, 200.0f};
controlBounds.height = height - controlBounds.y - padding * 4.0f;
- Rectangle contentSize = {x + padding, y + padding, 10 * TEXTURE_WIDTH, 1000};
+ Rectangle contentSize = {x + padding, y + padding, 10 * TILE_WIDTH, 1000};
GuiScrollPanel(controlBounds, NULL, contentSize, &editor->state.panelScroll,
&editor->state.panelView);
@@ -206,35 +78,34 @@ void drawBuildTab(Editor *editor, int x, int y, int width, int height,
// rendering floor tiles in grid
int row = 0, column = 0, texturesPerRow = 10;
float scale = 3.0f;
- for (int i = 0; i < editor->state.cache.tileDataSize; i++) {
- EditorTileData *tile = editor->state.cache.tileData[i];
+ for (int i = 0; i < tileset->tileCount; i++) {
+ TilesetTile *tile = tileset->tiles[i];
if (tile == NULL) continue;
row = i / texturesPerRow;
column = i % texturesPerRow;
- int x = controlBounds.x + 5 + TEXTURE_WIDTH * column * scale;
+ int x = controlBounds.x + 5 + TILE_WIDTH * column * scale;
int y = controlBounds.y + editor->state.panelScroll.y +
- TEXTURE_HEIGHT * row * scale;
+ TILE_HEIGHT * row * scale;
DrawTextureEx(tile->texture, (Vector2){x, y}, 0.0f, scale, WHITE);
bool isHovered =
- (x <= mousePos.x && mousePos.x <= x + TEXTURE_WIDTH * scale) &&
- (y <= mousePos.y && mousePos.y <= y + TEXTURE_HEIGHT * scale);
+ (x <= mousePos.x && mousePos.x <= x + TILE_WIDTH * scale) &&
+ (y <= mousePos.y && mousePos.y <= y + TILE_HEIGHT * scale);
- bool sameTile = editor->state.selectedTile != NULL &&
- editor->state.selectedTile->data.id == tile->data.id;
+ bool sameTile =
+ activeTilesetTile != NULL && activeTilesetTile->id == tile->id;
// hover event
if (isHovered || sameTile) {
- DrawRectangleLines(x, y, TEXTURE_WIDTH * scale, TEXTURE_HEIGHT * scale,
- YELLOW);
+ DrawRectangleLines(x, y, TILE_WIDTH * scale, TILE_HEIGHT * scale, YELLOW);
}
// select tile
if (isHovered && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
- editor->state.selectedTile = tile;
+ editor->state.activeTilesetTile = tile;
}
}
@@ -285,10 +156,10 @@ void drawPreview(EditorCreateBlockState *state, int wx, int wy, int ww, int wh,
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 5; y++) {
if (state->isFloor) {
- DrawTexture(state->upTexture, wx + pad + x * TEXTURE_WIDTH,
- wy + pad + 24.0f * 5.0f + y * TEXTURE_HEIGHT, WHITE);
+ DrawTexture(state->upTexture, wx + pad + x * TILE_WIDTH,
+ wy + pad + 24.0f * 5.0f + y * TILE_HEIGHT, WHITE);
} else {
- Vector2 position = {x * TEXTURE_WIDTH, y * TEXTURE_HEIGHT};
+ Vector2 position = {x * TILE_WIDTH, y * TILE_HEIGHT};
position.x += wx + pad;
position.y += wy + pad + 24.0f * 5.0f;
@@ -351,8 +222,8 @@ void loadTexture(EditorCreateBlockState *state) {
state->upFilePath = outPath;
Image image = LoadImage(state->upFilePath);
- if (image.width != TEXTURE_WIDTH || image.height != TEXTURE_HEIGHT) {
- ImageResizeNN(&image, TEXTURE_WIDTH, TEXTURE_HEIGHT);
+ if (image.width != TILE_WIDTH || image.height != TILE_HEIGHT) {
+ ImageResizeNN(&image, TILE_WIDTH, TILE_HEIGHT);
}
state->upTexture = LoadTextureFromImage(image);
@@ -361,7 +232,7 @@ void loadTexture(EditorCreateBlockState *state) {
}
}
-void drawCreatingNewBlock(Editor *editor) {
+void drawCreatingNewBlock(Editor *editor, Tileset *tileset) {
EditorCreateBlockState *state = editor->state.createBlockState;
int ww = 300;
@@ -396,8 +267,8 @@ void drawCreatingNewBlock(Editor *editor) {
}
// Changing the texture
else {
- if (GuiButton((Rectangle){wx + pad * 1.5f + TEXTURE_WIDTH,
- wy + pad + 24.0f * 3.0f - TEXTURE_WIDTH / 2.0f,
+ if (GuiButton((Rectangle){wx + pad * 1.5f + TILE_WIDTH,
+ wy + pad + 24.0f * 3.0f - TILE_WIDTH / 2.0f,
24.0f, 24.0f},
"#211#")) {
loadTexture(state);
@@ -417,18 +288,9 @@ void drawCreatingNewBlock(Editor *editor) {
if (createButton) {
close = true;
- int id = editor->state.cache.tileDataSize;
-
- EditorTileData *tile = malloc(sizeof(EditorTileData));
- editor->state.cache.tileData[id] = tile;
-
- tile->data = (XdTileData){};
- tile->data.id = id;
- tile->data.type = state->isFloor ? TILE_FLOOR : TILE_WALL;
-
- tile->texture = state->upTexture;
+ TilesetTileType type = state->isFloor ? TILE_FLOOR : TILE_WALL;
- editor->state.cache.tileDataSize++;
+ SE_AddTilesetTile(tileset, state->upTexture, type);
}
if (close) {
@@ -446,10 +308,10 @@ void drawCreatingNewBlock(Editor *editor) {
}
}
-void SE_DrawEditorToolkit(Editor *editor) {
+void SE_DrawEditor(Editor *editor, Tileset *tileset) {
switch (editor->state.activeMainTab) {
case 0: {
- drawBuildTab(editor, EDITOR_TOOLKIT_X, EDITOR_TOOLKIT_Y,
+ drawBuildTab(editor, tileset, EDITOR_TOOLKIT_X, EDITOR_TOOLKIT_Y,
EDITOR_TOOLKIT_WIDTH, EDITOR_TOOLKIT_HEIGHT, 20);
break;
}
@@ -463,6 +325,6 @@ void SE_DrawEditorToolkit(Editor *editor) {
// Creating new block
if (editor->state.createBlockState != NULL) {
- drawCreatingNewBlock(editor);
+ drawCreatingNewBlock(editor, tileset);
}
} \ No newline at end of file