1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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);
}
|