summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/editor.c210
-rw-r--r--src/editor.h39
-rw-r--r--src/floor.c179
-rw-r--r--src/floor.h37
-rw-r--r--src/main.c48
-rw-r--r--src/tileset.c38
-rw-r--r--src/tileset.h28
-rw-r--r--src/xd.c35
-rw-r--r--src/xd.h72
9 files changed, 347 insertions, 339 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
diff --git a/src/editor.h b/src/editor.h
index 65e57e8..b0b0441 100644
--- a/src/editor.h
+++ b/src/editor.h
@@ -3,16 +3,13 @@
#include <raylib.h>
#include <stdbool.h>
-#include "xd.h"
+#include "tileset.h"
#define EDITOR_TOOLKIT_WIDTH 400.0f
#define EDITOR_TOOLKIT_HEIGHT GetScreenHeight()
#define EDITOR_TOOLKIT_X GetScreenWidth() - EDITOR_TOOLKIT_WIDTH
#define EDITOR_TOOLKIT_Y 0.0f
-#define TEXTURE_WIDTH 16.0f
-#define TEXTURE_HEIGHT 16.0f
-
typedef struct {
bool isFloor, isWall;
char *upFilePath, *sideFilePath, *cornerFilePath;
@@ -20,44 +17,20 @@ typedef struct {
} EditorCreateBlockState;
typedef struct {
- XdTileData data;
- Texture2D texture;
-} EditorTileData;
-
-typedef struct {
- EditorTileData *tile;
- Vector2 position;
-} EditorTile;
-
-typedef struct {
- int index, tilesCount;
- EditorTile *tiles[];
-} EditorTileLayer;
+ int activeMainTab;
+ int activeTileLayerId;
-typedef struct {
- int tileDataSize;
- int layerCount;
- int selectedLayer;
- EditorTileData *tileData[200];
- EditorTileLayer *layers[];
-} EditorCache;
-
-typedef struct {
- int currentLevel, currentLayer, activeMainTab;
+ TilesetTile *activeTilesetTile;
EditorCreateBlockState *createBlockState;
Rectangle panelView;
Vector2 panelScroll;
- EditorTileData *selectedTile;
- EditorCache cache;
} EditorState;
typedef struct {
- XdData *data;
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);
+void SE_DrawEditor(Editor *editor, Tileset *tileset);
+
#endif
diff --git a/src/floor.c b/src/floor.c
new file mode 100644
index 0000000..22b2718
--- /dev/null
+++ b/src/floor.c
@@ -0,0 +1,179 @@
+#include "floor.h"
+
+#include "logger.h"
+#include "stdlib.h"
+
+Tile *SE_CreateTile(TileLayer *layer, TilesetTile *tilesetTile) {
+ Tile *tile = malloc(sizeof(Tile));
+
+ tile->id = layer->tileCount;
+ tile->tilesetTile = tilesetTile;
+ tile->position = (Vector2){0.0f, 0.0f};
+
+ layer->tiles[layer->tileCount] = tile;
+ layer->tileCount++;
+
+ return tile;
+}
+
+void SE_RemoveTile(TileLayer *layer, Tile *tile) {
+ int index = -1;
+
+ for (int i = 0; i < layer->tileCount; i++) {
+ Tile *t = layer->tiles[i];
+ if (t->id == tile->id) {
+ index = i;
+ break;
+ }
+ }
+
+ if (index == -1) {
+ SE_LOG_WARN("Tile doesn't belong to the layer");
+ return;
+ }
+
+ free(tile);
+ layer->tiles[index] = NULL;
+
+ // clearing layer from null values
+ int i, j = 0;
+ Tile **tiles = layer->tiles;
+ int *count = &layer->tileCount;
+
+ 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;
+ }
+}
+
+Tile *SE_FindTileAtPosition(TileLayer *layer, Vector2 position) {
+ Tile *tile = NULL;
+
+ for (int i = 0; i < layer->tileCount; i++) {
+ Tile *t = layer->tiles[i];
+ if (t->position.x == position.x && t->position.y == position.y) {
+ tile = t;
+ break;
+ }
+ }
+
+ return tile;
+}
+
+TileFloor *SE_CreateTileFloor(int width, int height) {
+ TileFloor *floor = malloc(sizeof(TileFloor));
+
+ floor->width = width;
+ floor->height = height;
+ floor->layerCount = 0;
+
+ return floor;
+}
+
+TileLayer *SE_CreateTileLayer(TileFloor *floor) {
+ TileLayer *layer = malloc(sizeof(TileLayer));
+
+ layer->tileCount = 0;
+ layer->index = floor->layerCount;
+
+ floor->layers[floor->layerCount] = layer;
+ floor->layerCount++;
+
+ return layer;
+}
+
+void SE_UpdateTileFloor(EditorState *state, TileFloor *floor,
+ Camera2D *camera) {
+ // creating a new layer if it does not exists
+ TileLayer *layer;
+ if (floor->layerCount <= state->activeTileLayerId) {
+ layer = SE_CreateTileLayer(floor);
+ } else {
+ layer = floor->layers[state->activeTileLayerId];
+ }
+
+ // tile manipulation
+ Vector2 mousePos = GetScreenToWorld2D(GetMousePosition(), *camera);
+
+ for (int x = 0; x < floor->width; x++) {
+ for (int y = 0; y < floor->height; y++) {
+ Vector2 pos = (Vector2){x, y};
+
+ if (CheckCollisionPointRec(
+ mousePos, (Rectangle){pos.x * camera->zoom, pos.y * camera->zoom,
+ camera->zoom, camera->zoom})) {
+ // placing tiles
+ if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) &&
+ state->activeTilesetTile != NULL) {
+ Tile *tile = SE_FindTileAtPosition(layer, pos);
+
+ if (tile == NULL) {
+ tile = SE_CreateTile(layer, state->activeTilesetTile);
+ tile->position = pos;
+ }
+
+ tile->tilesetTile = state->activeTilesetTile;
+ }
+
+ // removing tiles
+ if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
+ Tile *tile = SE_FindTileAtPosition(layer, pos);
+ if (tile != NULL) {
+ SE_RemoveTile(layer, tile);
+ }
+ }
+ }
+ }
+ }
+}
+
+void SE_DrawTileFloor(TileFloor *floor, EditorState *state, Camera2D *camera) {
+ // draw placed tiles
+ for (int layerIndex = 0; layerIndex < floor->layerCount; layerIndex++) {
+ TileLayer *layer = floor->layers[layerIndex];
+
+ for (int tileIndex = 0; tileIndex < layer->tileCount; tileIndex++) {
+ Tile *tile = layer->tiles[tileIndex];
+
+ Rectangle s = {0, 0, TILE_WIDTH, TILE_HEIGHT};
+ Rectangle d = {tile->position.x * camera->zoom,
+ tile->position.y * camera->zoom, camera->zoom,
+ camera->zoom};
+
+ DrawTexturePro(tile->tilesetTile->texture, s, d, (Vector2){0, 0}, 0.0f,
+ WHITE);
+ }
+ }
+
+ // draw active tile
+ if (state->activeTilesetTile != NULL) {
+ Vector2 mousePos = GetScreenToWorld2D(GetMousePosition(), *camera);
+ Rectangle s = {0, 0, TILE_WIDTH, TILE_HEIGHT};
+ Rectangle d = {mousePos.x, mousePos.y, camera->zoom / 2.0f,
+ camera->zoom / 2.0f};
+ DrawTexturePro(state->activeTilesetTile->texture, s, d, (Vector2){0, 0},
+ 0.0f, WHITE);
+ }
+}
+
+void SE_UnloadTileFloor(TileFloor *floor) {
+ for (int layerIndex = 0; layerIndex < floor->layerCount; layerIndex++) {
+ TileLayer *layer = floor->layers[layerIndex];
+
+ for (int tileIndex = 0; tileIndex < layer->tileCount; tileIndex++) {
+ Tile *tile = layer->tiles[tileIndex];
+ free(tile);
+ }
+
+ free(layer);
+ }
+
+ free(floor);
+} \ No newline at end of file
diff --git a/src/floor.h b/src/floor.h
new file mode 100644
index 0000000..4f112fa
--- /dev/null
+++ b/src/floor.h
@@ -0,0 +1,37 @@
+#ifndef __FLOOR_H_
+#define __FLOOR_H_
+
+#include "editor.h"
+#include "raylib.h"
+#include "tileset.h"
+
+typedef struct {
+ int id;
+ Vector2 position;
+ TilesetTile *tilesetTile;
+} Tile;
+
+typedef struct {
+ int index;
+ int tileCount;
+ Tile *tiles[];
+} TileLayer;
+
+typedef struct {
+ int width, height;
+ int layerCount;
+ TileLayer *layers[];
+} TileFloor;
+
+Tile *SE_CreateTile(TileLayer *layer, TilesetTile *tilesetTile);
+void SE_RemoveTile(TileLayer *layer, Tile *tile);
+Tile *SE_FindTileAtPosition(TileLayer *layer, Vector2 position);
+
+TileLayer *SE_CreateTileLayer(TileFloor *floor);
+
+TileFloor *SE_CreateTileFloor(int width, int height);
+void SE_UpdateTileFloor(EditorState *state, TileFloor *floor, Camera2D *camera);
+void SE_DrawTileFloor(TileFloor *floor, EditorState *state, Camera2D *camera);
+void SE_UnloadTileFloor(TileFloor *floor);
+
+#endif \ No newline at end of file
diff --git a/src/main.c b/src/main.c
index dd59a11..6c0e533 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,9 +3,10 @@
#include <stdlib.h>
#include "editor.h"
+#include "floor.h"
#include "logger.h"
#include "raylib.h"
-#include "xd.h"
+#include "tileset.h"
int main() {
SetTraceLogCallback(SE_Logger);
@@ -15,29 +16,11 @@ int main() {
SetTargetFPS(60);
SetWindowMinSize(800, 600);
- // Getting list of files
- XdData* datas[] = {};
- FilePathList list = LoadDirectoryFilesEx("datas", ".xd", true);
+ Editor editor = {0};
+ editor.state.activeTileLayerId = 0;
- for (int i = 0; i < list.count; i++) {
- XdData data = Xd_LoadFromFile(list.paths[i]);
- printf("%s\n", data.name);
- datas[i] = &data;
- }
-
- UnloadDirectoryFiles(list);
-
- XdData* data = NULL;
- Editor editor = {data, {0, 0, 0, NULL, {0, 0, 0, 0}, {0, 0}, NULL, {}}};
-
- // creating a new xd data
- data = malloc(sizeof(XdData));
- editor.data = data;
- data->name = "xd";
- data->levels[0] = malloc(sizeof(XdLevel));
- data->levels[0]->floors[0] = malloc(sizeof(XdFloor));
- data->levels[0]->floors[0]->width = 30;
- data->levels[0]->floors[0]->height = 30;
+ Tileset* tileset = SE_CreateTileset();
+ TileFloor* floor = SE_CreateTileFloor(30, 30);
Camera2D camera = {0};
camera.target = (Vector2){0.0f, 0.0f};
@@ -47,6 +30,7 @@ int main() {
while (!WindowShouldClose()) {
SE_UpdateEditor(&editor);
+ SE_UpdateTileFloor(&editor.state, floor, &camera);
// interact with the map if the mouse is outside build tab
if (GetMousePosition().x < EDITOR_TOOLKIT_X) {
@@ -70,13 +54,27 @@ int main() {
ClearBackground(RAYWHITE);
BeginMode2D(camera);
- SE_DrawEditor(&editor, &camera);
+ SE_DrawTileFloor(floor, &editor.state, &camera);
+
+ // rendering grid
+ for (int x = 0; x < floor->width; x++) {
+ for (int y = 0; y < floor->height; y++) {
+ DrawRectangleLines(x * camera.zoom, y * camera.zoom,
+ TILE_WIDTH * camera.zoom, TILE_HEIGHT * camera.zoom,
+ BLACK);
+ }
+ }
+
EndMode2D();
- SE_DrawEditorToolkit(&editor);
+ SE_DrawEditor(&editor, tileset);
EndDrawing();
}
+ SE_UnloadTileFloor(floor);
+ SE_UnloadTileset(tileset);
+ CloseWindow();
+
return 0;
}
diff --git a/src/tileset.c b/src/tileset.c
new file mode 100644
index 0000000..d3cd9c8
--- /dev/null
+++ b/src/tileset.c
@@ -0,0 +1,38 @@
+#include "tileset.h"
+
+#include "stdlib.h"
+
+Tileset *SE_CreateTileset() {
+ Tileset *set = malloc(sizeof(Tileset) * 100);
+ set->tileCount = 0;
+ return set;
+}
+
+TilesetTile *SE_AddTilesetTile(Tileset *set, Texture2D texture,
+ TilesetTileType type) {
+ TilesetTile *tile =
+ malloc(sizeof(TilesetTile) + texture.width * texture.height);
+
+ tile->id = set->tileCount;
+ tile->texture = texture;
+ tile->type = type;
+
+ set->tileCount++;
+ set->tiles[tile->id] = tile;
+
+ return tile;
+}
+
+void SE_UnloadTilesetTile(TilesetTile *tile) {
+ UnloadTexture(tile->texture);
+ free(tile);
+}
+
+void SE_UnloadTileset(Tileset *set) {
+ for (int i = 0; i < set->tileCount; i++) {
+ TilesetTile *tile = set->tiles[i];
+ SE_UnloadTilesetTile(tile);
+ }
+
+ free(set);
+} \ No newline at end of file
diff --git a/src/tileset.h b/src/tileset.h
new file mode 100644
index 0000000..3f74808
--- /dev/null
+++ b/src/tileset.h
@@ -0,0 +1,28 @@
+#ifndef __TEXTURE_H__
+#define __TEXTURE_H__
+
+#include "raylib.h"
+
+#define TILE_WIDTH 16
+#define TILE_HEIGHT 16
+
+typedef enum { TILE_FLOOR = 0, TILE_WALL } TilesetTileType;
+
+typedef struct {
+ int id;
+ Texture2D texture;
+ TilesetTileType type;
+} TilesetTile;
+
+typedef struct {
+ int tileCount;
+ TilesetTile *tiles[];
+} Tileset;
+
+Tileset *SE_CreateTileset();
+TilesetTile *SE_AddTilesetTile(Tileset *set, Texture2D texture,
+ TilesetTileType type);
+void SE_UnloadTilesetTile(TilesetTile *tile);
+void SE_UnloadTileset(Tileset *set);
+
+#endif \ No newline at end of file
diff --git a/src/xd.c b/src/xd.c
deleted file mode 100644
index 32cc83e..0000000
--- a/src/xd.c
+++ /dev/null
@@ -1,35 +0,0 @@
-#include "xd.h"
-
-#include <raylib.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-XdData Xd_LoadFromFile(const char *filePath) {
- FILE *file = fopen(filePath, "rb");
-
- XdData data;
- fread(&data, sizeof(XdData), 1, file);
- printf("1 %s\n", data.name);
-
- fclose(file);
- return data;
-}
-
-void Xd_SaveFile(const char *filePath, XdData *data) {
- if (data == NULL) {
- printf("data is null\n");
- return;
- }
-
- FILE *file = fopen(filePath, "wb");
-
- if (file == NULL) {
- perror("Failed to open a file");
- return;
- }
-
- fwrite(data, sizeof(XdData), 1, file);
-
- fclose(file);
- printf("saved\n");
-}
diff --git a/src/xd.h b/src/xd.h
deleted file mode 100644
index 163c680..0000000
--- a/src/xd.h
+++ /dev/null
@@ -1,72 +0,0 @@
-#ifndef __XD_H__
-#define __XD_H__
-
-#define XD_VERSION 1
-#define XD_MAX_ENTITIES 256
-#define XD_MAX_TEXTURES 256
-
-typedef enum { PLAYER_START = 0, NEXT_FLOOR, PREVIOUS_FLOOR } XdEntityType;
-typedef enum { TILE_FLOOR = 0, TILE_WALL, TILE_CORNER } XdTileType;
-
-typedef struct {
- char *type;
- unsigned char *data;
- int width, height, dataSize;
-} XdTexture;
-
-typedef struct {
- XdTexture *texture;
- int id;
- XdTileType type;
-} XdTile;
-
-typedef struct {
- int id;
- XdTileType type;
-} XdTileData;
-
-typedef struct {
- int x, y;
- XdTexture *texture;
-} XdMapTile;
-
-typedef struct {
- int id;
- XdTexture *texture;
- XdEntityType type;
-} XdEntity;
-
-typedef struct {
- int x, y;
- XdEntity *entity;
-} XdMapEntity;
-
-typedef struct {
- int zIndex;
- XdMapEntity *entities[XD_MAX_ENTITIES];
- XdMapTile *tiles[];
-} XdMapLayer;
-
-typedef struct {
- int width, height;
- XdMapLayer *layers[];
-} XdFloor;
-
-typedef struct {
- int id;
- XdFloor *floors[];
-} XdLevel;
-
-typedef struct {
- char *name;
- int version;
- XdTexture *textures[XD_MAX_TEXTURES];
- XdTile *tiles[XD_MAX_TEXTURES];
- XdEntity *entities[XD_MAX_ENTITIES];
- XdLevel *levels[];
-} XdData;
-
-XdData Xd_LoadFromFile(const char *filePath);
-void Xd_SaveFile(const char *filePath, XdData *xd);
-
-#endif