Mercurial > molko
comparison examples/example-action.c @ 136:30b68089ae70
core: rework actions and a bit of drawables, closes #2492
In the effort of having as less as possible memory allocation in libcore, the
usage of actions and drawable no longer copy the original source parameter to
let user pass a heap allocated variable or a static storage one.
Update predefined drawable and actions to match these new needs.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 13 Oct 2020 09:38:44 +0200 |
parents | |
children | 8b035f7f978a |
comparison
equal
deleted
inserted
replaced
135:eadfed7674ac | 136:30b68089ae70 |
---|---|
1 /* | |
2 * example-action.c -- example on how to use automatic drawables | |
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 <core/action.h> | |
22 #include <core/clock.h> | |
23 #include <core/event.h> | |
24 #include <core/image.h> | |
25 #include <core/maths.h> | |
26 #include <core/message.h> | |
27 #include <core/painter.h> | |
28 #include <core/panic.h> | |
29 #include <core/script.h> | |
30 #include <core/sprite.h> | |
31 #include <core/sys.h> | |
32 #include <core/texture.h> | |
33 #include <core/theme.h> | |
34 #include <core/util.h> | |
35 #include <core/window.h> | |
36 | |
37 #include <assets/sprites/chest.h> | |
38 #include <assets/sprites/people.h> | |
39 | |
40 #define W 1280 | |
41 #define H 720 | |
42 | |
43 static struct action_stack stack; | |
44 | |
45 /* | |
46 * Event object for the chest to click on. | |
47 */ | |
48 static struct { | |
49 struct message msg; | |
50 struct action msg_act; | |
51 int x; | |
52 int y; | |
53 bool opened; | |
54 struct texture image; | |
55 struct sprite sprite; | |
56 struct action event; | |
57 } chest = { | |
58 .msg = { | |
59 .text = { | |
60 "100000 pièces.", | |
61 "Te voilà riche sale file de crevette." | |
62 } | |
63 } | |
64 }; | |
65 | |
66 /* | |
67 * Event object for the guide who ask dialog with you. | |
68 * | |
69 * It is a script spawned upon click like this. | |
70 * | |
71 * [guide.event] | |
72 * | spawn | |
73 * (on click)------------------->[message 1] | |
74 * v | |
75 * [message 2] | |
76 * v | |
77 * [question 3] | |
78 * spawn v | |
79 * [response]<---------------------[check result] | |
80 * v | |
81 * [message 4/5] | |
82 */ | |
83 static struct { | |
84 struct { | |
85 struct message msg; | |
86 struct action act; | |
87 } msgs[5]; | |
88 | |
89 /* This is the sprite and action to click on. */ | |
90 int x; | |
91 int y; | |
92 struct texture image; | |
93 struct sprite sprite; | |
94 struct action event; | |
95 | |
96 /* This is the event for the response. */ | |
97 struct action response; | |
98 | |
99 struct script script; | |
100 struct action script_act; | |
101 } guide = { | |
102 .msgs = { | |
103 { | |
104 .msg = { | |
105 .text = { | |
106 "Bienvenue dans ce monde Molko." | |
107 } | |
108 }, | |
109 }, | |
110 { | |
111 .msg = { | |
112 .text = { | |
113 "Penses tu vraiment pouvoir me battre ?" | |
114 } | |
115 } | |
116 }, | |
117 { | |
118 .msg = { | |
119 .flags = MESSAGE_QUESTION, | |
120 .text = { | |
121 "Non j'ai la trouille.", | |
122 "Bien sûr, je suis la légende." | |
123 } | |
124 } | |
125 }, | |
126 | |
127 /* In case of NO. */ | |
128 { | |
129 .msg = { | |
130 .text = { | |
131 "Poule mouillée." | |
132 } | |
133 } | |
134 }, | |
135 | |
136 /* In case of YES. */ | |
137 { | |
138 .msg = { | |
139 .text = { | |
140 "Prépare toi à souffrir." | |
141 } | |
142 } | |
143 } | |
144 } | |
145 }; | |
146 | |
147 static bool | |
148 guide_response_update(struct action *act, unsigned int ticks) | |
149 { | |
150 /* Immediately return to get access to end. */ | |
151 (void)act; | |
152 (void)ticks; | |
153 | |
154 return true; | |
155 } | |
156 | |
157 static void | |
158 guide_response_end(struct action *act) | |
159 { | |
160 (void)act; | |
161 | |
162 /* We add a final response depending on the result. */ | |
163 const int index = guide.msgs[2].msg.index + 3; | |
164 | |
165 message_action(&guide.msgs[index].msg, &guide.msgs[index].act); | |
166 action_stack_add(&stack, &guide.msgs[index].act); | |
167 } | |
168 | |
169 static bool | |
170 guide_running(void) | |
171 { | |
172 return guide.script.actionsz > 0; | |
173 } | |
174 | |
175 static void | |
176 guide_popup(void) | |
177 { | |
178 /* Prepare the script with first 3 messages. */ | |
179 for (size_t i = 0; i < 3; ++i) { | |
180 message_action(&guide.msgs[i].msg, &guide.msgs[i].act); | |
181 script_append(&guide.script, &guide.msgs[i].act); | |
182 } | |
183 | |
184 /* Reset the message index. */ | |
185 guide.msgs[2].msg.index = 0; | |
186 | |
187 /* This is final action that analyze user input. */ | |
188 guide.response.update = guide_response_update; | |
189 guide.response.end = guide_response_end; | |
190 script_append(&guide.script, &guide.response); | |
191 | |
192 script_action(&guide.script, &guide.script_act); | |
193 action_stack_add(&stack, &guide.script_act); | |
194 } | |
195 | |
196 static void | |
197 guide_handle(struct action *act, const union event *ev) | |
198 { | |
199 (void)act; | |
200 | |
201 if (guide_running()) | |
202 return; | |
203 | |
204 switch (ev->type) { | |
205 case EVENT_CLICKDOWN: | |
206 if (maths_is_boxed(guide.x, guide.y, | |
207 guide.sprite.cellw, guide.sprite.cellh, | |
208 ev->click.x, ev->click.y)) | |
209 guide_popup(); | |
210 break; | |
211 default: | |
212 break; | |
213 } | |
214 } | |
215 | |
216 static void | |
217 guide_draw(struct action *act) | |
218 { | |
219 (void)act; | |
220 | |
221 sprite_draw(&guide.sprite, 0, 0, guide.x, guide.y); | |
222 } | |
223 | |
224 static void | |
225 guide_init(void) | |
226 { | |
227 if (!image_openmem(&guide.image, sprites_people, sizeof (sprites_people))) | |
228 panic(); | |
229 | |
230 sprite_init(&guide.sprite, &guide.image, 48, 48); | |
231 | |
232 /* This is the global guide action. */ | |
233 guide.event.handle = guide_handle; | |
234 guide.event.draw = guide_draw; | |
235 guide.x = 200; | |
236 guide.y = 200; | |
237 } | |
238 | |
239 static void | |
240 chest_handle(struct action *act, const union event *ev) | |
241 { | |
242 (void)act; | |
243 | |
244 if (chest.opened) | |
245 return; | |
246 | |
247 switch (ev->type) { | |
248 case EVENT_CLICKDOWN: | |
249 if (maths_is_boxed(chest.x, chest.y, chest.sprite.cellw, chest.sprite.cellh, | |
250 ev->click.x, ev->click.y)) { | |
251 chest.opened = true; | |
252 message_action(&chest.msg, &chest.msg_act); | |
253 action_stack_add(&stack, &chest.msg_act); | |
254 } | |
255 default: | |
256 break; | |
257 } | |
258 } | |
259 | |
260 static void | |
261 chest_draw(struct action *act) | |
262 { | |
263 (void)act; | |
264 | |
265 const int row = chest.opened ? 3 : 0; | |
266 | |
267 sprite_draw(&chest.sprite, row, 0, chest.x, chest.y); | |
268 } | |
269 | |
270 static void | |
271 chest_init(void) | |
272 { | |
273 if (!image_openmem(&chest.image, sprites_chest, sizeof (sprites_chest))) | |
274 panic(); | |
275 | |
276 sprite_init(&chest.sprite, &chest.image, 32, 32); | |
277 | |
278 chest.x = 100; | |
279 chest.y = 100; | |
280 chest.event.handle = chest_handle; | |
281 chest.event.draw = chest_draw; | |
282 } | |
283 | |
284 static void | |
285 init(void) | |
286 { | |
287 if (!sys_init() || | |
288 !window_init("Example - Action", W, H) || | |
289 !theme_init()) | |
290 panic(); | |
291 | |
292 guide_init(); | |
293 chest_init(); | |
294 } | |
295 | |
296 static void | |
297 run(void) | |
298 { | |
299 struct clock clock = {0}; | |
300 | |
301 clock_start(&clock); | |
302 action_stack_add(&stack, &chest.event); | |
303 action_stack_add(&stack, &guide.event); | |
304 | |
305 for (;;) { | |
306 unsigned int elapsed = clock_elapsed(&clock); | |
307 | |
308 clock_start(&clock); | |
309 | |
310 for (union event ev; event_poll(&ev); ) { | |
311 switch (ev.type) { | |
312 case EVENT_QUIT: | |
313 return; | |
314 default: | |
315 action_stack_handle(&stack, &ev); | |
316 break; | |
317 } | |
318 } | |
319 | |
320 painter_set_color(0xffffffff); | |
321 painter_clear(); | |
322 action_stack_update(&stack, elapsed); | |
323 action_stack_draw(&stack); | |
324 painter_present(); | |
325 | |
326 if ((elapsed = clock_elapsed(&clock)) < 20) | |
327 delay(20 - elapsed); | |
328 } | |
329 } | |
330 | |
331 static void | |
332 quit(void) | |
333 { | |
334 theme_finish(); | |
335 window_finish(); | |
336 sys_finish(); | |
337 } | |
338 | |
339 int | |
340 main(int argc, char **argv) | |
341 { | |
342 (void)argc; | |
343 (void)argv; | |
344 | |
345 init(); | |
346 run(); | |
347 quit(); | |
348 | |
349 return 0; | |
350 } |