Mercurial > molko
comparison src/libmlk-core/core/drawable.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/drawable.c@d01e83210ca2 |
children | 15bdac29ba4b |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * drawable.c -- automatic drawable objects | |
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 <stddef.h> | |
21 #include <stdlib.h> | |
22 #include <string.h> | |
23 | |
24 #include "animation.h" | |
25 #include "drawable.h" | |
26 #include "util.h" | |
27 #include "sprite.h" | |
28 | |
29 #define DRAWABLE_FOREACH(st, iter) \ | |
30 for (size_t i = 0; i < DRAWABLE_STACK_MAX && ((iter) = (st)->objects[i], 1); ++i) | |
31 | |
32 int | |
33 drawable_update(struct drawable *dw, unsigned int ticks) | |
34 { | |
35 assert(dw); | |
36 | |
37 return dw->update(dw, ticks); | |
38 } | |
39 | |
40 void | |
41 drawable_draw(struct drawable *dw) | |
42 { | |
43 assert(dw); | |
44 | |
45 dw->draw(dw); | |
46 } | |
47 | |
48 void | |
49 drawable_end(struct drawable *dw) | |
50 { | |
51 assert(dw); | |
52 | |
53 if (dw->end) | |
54 dw->end(dw); | |
55 } | |
56 | |
57 void | |
58 drawable_finish(struct drawable *dw) | |
59 { | |
60 assert(dw); | |
61 | |
62 if (dw->finish) | |
63 dw->finish(dw); | |
64 } | |
65 | |
66 void | |
67 drawable_stack_init(struct drawable_stack *st) | |
68 { | |
69 assert(st); | |
70 | |
71 memset(st, 0, sizeof (*st)); | |
72 } | |
73 | |
74 int | |
75 drawable_stack_add(struct drawable_stack *st, struct drawable *dw) | |
76 { | |
77 assert(st); | |
78 assert(dw); | |
79 | |
80 for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) { | |
81 if (!st->objects[i]) { | |
82 st->objects[i] = dw; | |
83 return 0; | |
84 } | |
85 } | |
86 | |
87 return -1; | |
88 } | |
89 | |
90 int | |
91 drawable_stack_update(struct drawable_stack *st, unsigned int ticks) | |
92 { | |
93 assert(st); | |
94 | |
95 for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) { | |
96 struct drawable *dw = st->objects[i]; | |
97 | |
98 if (dw && drawable_update(dw, ticks)) { | |
99 drawable_end(dw); | |
100 drawable_finish(dw); | |
101 st->objects[i] = NULL; | |
102 } | |
103 } | |
104 | |
105 /* | |
106 * We process the array again in case a drawable added a new drawable | |
107 * within the update function. | |
108 */ | |
109 return drawable_stack_completed(st); | |
110 } | |
111 | |
112 void | |
113 drawable_stack_draw(struct drawable_stack *st) | |
114 { | |
115 assert(st); | |
116 | |
117 struct drawable *dw; | |
118 | |
119 DRAWABLE_FOREACH(st, dw) | |
120 if (dw) | |
121 drawable_draw(dw); | |
122 } | |
123 | |
124 int | |
125 drawable_stack_completed(const struct drawable_stack *st) | |
126 { | |
127 assert(st); | |
128 | |
129 struct drawable *dw; | |
130 | |
131 DRAWABLE_FOREACH(st, dw) | |
132 if (dw) | |
133 return 0; | |
134 | |
135 return 1; | |
136 } | |
137 | |
138 void | |
139 drawable_stack_finish(struct drawable_stack *st) | |
140 { | |
141 assert(st); | |
142 | |
143 struct drawable *dw; | |
144 | |
145 DRAWABLE_FOREACH(st, dw) { | |
146 if (dw) { | |
147 drawable_end(dw); | |
148 drawable_finish(dw); | |
149 } | |
150 } | |
151 | |
152 memset(st, 0, sizeof (*st)); | |
153 } |