Mercurial > molko
comparison src/libmlk-core/core/animation.c @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | libmlk-core/core/animation.c@d01e83210ca2 |
children | 460c78706989 |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * animation.c -- basic animations | |
3 * | |
4 * Copyright (c) 2020-2021 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 int | |
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, const 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 int | |
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 int | |
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 0; | |
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 int | |
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 } |