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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/*
* Copyright (C) 2021 P. J. McDermott
*
* This file is part of Maze Fight
*
* Maze Fight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Maze Fight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Maze Fight. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SDL.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "../char.h"
#include "char.h"
struct mf_char *
mf_char_new(size_t size)
{
struct mf_char *c;
c = calloc(1, size);
if (c == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't create character: %s",
strerror(errno));
return NULL;
}
return c;
}
void
mf_char_get_vector(struct mf_char *c, int *x, int *y, int *travel,
int *dx, int *dy)
{
*x = c->cur_x;
*y = c->cur_y;
*travel = c->travel;
switch (c->cur_dir) {
case MF_CHAR_DIR_U_: *dx = 0; *dy = -1; break;
case MF_CHAR_DIR_D_: *dx = 0; *dy = 1; break;
case MF_CHAR_DIR_L_: *dx = -1; *dy = 0; break;
case MF_CHAR_DIR_R_: *dx = 1; *dy = 0; break;
default: *dx = 0; *dy = 0; break;
}
}
int
mf_char_update(struct mf_char *c)
{
int dx;
int dy;
if (c->travel > 0) {
/* Currently moving */
c->travel += c->speed;
if (c->travel >= c->cell_width) {
/* Reached next cell */
c->cur_x = c->new_x;
c->cur_y = c->new_y;
c->travel -= c->cell_width;
if (c->step(c) < 0) {
return -1;
}
if (c->cur_dir == c->new_dir) {
/* Want to continue straight */
} else {
/* Want to stop or turn */
c->travel = 0;
}
} else {
/* Farther to go */
goto end;
}
}
if (c->new_dir != MF_CHAR_DIR_N_ && c->cur_dir != c->new_dir) {
/* Want to turn */
c->old_dir = c->cur_dir;
c->cur_dir = c->new_dir;
c->new_dir = MF_CHAR_DIR_N_;
c->turning = c->turn_time + 1;
}
if (c->turning > 0) {
/* Turning */
--c->turning;
}
if (c->turning == 0) {
/* Done turning */
if (c->new_dir != MF_CHAR_DIR_N_) {
/* Want to move */
switch (c->new_dir) {
case MF_CHAR_DIR_U_: dx = 0; dy = -1; break;
case MF_CHAR_DIR_D_: dx = 0; dy = 1; break;
case MF_CHAR_DIR_L_: dx = -1; dy = 0; break;
case MF_CHAR_DIR_R_: dx = 1; dy = 0; break;
default: dx = 0; dy = 0; break;
}
c->new_dir = MF_CHAR_DIR_N_;
if (mf_maze_is_wall(c->maze, c->cur_x, c->cur_y,
dx, dy)) {
goto end;
}
c->new_x = c->cur_x + dx;
c->new_y = c->cur_y + dy;
c->travel += c->speed;
} else {
if (c->turn(c) < 0) {
return -1;
}
}
}
end:
return c->update(c);
}
int
mf_char_render(struct mf_char *c, SDL_Renderer *renderer)
{
int e = 0;
int cx;
int cy;
double fx;
double ofx;
int r;
int x;
int y;
int i;
int oy;
int fr;
int hy;
int ox;
cx = c->cur_x * c->cell_width + c->cell_width / 2;
cy = c->cur_y * c->cell_width + c->cell_width / 2;
switch (c->cur_dir) {
case MF_CHAR_DIR_U_:
cy -= c->travel;
if (c->old_dir == MF_CHAR_DIR_L_) {
fx = -2.0;
} else {
fx = 2.0;
}
break;
case MF_CHAR_DIR_D_: cy += c->travel; fx = 0.0; break;
case MF_CHAR_DIR_L_: cx -= c->travel; fx = -1.0; break;
case MF_CHAR_DIR_R_: cx += c->travel; fx = 1.0; break;
default: fx = 0.0; break;
}
switch (c->old_dir) {
case MF_CHAR_DIR_U_:
if (c->cur_dir == MF_CHAR_DIR_L_) {
ofx = -2.0;
} else {
ofx = 2.0;
}
break;
case MF_CHAR_DIR_D_: ofx = 0.0; break;
case MF_CHAR_DIR_L_: ofx = -1.0; break;
case MF_CHAR_DIR_R_: ofx = 1.0; break;
default: ofx = 0.0; break;
}
r = c->cell_width / 2 - c->padding;
fx = ofx + (fx - ofx) * (c->turn_time - c->turning) / c->turn_time;
fx *= r;
/*
* Head
*/
if (SDL_SetRenderDrawColor(renderer,
c->head_color.r, c->head_color.g,
c->head_color.b, c->head_color.a) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't render character: %s",
SDL_GetError());
e = -1;
}
#define _mf_char_px(X, Y) \
do { \
if (SDL_RenderDrawPoint(renderer, cx + X, cy + Y) < 0) { \
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
"Couldn't render character: %s", \
SDL_GetError()); \
e = -1; \
} \
} while (0)
/* TODO: This is one pixel larger than it should be. */
for (x = 0, y = r; y > x; ++x) {
y = round(sqrt((r-0.5)*(r-0.5) - (x-0.5)*(x-0.5)) + 0.5);
for (i = 0; i <= y; ++i) _mf_char_px( x, i);
for (i = 0; i >= 0-y; --i) _mf_char_px( x, i);
for (i = 0; i <= y; ++i) _mf_char_px(0-x, i);
for (i = 0; i >= 0-y; --i) _mf_char_px(0-x, i);
for (i = 0; i <= y; ++i) _mf_char_px( i, x);
for (i = 0; i <= y; ++i) _mf_char_px( i, 0-x);
for (i = 0; i >= 0-y; --i) _mf_char_px( i, x);
for (i = 0; i >= 0-y; --i) _mf_char_px( i, 0-x);
}
#undef _mf_char_px
/*
* Smile
*/
if (SDL_SetRenderDrawColor(renderer,
c->smil_color.r, c->smil_color.g,
c->smil_color.b, c->smil_color.a) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't render character: %s",
SDL_GetError());
e = -1;
}
oy = r * c->smile_y;
fr = r * c->smile_r;
#define _mf_char_px(X, Y) \
do { \
hy = round(sqrt((r-0.5)*(r-0.5) - \
(X+fx-0.5)*(X+fx-0.5)) + 0.5); \
if (Y + oy < 0-hy || Y + oy > hy) continue; \
if (SDL_RenderDrawPoint(renderer, cx + fx + X, cy + oy + Y) \
< 0) { \
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
"Couldn't render character: %s", \
SDL_GetError()); \
e = -1; \
} \
} while (0)
for (x = 0, y = fr; y > x; ++x) {
y = round(sqrt((fr-0.5)*(fr-0.5) - (x-0.5)*(x-0.5)) + 0.5);
for (i = 0; i <= y; ++i) _mf_char_px( x, i);
for (i = 0; i <= y; ++i) _mf_char_px(0-x, i);
for (i = 0; i <= y; ++i) _mf_char_px( i, x);
for (i = 0; i >= 0-y; --i) _mf_char_px( i, x);
}
#undef _mf_char_px
/*
* Eyes
*/
if (SDL_SetRenderDrawColor(renderer,
c->eyes_color.r, c->eyes_color.g,
c->eyes_color.b, c->eyes_color.a) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't render character: %s",
SDL_GetError());
e = -1;
}
ox = r * c->eye_x;
oy = r * c->eye_y;
fr = r * c->eye_r;
#define _mf_char_px(X, Y) \
do { \
hy = round(sqrt((r-0.5)*(r-0.5) - \
(X+fx-ox-0.5)*(X+fx-ox-0.5)) + 0.5); \
if (Y - oy >= 0-hy && Y - oy <= hy) { \
if (SDL_RenderDrawPoint(renderer, \
cx + fx - ox + X, cy - oy + Y) \
< 0) { \
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
"Couldn't render character: %s"\
, SDL_GetError()); \
e = -1; \
} \
} \
hy = round(sqrt((r-0.5)*(r-0.5) - \
(X+fx+ox-0.5)*(X+fx+ox-0.5)) + 0.5); \
if (Y - oy >= 0-hy && Y - oy <= hy) { \
if (SDL_RenderDrawPoint(renderer, \
cx + fx + ox + X, cy - oy + Y) \
< 0) { \
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
"Couldn't render character: %s"\
, SDL_GetError()); \
e = -1; \
} \
} \
} while (0)
for (x = 0, y = fr; y > x; ++x) {
y = round(sqrt((fr-0.5)*(fr-0.5) - (x-0.5)*(x-0.5)) + 0.5);
for (i = 0; i <= y; ++i) _mf_char_px( x, i);
for (i = 0; i >= 0-y; --i) _mf_char_px( x, i);
for (i = 0; i <= y; ++i) _mf_char_px(0-x, i);
for (i = 0; i >= 0-y; --i) _mf_char_px(0-x, i);
for (i = 0; i <= y; ++i) _mf_char_px( i, x);
for (i = 0; i <= y; ++i) _mf_char_px( i, 0-x);
for (i = 0; i >= 0-y; --i) _mf_char_px( i, x);
for (i = 0; i >= 0-y; --i) _mf_char_px( i, 0-x);
}
#undef _mf_char_px
if (e < 0) {
return e;
}
return c->render(c, renderer);
}
void
mf_char_destroy(struct mf_char **c_p)
{
struct mf_char *c;
if (c_p == NULL || *c_p == NULL) {
return;
}
c = *c_p;
c->destroy(c);
free(c);
c = NULL;
}
|