-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilemap.c
102 lines (82 loc) · 1.95 KB
/
tilemap.c
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
#include "tilemap.h"
#include <stdlib.h>
struct tile
{
tilemap_t parent;
int x;
int y;
int id;
};
struct tilemap
{
struct tile* tiles;
int width;
int height;
};
tilemap_t tilemap_create(int width, int height)
{
struct tilemap* tilemap = malloc(sizeof(struct tilemap));
tilemap->tiles = malloc(width * height * sizeof(struct tile));
tilemap->width = width;
tilemap->height = height;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
tilemap->tiles[y*width + x].parent = tilemap;
tilemap->tiles[y*width + x].x = x;
tilemap->tiles[y*width + x].y = y;
tilemap->tiles[y*width + x].id = -1;
}
return tilemap;
}
void tilemap_destroy(tilemap_t tilemap)
{
free(tilemap->tiles);
free(tilemap);
}
void tilemap_get_dimensions(tilemap_t tilemap, int* width, int* height)
{
*width = tilemap->width;
*height = tilemap->height;
}
void tilemap_add_child(tilemap_t tilemap, int x, int y, tilemap_t child)
{
int child_width, child_height;
tilemap_get_dimensions(child, &child_width, &child_height);
int ex = x + child_width;
int ey = y + child_height;
for (int cy = y; cy >= 0 && cy < ey; cy++)
{
for (int cx = x; cx >= 0 && cx < ex; cx++)
{
int child_x = cx - x;
int child_y = cy - y;
if (tilemap_get_id_at(child, child_x, child_y) != -1)
{
struct tile* tile = &tilemap->tiles[tilemap->width*cy + cx];
tile->parent = child;
tile->x = child_x;
tile->y = child_y;
tile->id = -1;
}
}
}
}
void tilemap_set_id_at(tilemap_t tilemap, int x, int y, int id)
{
if (x < 0 || x >= tilemap->width || y < 0 || y >= tilemap->height)
return;
tilemap->tiles[tilemap->width*y + x].id = id;
}
int tilemap_get_id_at(tilemap_t tilemap, int x, int y)
{
if (x < 0 || x >= tilemap->width || y < 0 || y >= tilemap->height)
return -1;
struct tile* tile = &tilemap->tiles[tilemap->width*y + x];
if (tile->parent == tilemap)
return tile->id;
else
{
return tilemap_get_id_at(tile->parent, tile->x, tile->y);
}
}