-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr_entity.c
More file actions
283 lines (243 loc) · 5.7 KB
/
Copy pathr_entity.c
File metadata and controls
283 lines (243 loc) · 5.7 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
#include "renderer.h"
/*
* utilities
*
*/
static void make_centered_rect(SDL_Rect* r, double s)
{
r->w *= s;
r->h *= s;
r->y -= r->h/2;
r->x -= r->w/2;
}
static void make_isometric_rect(SDL_Rect* r, double s)
{
r->w *= s;
r->h *= s;
r->y -= r->h;
r->y += r->w/2;
r->x -= r->w/2;
}
static void make_isometric_rect_ex(SDL_Rect* r, double s, int x, int y)
{
r->w *= s;
r->h *= s;
r->y -= s*y;
r->x -= s*x;
}
/* FIXME: name */
static void r_scale_width(struct box* b)
{
double s = b->sz[0] / R.sprite.srcrect.w;
R.scale[0] *= s;
R.scale[1] *= s;
R.translate[0] += b->x[0];
R.translate[1] += b->x[1];
}
static void r_scale_fit(struct box* b)
{
R.scale[0] *= b->sz[0] / R.sprite.srcrect.w;
R.scale[1] *= b->sz[1] / R.sprite.srcrect.h;
R.translate[0] += b->x[0];
R.translate[1] += b->x[1];
}
static void r_bind_sprite(struct r_sprite* sprite)
{
R.sprite = *sprite;
}
static void r_bind_strip_frame(struct r_strip* s, unsigned long t)
{
int i;
i = (int) (0.001 * s->fps * t) % s->n_frames;
r_bind_sprite(s->frames + i);
}
static void r_render()
{
struct r_sprite s;
s = R.sprite;
s.dstrect.x *= R.scale[0];
s.dstrect.y *= R.scale[1];
s.dstrect.w *= R.scale[0];
s.dstrect.h *= R.scale[1];
s.dstrect.x += R.translate[0];
s.dstrect.y += R.translate[1];
s.flip ^= R.flip;
SDL_RenderCopyEx(R.renderer, s.texture, &s.srcrect, &s.dstrect,
s.angle, &s.center, s.flip);
}
/* FIXME: name */
static void r_render_width(struct box* b)
{
r_scale_width(b);
r_render();
}
static void r_render_tiled(struct box* area)
{
int i, j, m, n, w, h;
w = R.scale[0] * R.sprite.srcrect.w;
h = w;//R.scale[1] * R.sprite.srcrect.h;
m = area->sz[1] / w;
n = area->sz[0] / h;
v_axpy(1, area->B[0], R.translate);
R.translate[0] += w/2;
R.translate[1] += h/2;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
r_render();
R.translate[0] += w;
}
R.translate[0] -= n * w;
R.translate[1] += h;
}
}
static void r_warudo()
{
int ww, wh;
SDL_GetWindowSize(R.window, &ww, &wh);
R.flip = 0;
v_set(R.scale, 1, 1);
v_set(R.translate, ww/2, wh/2);
v_axpy(-1, g_player->b.x, R.translate);
}
/*
* entity drawing functions
*
*/
static void re_ventura_v(struct g_entity* e)
{
if (e->v_walk[1] < 0)
R.flip = SDL_FLIP_HORIZONTAL;
int i = 0; /* TODO */
r_bind_strip_frame(&RS.ventura.walk_front, SDL_GetTicks());
make_isometric_rect_ex(&R.sprite.dstrect, 1.5 * 1.5, 28, 120);
R.sprite.dstrect.y -= 8 * sin(i * (3.14/8));
}
static void re_ventura_h(struct g_entity* e)
{
if (0 < e->v_walk[0])
R.flip = SDL_FLIP_HORIZONTAL;
int i = 0; /* TODO */
r_bind_strip_frame(&RS.ventura.walk_side, SDL_GetTicks());
make_isometric_rect_ex(&R.sprite.dstrect, 1.5 * 2.5, 47, 118);
R.sprite.dstrect.y -= 8 * sin(i * (3.14/7));
}
static void re_ventura(struct g_entity* e)
{
r_warudo();
if (fabs(e->v_walk[0]) <= fabs(e->v_walk[1]))
re_ventura_v(e);
else
re_ventura_h(e);
r_render_width(&e->b);
}
static void re_bianca(struct g_entity* e)
{
r_warudo();
r_bind_strip_frame(&RS.bianca, SDL_GetTicks());
make_isometric_rect_ex(&R.sprite.dstrect, 4.2, 150, 320);
r_render_width(&e->b);
}
static void re_farisonha(struct g_entity* e)
{
r_warudo();
r_bind_sprite(&RS.farisonha);
make_isometric_rect_ex(&R.sprite.dstrect, 1.5, 32, 56);
r_render_width(&e->b);
}
static void re_bota(struct g_entity* e)
{
r_warudo();
SDL_SetTextureColorMod(RS.bota.texture,
255,
(SDL_GetTicks()/1 + 90) % 256,
(SDL_GetTicks()/1 + 180) % 256);
r_bind_sprite(&RS.bota);
make_isometric_rect_ex(&R.sprite.dstrect, 1, 191, 300);
r_render_width(&e->b);
SDL_SetTextureColorMod(RS.bota.texture, 255, 255, 255);
}
static void re_copa(struct g_entity* e)
{
r_warudo();
v_scal(2, R.scale);
SDL_SetTextureColorMod(RS.paredy.texture, 186, 166, 95);
r_bind_sprite(&RS.paredy);
make_isometric_rect(&R.sprite.dstrect, 1);
r_render_tiled(&e->b);
SDL_SetTextureColorMod(RS.paredy.texture, 255, 255, 255);
r_warudo();
v_scal(0.5, R.scale);
R.translate[1] -= 64;
r_bind_sprite(&RS.copa);
make_isometric_rect_ex(&R.sprite.dstrect, 1, 46, 209);
r_render_width(&e->b);
}
static void re_coluna(struct g_entity* e)
{
r_warudo();
v_scal(2, R.scale);
r_bind_sprite(&RS.paredy);
make_isometric_rect(&R.sprite.dstrect, 1);
r_render_tiled(&e->b);
r_warudo();
v_scal(2, R.scale);
r_bind_sprite(&RS.coluna);
make_isometric_rect(&R.sprite.dstrect, 1);
R.translate[1] -= 64;
r_render_tiled(&e->b);
}
static void re_paredy(struct g_entity* e)
{
r_warudo();
R.scale[0] = 2;
R.scale[1] = 2;
r_bind_sprite(&RS.paredy);
make_isometric_rect(&R.sprite.dstrect, 1);
r_render_tiled(&e->b);
}
static void re_xs2(struct g_entity* e)
{
r_warudo();
if (e->type == GE_XS2_SHINE)
SDL_SetTextureColorMod(RS.xs2.texture, 0, 255, 0);
r_bind_sprite(&RS.xs2);
make_centered_rect(&R.sprite.dstrect, 1);
r_render_tiled(&e->b);
SDL_SetTextureColorMod(RS.xs2.texture, 255, 255, 255);
}
static void re_chama(struct g_entity* e)
{
r_warudo();
r_bind_strip_frame(&RS.chama, SDL_GetTicks());
make_isometric_rect_ex(&R.sprite.dstrect, 1.5, 64, 222);
v_scal(64.0/RS.chama.frames[0].srcrect.w, R.scale);
r_render_tiled(&e->b);
}
static void re_igualop(struct g_entity* e)
{
r_warudo();
r_bind_sprite(&RS.igualop);
make_isometric_rect(&R.sprite.dstrect, 1);
r_scale_fit(&e->b);
r_render();
}
void r_entity(struct g_entity* e)
{
switch (e->type)
{
case GE_VENTURA: return re_ventura(e);
case GE_BIANCA: return re_bianca(e);
case GE_FARISONHA: return re_farisonha(e);
case GE_BOTA: return re_bota(e);
case GE_COPA: return re_copa(e);
case GE_COLUNA: return re_coluna(e);
case GE_PAREDY: return re_paredy(e);
case GE_XS2_SHINE: return re_xs2(e);
case GE_XS2: return re_xs2(e);
case GE_CHAMA: return re_chama(e);
case GE_NADA: return;
default: return re_igualop(e);
}
}