Mercurial > molko
comparison src/libmlk-core/core/drawable-stack.c @ 368:15bdac29ba4b
core: split drawable and drawable_stack
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 24 Oct 2021 17:07:20 +0200 |
parents | |
children | 460c78706989 |
comparison
equal
deleted
inserted
replaced
367:74f9cb70fc5d | 368:15bdac29ba4b |
---|---|
1 /* | |
2 * drawable-stack.c -- convenient stack of drawables | |
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 "drawable-stack.h" | |
24 | |
25 #define DRAWABLE_FOREACH(st, iter) \ | |
26 for (size_t i = 0; i < DRAWABLE_STACK_MAX && ((iter) = (st)->objects[i], 1); ++i) | |
27 | |
28 void | |
29 drawable_stack_init(struct drawable_stack *st) | |
30 { | |
31 assert(st); | |
32 | |
33 memset(st, 0, sizeof (*st)); | |
34 } | |
35 | |
36 int | |
37 drawable_stack_add(struct drawable_stack *st, struct drawable *dw) | |
38 { | |
39 assert(st); | |
40 assert(dw); | |
41 | |
42 for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) { | |
43 if (!st->objects[i]) { | |
44 st->objects[i] = dw; | |
45 return 0; | |
46 } | |
47 } | |
48 | |
49 return -1; | |
50 } | |
51 | |
52 int | |
53 drawable_stack_update(struct drawable_stack *st, unsigned int ticks) | |
54 { | |
55 assert(st); | |
56 | |
57 for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) { | |
58 struct drawable *dw = st->objects[i]; | |
59 | |
60 if (dw && drawable_update(dw, ticks)) { | |
61 drawable_end(dw); | |
62 drawable_finish(dw); | |
63 st->objects[i] = NULL; | |
64 } | |
65 } | |
66 | |
67 /* | |
68 * We process the array again in case a drawable added a new drawable | |
69 * within the update function. | |
70 */ | |
71 return drawable_stack_completed(st); | |
72 } | |
73 | |
74 void | |
75 drawable_stack_draw(struct drawable_stack *st) | |
76 { | |
77 assert(st); | |
78 | |
79 struct drawable *dw; | |
80 | |
81 DRAWABLE_FOREACH(st, dw) | |
82 if (dw) | |
83 drawable_draw(dw); | |
84 } | |
85 | |
86 int | |
87 drawable_stack_completed(const struct drawable_stack *st) | |
88 { | |
89 assert(st); | |
90 | |
91 struct drawable *dw; | |
92 | |
93 DRAWABLE_FOREACH(st, dw) | |
94 if (dw) | |
95 return 0; | |
96 | |
97 return 1; | |
98 } | |
99 | |
100 void | |
101 drawable_stack_finish(struct drawable_stack *st) | |
102 { | |
103 assert(st); | |
104 | |
105 struct drawable *dw; | |
106 | |
107 DRAWABLE_FOREACH(st, dw) { | |
108 if (dw) { | |
109 drawable_end(dw); | |
110 drawable_finish(dw); | |
111 } | |
112 } | |
113 | |
114 memset(st, 0, sizeof (*st)); | |
115 } |