comparison src/libmlk-core/core/script.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/script.c@d01e83210ca2
children 460c78706989
comparison
equal deleted inserted replaced
319:b843eef4cc35 320:8f9937403749
1 /*
2 * script.c -- convenient sequence 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 <stdlib.h>
21 #include <string.h>
22
23 #include "action.h"
24 #include "error.h"
25 #include "script.h"
26
27 static struct action *
28 current(struct script *s)
29 {
30 if (s->cur >= s->actionsz)
31 return NULL;
32
33 return s->actions[s->cur];
34 }
35
36 static void
37 handle(struct action *a, const union event *ev)
38 {
39 script_handle(a->data, ev);
40 }
41
42 static int
43 update(struct action *a, unsigned int ticks)
44 {
45 return script_update(a->data, ticks);
46 }
47
48 static void
49 draw(struct action *a)
50 {
51 script_draw(a->data);
52 }
53
54 static void
55 finish(struct action *a)
56 {
57 script_finish(a->data);
58 }
59
60 void
61 script_init(struct script *s)
62 {
63 assert(s);
64
65 memset(s, 0, sizeof (*s));
66 }
67
68 int
69 script_append(struct script *s, struct action *a)
70 {
71 assert(s);
72 assert(a);
73
74 if (s->actionsz >= SCRIPT_ACTION_MAX)
75 return errorf("script is full");
76
77 s->actions[s->actionsz++] = a;
78
79 return 0;
80 }
81
82 void
83 script_handle(struct script *s, const union event *ev)
84 {
85 assert(s);
86 assert(ev);
87
88 struct action *a = current(s);
89
90 if (a)
91 action_handle(a, ev);
92 }
93
94 int
95 script_update(struct script *s, unsigned int ticks)
96 {
97 assert(s);
98
99 struct action *a = current(s);
100
101 if (!a)
102 return 1;
103
104 if (action_update(a, ticks)) {
105 action_end(a);
106 s->cur++;
107 }
108
109 return s->cur >= s->actionsz;
110 }
111
112 void
113 script_draw(struct script *s)
114 {
115 assert(s);
116
117 struct action *a = current(s);
118
119 if (a)
120 action_draw(a);
121 }
122
123 int
124 script_completed(const struct script *s)
125 {
126 assert(s);
127
128 return s->cur >= s->actionsz;
129 }
130
131 void
132 script_finish(struct script *s)
133 {
134 assert(s);
135
136 for (size_t i = 0; i < s->actionsz; ++i)
137 action_finish(s->actions[i]);
138
139 memset(s, 0, sizeof (*s));
140 }
141
142 void
143 script_action(struct script *s, struct action *action)
144 {
145 assert(s);
146 assert(action);
147
148 memset(action, 0, sizeof (*action));
149 action->data = s;
150 action->handle = handle;
151 action->update = update;
152 action->draw = draw;
153 action->finish = finish;
154 }