-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanimation.c
More file actions
361 lines (340 loc) · 11.6 KB
/
animation.c
File metadata and controls
361 lines (340 loc) · 11.6 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2024-2025 Viacheslav Chimishuk <vchimishuk@yandex.ru>
//
// This file is part of loderunner-ng.
//
// loderunner-ng 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.
//
// loderunner-ng 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 loderunner-ng. If not, see <http://www.gnu.org/licenses/>.
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "texture.h"
#include "animation.h"
#include "exit.h"
#include "xmalloc.h"
static struct sprite *animation_sprite_init(enum texture tx, int x, int y,
int w, int h, int frames)
{
struct sprite *s = xmalloc(sizeof(struct sprite));
s->texture = texture_get(tx);
s->x = x;
s->y = y;
s->w = w;
s->h = h;
s->frames = frames;
return s;
}
static struct sprite **sprites_init(int size)
{
return xmalloc(sizeof(struct sprite) * size);
}
static struct sprite *runner_sprite_init(int n, int frames)
{
int r = n / 9;
int c = n % 9;
return animation_sprite_init(TEXTURE_RUNNER,
c * TILE_MAP_WIDTH, r * TILE_MAP_HEIGHT,
TILE_MAP_WIDTH, TILE_MAP_HEIGHT,
frames);
}
static struct sprite *guard_sprite_init(int n, int frames)
{
int r = n / 11;
int c = n % 11;
return animation_sprite_init(TEXTURE_GUARD,
c * TILE_MAP_WIDTH, r * TILE_MAP_HEIGHT,
TILE_MAP_WIDTH, TILE_MAP_HEIGHT,
frames);
}
static struct sprite *hole_sprite_init(int n, bool tall, int frames)
{
int r = n / 9;
int c = n % 9;
int t = tall ? 2 : 1;
return animation_sprite_init(TEXTURE_HOLE,
c * TILE_MAP_WIDTH, r * TILE_MAP_HEIGHT * t,
TILE_MAP_WIDTH, TILE_MAP_HEIGHT * t,
frames);
}
struct animation *animation_init(enum animation_t t)
{
struct animation *a = xmalloc(sizeof(struct animation));
switch (t) {
case ANIMATION_BRICK:
a->sprites = sprites_init(2);
a->sprites[0] = animation_sprite_init(TEXTURE_BRICK, 0, 0,
TILE_MAP_WIDTH, TILE_MAP_HEIGHT, 1);
a->sprites[1] = NULL;
break;
case ANIMATION_GOLD:
a->sprites = sprites_init(2);
a->sprites[0] = animation_sprite_init(TEXTURE_GOLD, 0, 0,
TILE_MAP_WIDTH, TILE_MAP_HEIGHT, 1);
a->sprites[1] = NULL;
break;
case ANIMATION_GROUND:
a->sprites = sprites_init(2);
a->sprites[0] = animation_sprite_init(TEXTURE_GROUND, 0, 0,
TILE_GROUND_WIDTH, TILE_GROUND_HEIGHT, 1);
a->sprites[1] = NULL;
break;
case ANIMATION_GUARD_CLIMB_LEFT:
a->sprites = sprites_init(4);
a->sprites[0] = guard_sprite_init(25, 1);
a->sprites[1] = guard_sprite_init(26, 2);
a->sprites[2] = guard_sprite_init(27, 2);
a->sprites[3] = NULL;
break;
case ANIMATION_GUARD_CLIMB_RIGHT:
a->sprites = sprites_init(4);
a->sprites[0] = guard_sprite_init(22, 1);
a->sprites[1] = guard_sprite_init(23, 2);
a->sprites[2] = guard_sprite_init(24, 2);
a->sprites[3] = NULL;
break;
case ANIMATION_GUARD_FALL_LEFT:
a->sprites = sprites_init(2);
a->sprites[0] = guard_sprite_init(30, 1);
a->sprites[1] = NULL;
break;
case ANIMATION_GUARD_FALL_RIGHT:
a->sprites = sprites_init(2);
a->sprites[0] = guard_sprite_init(8, 1);
a->sprites[1] = NULL;
break;
case ANIMATION_GUARD_LEFT:
a->sprites = sprites_init(4);
a->sprites[0] = guard_sprite_init(3, 2);
a->sprites[1] = guard_sprite_init(4, 2);
a->sprites[2] = guard_sprite_init(5, 2);
a->sprites[3] = NULL;
break;
case ANIMATION_GUARD_REBORN:
a->sprites = sprites_init(3);
a->sprites[0] = guard_sprite_init(28, 6);
a->sprites[1] = guard_sprite_init(29, 2);
a->sprites[2] = NULL;
break;
case ANIMATION_GUARD_RIGHT:
a->sprites = sprites_init(4);
a->sprites[0] = guard_sprite_init(0, 2);
a->sprites[1] = guard_sprite_init(1, 2);
a->sprites[2] = guard_sprite_init(2, 2);
a->sprites[3] = NULL;
break;
case ANIMATION_GUARD_TRAP_LEFT:
a->sprites = sprites_init(7);
// LodeRunner TotalRecall uses totally 18 frames with 0.3 speed.
// In this case we should have 60 frames in total instead of original
// 18 (18 * 3.33333) but it looks too long comparing to TotalRecal.
// So, I simply tune number of frames here to make it have the same
// speed.
a->sprites[0] = guard_sprite_init(30, 37);
a->sprites[1] = guard_sprite_init(31, 3);
a->sprites[2] = guard_sprite_init(32, 3);
a->sprites[3] = guard_sprite_init(31, 3);
a->sprites[4] = guard_sprite_init(32, 3);
a->sprites[5] = guard_sprite_init(30, 3);
a->sprites[6] = NULL;
break;
case ANIMATION_GUARD_TRAP_RIGHT:
a->sprites = sprites_init(7);
a->sprites[0] = guard_sprite_init(8, 37);
a->sprites[1] = guard_sprite_init(9, 3);
a->sprites[2] = guard_sprite_init(10, 3);
a->sprites[3] = guard_sprite_init(9, 3);
a->sprites[4] = guard_sprite_init(10, 3);
a->sprites[5] = guard_sprite_init(8, 3);
a->sprites[6] = NULL;
break;
case ANIMATION_GUARD_UPDOWN:
a->sprites = sprites_init(3);
a->sprites[0] = guard_sprite_init(6, 1);
a->sprites[1] = guard_sprite_init(7, 1);
a->sprites[2] = NULL;
break;
case ANIMATION_HOLE_FILL:
a->sprites = sprites_init(5);
a->sprites[0] = hole_sprite_init(26, false, 166);
a->sprites[1] = hole_sprite_init(17, false, 8);
a->sprites[2] = hole_sprite_init(8, false, 8);
a->sprites[3] = hole_sprite_init(35, false, 4);
a->sprites[4] = NULL;
break;
case ANIMATION_LADDER:
a->sprites = sprites_init(2);
a->sprites[0] = animation_sprite_init(TEXTURE_LADDER, 0, 0,
TILE_MAP_WIDTH, TILE_MAP_HEIGHT, 1);
a->sprites[1] = NULL;
break;
case ANIMATION_ROPE:
a->sprites = sprites_init(2);
a->sprites[0] = animation_sprite_init(TEXTURE_ROPE, 0, 0,
TILE_MAP_WIDTH, TILE_MAP_HEIGHT, 1);
a->sprites[1] = NULL;
break;
case ANIMATION_RUNNER_CLIMB_LEFT:
a->sprites = sprites_init(4);
a->sprites[0] = runner_sprite_init(21, 1);
a->sprites[1] = runner_sprite_init(22, 2);
a->sprites[2] = runner_sprite_init(23, 2);
a->sprites[3] = NULL;
break;
case ANIMATION_RUNNER_CLIMB_RIGHT:
a->sprites = sprites_init(4);
a->sprites[0] = runner_sprite_init(18, 1);
a->sprites[1] = runner_sprite_init(19, 2);
a->sprites[2] = runner_sprite_init(20, 2);
a->sprites[3] = NULL;
break;
case ANIMATION_RUNNER_DIG_LEFT:
// Repeat dig animation as many sprites as hole digging animation
// lasts so they have the same duration.
a->sprites = sprites_init(2);
a->sprites[0] = runner_sprite_init(25, 11);
a->sprites[1] = NULL;
break;
case ANIMATION_RUNNER_DIG_RIGHT:
// Repeat dig animation as many sprites as hole digging animation
// lasts so they have the same duration.
a->sprites = sprites_init(2);
a->sprites[0] = runner_sprite_init(24, 11);
a->sprites[1] = NULL;
break;
case ANIMATION_RUNNER_FALL_LEFT:
a->sprites = sprites_init(2);
a->sprites[0] = runner_sprite_init(26, 1);
a->sprites[1] = NULL;
break;
case ANIMATION_RUNNER_FALL_RIGHT:
a->sprites = sprites_init(2);
a->sprites[0] = runner_sprite_init(8, 1);
a->sprites[1] = NULL;
break;
case ANIMATION_RUNNER_HOLE_LEFT:
a->sprites = sprites_init(9);
a->sprites[0] = hole_sprite_init(0, true, 1);
a->sprites[1] = hole_sprite_init(1, true, 1);
a->sprites[2] = hole_sprite_init(2, true, 2);
a->sprites[3] = hole_sprite_init(3, true, 1);
a->sprites[4] = hole_sprite_init(4, true, 2);
a->sprites[5] = hole_sprite_init(5, true, 1);
a->sprites[6] = hole_sprite_init(6, true, 2);
a->sprites[7] = hole_sprite_init(7, true, 1);
a->sprites[8] = NULL;
break;
case ANIMATION_RUNNER_HOLE_RIGHT:
a->sprites = sprites_init(9);
a->sprites[0] = hole_sprite_init(9, true, 1);
a->sprites[1] = hole_sprite_init(10, true, 1);
a->sprites[2] = hole_sprite_init(11, true, 2);
a->sprites[3] = hole_sprite_init(12, true, 1);
a->sprites[4] = hole_sprite_init(13, true, 2);
a->sprites[5] = hole_sprite_init(14, true, 1);
a->sprites[6] = hole_sprite_init(15, true, 2);
a->sprites[7] = hole_sprite_init(16, true, 1);
a->sprites[8] = NULL;
break;
case ANIMATION_RUNNER_LEFT:
a->sprites = sprites_init(4);
a->sprites[0] = runner_sprite_init(3, 1);
a->sprites[1] = runner_sprite_init(4, 1);
a->sprites[2] = runner_sprite_init(5, 1);
a->sprites[3] = NULL;
break;
case ANIMATION_RUNNER_RIGHT:
a->sprites = sprites_init(4);
a->sprites[0] = runner_sprite_init(0, 1);
a->sprites[1] = runner_sprite_init(1, 1);
a->sprites[2] = runner_sprite_init(2, 1);
a->sprites[3] = NULL;
break;
case ANIMATION_RUNNER_UPDOWN:
a->sprites = sprites_init(3);
a->sprites[0] = runner_sprite_init(6, 1);
a->sprites[1] = runner_sprite_init(7, 1);
a->sprites[2] = NULL;
break;
case ANIMATION_SOLID:
a->sprites = sprites_init(2);
a->sprites[0] = animation_sprite_init(TEXTURE_SOLID, 0, 0,
TILE_MAP_WIDTH, TILE_MAP_HEIGHT, 1);
a->sprites[1] = NULL;
break;
default:
die("illegal state");
}
animation_reset(a);
return a;
}
void animation_destroy(struct animation *a)
{
for (struct sprite **s = a->sprites; *s != NULL; s++) {
free(*s);
}
free(a->sprites);
free(a);
}
/*
* Switch to the next animation sprite.
* Returns true it end of the animation has been reached and moved back
* to the first sprite.
*/
bool animation_tick(struct animation *a)
{
bool replay = false;
if (a->frame) {
// Keep showing the same sprite for the number of frames defined by
// sprite's `frames` field.
a->frame--;
} else {
// Move to the next sprite.
a->cur++;
if (*(a->cur) == NULL) {
replay = true;
animation_reset(a);
} else {
a->frame = (*(a->cur))->frames;
}
}
return replay;
}
/*
* Reset animation to the first sprite.
*/
void animation_reset(struct animation *a)
{
a->cur = a->sprites;
a->frame = (*(a->cur))->frames;
}
/*
* Render current sprite of the animation.
*/
void animation_render(SDL_Renderer *renderer, struct animation *a, int x, int y)
{
animation_render_sprite(renderer, *(a->cur), x, y);
}
void animation_render_sprite(SDL_Renderer *renderer, struct sprite *s, int x, int y)
{
SDL_Rect src;
src.x = s->x;
src.y = s->y;
src.w = s->w;
src.h = s->h;
SDL_Rect dst;
dst.x = x;
dst.y = y;
dst.w = s->w;
dst.h = s->h;
if (SDL_RenderCopy(renderer, s->texture, &src, &dst) < 0) {
die_sdl("SDL_RenderCopy");
}
}