comparison libmlk-core/core/animation.c @ 243:71b3b7036de7

misc: lot of cleanups, - prefix libraries with libmlk, - move assets from source directories closes #2520, - prefix header guards closes #2519
author David Demelier <markand@malikania.fr>
date Sat, 28 Nov 2020 22:37:30 +0100
parents libcore/core/animation.c@64f24b482722
children c4da052c0def
comparison
equal deleted inserted replaced
242:4c24604efcab 243:71b3b7036de7
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 #include <string.h>
21
22 #include "drawable.h"
23 #include "animation.h"
24 #include "sprite.h"
25
26 static bool
27 update(struct drawable *dw, unsigned int ticks)
28 {
29 return animation_update(dw->data, ticks);
30 }
31
32 static void
33 draw(struct drawable *dw)
34 {
35 animation_draw(dw->data, dw->x, dw->y);
36 }
37
38 void
39 animation_init(struct animation *an, struct sprite *sprite, unsigned int delay)
40 {
41 assert(an);
42 assert(sprite);
43
44 an->sprite = sprite;
45 an->row = 0;
46 an->column = 0;
47 an->delay = delay;
48 an->elapsed = 0;
49 }
50
51 void
52 animation_start(struct animation *an)
53 {
54 assert(an);
55
56 an->row = 0;
57 an->column = 0;
58 an->elapsed = 0;
59 }
60
61 bool
62 animation_completed(const struct animation *an)
63 {
64 assert(an);
65
66 return an->elapsed >= an->delay &&
67 an->row >= an->sprite->nrows &&
68 an->column >= an->sprite->ncols;
69 }
70
71 bool
72 animation_update(struct animation *an, unsigned int ticks)
73 {
74 assert(an);
75
76 an->elapsed += ticks;
77
78 if (an->elapsed < an->delay)
79 return false;
80
81 /* Increment column first */
82 if (++an->column >= an->sprite->ncols) {
83 /*
84 * Increment row, if we reach the last row it means we are
85 * at the last frame.
86 */
87 if (++an->row >= an->sprite->nrows)
88 an->row = an->sprite->nrows;
89 else
90 an->column = an->elapsed = 0;
91 } else
92 an->elapsed = 0;
93
94 return animation_completed(an);
95 }
96
97 bool
98 animation_draw(const struct animation *an, int x, int y)
99 {
100 assert(an);
101
102 return sprite_draw(an->sprite, an->row, an->column, x, y);
103 }
104
105 void
106 animation_drawable(struct animation *an, struct drawable *dw, int x, int y)
107 {
108 assert(an);
109 assert(dw);
110
111 memset(dw, 0, sizeof (*dw));
112
113 dw->data = an;
114 dw->x = x - (an->sprite->cellw / 2);
115 dw->y = y - (an->sprite->cellh / 2);
116 dw->update = update;
117 dw->draw = draw;
118
119 animation_start(an);
120 }