Mercurial > molko
comparison src/libmlk-core/core/action-stack.c @ 369:1e06f871dc63
core: split action and action_stack
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 24 Oct 2021 17:33:12 +0200 |
parents | |
children | 460c78706989 |
comparison
equal
deleted
inserted
replaced
368:15bdac29ba4b | 369:1e06f871dc63 |
---|---|
1 /* | |
2 * action-stack.h -- convenient stack of actions | |
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 "action.h" | |
23 #include "action-stack.h" | |
24 | |
25 #define ACTION_FOREACH(st, iter) \ | |
26 for (size_t i = 0; i < ACTION_STACK_MAX && ((iter) = (st)->actions[i], 1); ++i) | |
27 | |
28 void | |
29 action_stack_init(struct action_stack *st) | |
30 { | |
31 assert(st); | |
32 | |
33 memset(st, 0, sizeof (*st)); | |
34 } | |
35 | |
36 int | |
37 action_stack_add(struct action_stack *st, struct action *act) | |
38 { | |
39 assert(st); | |
40 assert(act); | |
41 | |
42 for (size_t i = 0; i < ACTION_STACK_MAX; ++i) { | |
43 if (!st->actions[i]) { | |
44 st->actions[i] = act; | |
45 return 0; | |
46 } | |
47 } | |
48 | |
49 return -1; | |
50 } | |
51 | |
52 void | |
53 action_stack_handle(struct action_stack *st, const union event *ev) | |
54 { | |
55 assert(st); | |
56 assert(ev); | |
57 | |
58 struct action *act; | |
59 | |
60 ACTION_FOREACH(st, act) | |
61 if (act) | |
62 action_handle(act, ev); | |
63 } | |
64 | |
65 int | |
66 action_stack_update(struct action_stack *st, unsigned int ticks) | |
67 { | |
68 assert(st); | |
69 | |
70 struct action *act; | |
71 | |
72 for (size_t i = 0; i < ACTION_STACK_MAX; ++i) { | |
73 act = st->actions[i]; | |
74 | |
75 if (act && action_update(act, ticks)) { | |
76 action_end(act); | |
77 action_finish(act); | |
78 st->actions[i] = NULL; | |
79 } | |
80 } | |
81 | |
82 /* | |
83 * We process all actions again in case the user modified the stack | |
84 * within their update function. | |
85 */ | |
86 return action_stack_completed(st); | |
87 } | |
88 | |
89 void | |
90 action_stack_draw(const struct action_stack *st) | |
91 { | |
92 assert(st); | |
93 | |
94 struct action *act; | |
95 | |
96 ACTION_FOREACH(st, act) | |
97 if (act) | |
98 action_draw(act); | |
99 } | |
100 | |
101 int | |
102 action_stack_completed(const struct action_stack *st) | |
103 { | |
104 assert(st); | |
105 | |
106 struct action *act; | |
107 | |
108 ACTION_FOREACH(st, act) | |
109 if (act) | |
110 return 0; | |
111 | |
112 return 1; | |
113 } | |
114 | |
115 void | |
116 action_stack_finish(struct action_stack *st) | |
117 { | |
118 assert(st); | |
119 | |
120 struct action *act; | |
121 | |
122 ACTION_FOREACH(st, act) { | |
123 if (act) { | |
124 action_end(act); | |
125 action_finish(act); | |
126 } | |
127 } | |
128 | |
129 memset(st, 0, sizeof (*st)); | |
130 } |