-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.c
More file actions
285 lines (244 loc) · 5.94 KB
/
objects.c
File metadata and controls
285 lines (244 loc) · 5.94 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <math.h>
#include <stdlib.h>
#include "map.h"
#include "objects.h"
#include "screen.h"
#include "utils.h"
Vec2i get_center(Rect rect)
{
Vec2i center;
center.x = rect.tl.x + (rect.br.x - rect.tl.x) / 2;
center.y = rect.tl.y + (rect.br.y - rect.tl.y) / 2;
return center;
}
Vec2f vec2i_to_f(Vec2i a)
{
Vec2f r = {a.x, a.y};
return r;
}
Vec2i vec2f_to_i(Vec2f a)
{
Vec2i r = {a.x, a.y};
return r;
}
Vec2f vec2f_add(Vec2f a, Vec2f b)
{
Vec2f r = {a.x + b.x, a.y + b.y};
return r;
}
Vec2f vec2f_sub(Vec2f a, Vec2f b)
{
Vec2f r = {a.x - b.x, a.y - b.y};
return r;
}
Vec2f vec2f_div_const(Vec2f a, int b)
{
Vec2f r = {a.x / b, a.y / b};
return r;
}
Vec2i vec2i_add(Vec2i a, Vec2i b)
{
Vec2i r = {a.x + b.x, a.y + b.y};
return r;
}
Vec2i vec2i_mul(Vec2i a, Vec2i b)
{
Vec2i r = {a.x * b.x, a.y * b.y};
return r;
}
Vec2i vec2i_mul_const(Vec2i a, int b)
{
Vec2i r = {a.x * b, a.y * b};
return r;
}
Vec2f vec2f_mul(Vec2f a, Vec2f b)
{
Vec2f r = {a.x * b.x, a.y * b.y};
return r;
}
Vec2f vec2f_mul_const(Vec2f a, float b)
{
Vec2f r = {a.x * b, a.y * b};
return r;
}
Vec2i vec2i_add_const(Vec2i a, int b)
{
Vec2i r = {a.x + b, a.y + b};
return r;
}
Vec2i vec2i_sub(Vec2i a, Vec2i b)
{
Vec2i r = {a.x - b.x, a.y - b.y};
return r;
}
Vec2i vec2i_div_const(Vec2i a, int b)
{
Vec2i r = {a.x / b, a.y / b};
return r;
}
Vec2i vec2i_sub_const(Vec2i a, int b)
{
Vec2i r = {a.x - b, a.y - b};
return r;
}
int vec2i_dot(Vec2i a, Vec2i b)
{
return (float)a.x * b.x + a.y * b.y;
}
float vec2f_dot(Vec2f a, Vec2f b)
{
return (float)a.x * b.x + a.y * b.y;
}
float vec2i_sqrdistance(Vec2i a)
{
return vec2i_dot(a, a);
}
float vec2f_sqrdistance(Vec2f a, Vec2f b)
{
return vec2f_dot(vec2f_sub(a, b), vec2f_sub(a, b));
}
float vec2f_distance(Vec2f a, Vec2f b)
{
return sqrtf(vec2f_sqrdistance(a, b));
}
Vec2f vec2f_normalize(Vec2f a)
{
float len = vec2f_distance(a, (Vec2f){0, 0});
Vec2f r = {a.x / len, a.y / len};
return r;
}
Vec2i rect_size(Rect rect)
{
Vec2i r = {rect.br.x - rect.tl.x + 1, rect.br.y - rect.tl.y + 1};
return r;
}
Vec2f rect_float_size(RectFloat rect)
{
Vec2f r = {rect.br.x - rect.tl.x + 1, rect.br.y - rect.tl.y + 1};
return r;
}
Vec2f rect_center(Rect rect)
{
Vec2f r = vec2f_add(vec2f_div_const(vec2i_to_f(rect_size(rect)), 2), vec2i_to_f(rect.tl));
return r;
}
Vec2f rect_float_center(RectFloat rect)
{
Vec2f r = vec2f_add(vec2f_div_const(rect_float_size(rect), 2), rect.tl);
return r;
}
Rect rect_translate(Rect rect, Vec2i trans)
{
Rect result;
result.color = rect.color;
result.tl = vec2i_add(rect.tl, trans);
result.br = vec2i_add(rect.br, trans);
return result;
}
RectFloat rect_float_translate(RectFloat rect, Vec2f trans)
{
RectFloat result;
result.color = rect.color;
result.tl = vec2f_add(rect.tl, trans);
result.br = vec2f_add(rect.br, trans);
return result;
}
Rect rect_float_to_rect(RectFloat rect)
{
Rect result;
result.color = rect.color;
result.tl = vec2f_to_i(rect.tl);
result.br = vec2f_to_i(rect.br);
return result;
}
// -------------------
// | | |
// | 0 | 1 |
// | | |
// -------------------
// | | |
// | 2 | 3 |
// | | |
// -------------------
Rect subdivide_rect(Rect container, int div, int index)
{
Rect result;
Vec2i size = rect_size(container);
result.tl.x = size.x / div * (index % div);
result.tl.y = size.y / div * (index / div);
result.br.x = size.x / div * (((index) % (div)) + 1);
result.br.y = size.y / div * ((index + div) / (div));
return rect_translate(result, container.tl);
}
Rect gen_random_subrect(Rect container)
{
Vec2i size = rect_size(container);
Rect result;
int random_width = random_between(size.x / 6, size.x / 2);
int random_height = random_between(size.y / 6, size.y / 2);
int random_x = random_between(0, size.x - random_width);
int random_y = random_between(0, size.y - random_height);
result.tl.x = container.tl.x + random_x;
result.tl.y = container.tl.y + random_y;
result.br.x = container.tl.x + random_x + random_width;
result.br.y = container.tl.y + random_y + random_height;
return result;
}
Rect gen_subrect_with_size(Vec2i size, Rect container)
{
Rect result;
int random_x = random_between(container.tl.x, container.br.x - size.x - 1);
int random_y = random_between(container.tl.y, container.br.y - size.y - 1);
result.tl.x = random_x;
result.tl.y = random_y;
result.br.x = random_x + size.x;
result.br.y = random_y + size.y;
return result;
}
Rect expand_rect(Rect rect, int amount)
{
Rect result;
result.br.y = rect.br.y + amount;
result.br.x = rect.br.x + amount;
result.tl.x = rect.tl.x - amount;
result.tl.y = rect.tl.y - amount;
return result;
}
void zero_bitmap(Bitmap bitmap)
{
for (int i = 0; i < bitmap.width * bitmap.height; i++)
{
bitmap.data[i] = 0;
}
}
Bitmap alloc_bitmap(int width, int height)
{
int *data = (int *)malloc(width * height * sizeof(int));
Bitmap result = {data, {{width, height}}};
zero_bitmap(result);
return result;
}
void free_bitmap(Bitmap bitmap)
{
free(bitmap.data);
}
void set_bitmap_value(Bitmap bitmap, Vec2i pos, int value)
{
bitmap.data[pos.y * bitmap.width + pos.x] = value;
}
int get_bitmap_value(Bitmap bitmap, Vec2i pos)
{
if (pos.x < 0 || pos.x >= bitmap.width || pos.y < 0 || pos.y >= bitmap.height)
{
return 0;
}
return bitmap.data[pos.y * bitmap.width + pos.x];
}
int get_rect_distance(Bitmap distance, Rect rect)
{
int tl = get_bitmap_value(distance, rect.tl);
int tr = get_bitmap_value(distance, (Vec2i){rect.br.x, rect.tl.y});
int bl = get_bitmap_value(distance, (Vec2i){rect.tl.x, rect.br.y});
int br = get_bitmap_value(distance, rect.br);
return MIN(MIN(tl, tr), MIN(bl, br));
}