summaryrefslogtreecommitdiff
path: root/src/floor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/floor.c')
-rw-r--r--src/floor.c195
1 files changed, 0 insertions, 195 deletions
diff --git a/src/floor.c b/src/floor.c
deleted file mode 100644
index 91dcf86..0000000
--- a/src/floor.c
+++ /dev/null
@@ -1,195 +0,0 @@
-#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};
- tile->rotation = 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->rotation = state->tileRotation;
- 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};
-
- Vector2 o = {0, 0};
-
- if (tile->rotation == 90.0f) {
- o.y = d.height;
- } else if (tile->rotation == 180.0f) {
- o.x = d.width;
- o.y = d.height;
- } else if (tile->rotation == 270.0f) {
- o.x = d.width;
- }
-
- DrawTexturePro(tile->tilesetTile->texture, s, d, o, tile->rotation,
- 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};
-
- Vector2 o = {d.width / 2.0f, d.height / 2.0f};
-
- DrawTexturePro(state->activeTilesetTile->texture, s, d, o,
- state->tileRotation, 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