comparison libcore/core/animation.c @ 121:789b23e01f52

misc: reorganize hierarchy, closes #2490
author David Demelier <markand@malikania.fr>
date Mon, 05 Oct 2020 13:25:06 +0200
parents src/core/animation.c@52792b863ff7
children 6691c9f69028
comparison
equal deleted inserted replaced
120:b3429b26d60d 121:789b23e01f52
1 /*
2 * animation.c -- basic animations
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20
21 #include "animation.h"
22 #include "sprite.h"
23
24 void
25 animation_init(struct animation *an, struct sprite *sprite, unsigned int delay)
26 {
27 assert(an);
28 assert(sprite);
29
30 an->sprite = sprite;
31 an->row = 0;
32 an->column = 0;
33 an->delay = delay;
34 an->elapsed = 0;
35 }
36
37 bool
38 animation_is_complete(const struct animation *an)
39 {
40 assert(an);
41
42 return an->row == an->sprite->nrows &&
43 an->column == an->sprite->ncols &&
44 an->elapsed >= an->delay;
45 }
46
47 void
48 animation_start(struct animation *an)
49 {
50 assert(an);
51
52 an->row = 0;
53 an->column = 0;
54 an->elapsed = 0;
55 }
56
57 void
58 animation_update(struct animation *an, unsigned int ticks)
59 {
60 assert(an);
61
62 an->elapsed += ticks;
63
64 if (an->elapsed < an->delay)
65 return;
66
67 /* Increment column first */
68 if (++an->column >= an->sprite->ncols) {
69 /*
70 * Increment row, if we reach the last row it means we are
71 * at the last frame.
72 */
73 if (++an->row >= an->sprite->nrows)
74 an->row = an->sprite->nrows;
75 else
76 an->column = 0;
77 }
78 }
79
80 void
81 animation_draw(struct animation *an, int x, int y)
82 {
83 sprite_draw(an->sprite, an->row, an->column, x, y);
84 }