comparison libcore/core/drawable.c @ 142:fea0cc899931

core: drawable_stack_update returns a bool (similar to action_stack_update) While here, give the opportunity to do something on the end of the drawable with the help of the new end field. While here, add a test and fix some documentation.
author David Demelier <markand@malikania.fr>
date Wed, 14 Oct 2020 14:48:07 +0200
parents 30b68089ae70
children
comparison
equal deleted inserted replaced
141:4eeeccf2b732 142:fea0cc899931
44 44
45 dw->draw(dw); 45 dw->draw(dw);
46 } 46 }
47 47
48 void 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
49 drawable_finish(struct drawable *dw) 58 drawable_finish(struct drawable *dw)
50 { 59 {
60 assert(dw);
61
51 if (dw->finish) 62 if (dw->finish)
52 dw->finish(dw); 63 dw->finish(dw);
53 } 64 }
54 65
55 void 66 void
74 } 85 }
75 86
76 return false; 87 return false;
77 } 88 }
78 89
79 void 90 bool
80 drawable_stack_update(struct drawable_stack *st, unsigned int ticks) 91 drawable_stack_update(struct drawable_stack *st, unsigned int ticks)
81 { 92 {
82 assert(st); 93 assert(st);
83 94
84 for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) { 95 for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) {
85 struct drawable *dw = st->objects[i]; 96 struct drawable *dw = st->objects[i];
86 97
87 if (dw && drawable_update(dw, ticks)) { 98 if (dw && drawable_update(dw, ticks)) {
99 drawable_end(dw);
88 drawable_finish(dw); 100 drawable_finish(dw);
89 st->objects[i] = NULL; 101 st->objects[i] = NULL;
90 } 102 }
91 } 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);
92 } 110 }
93 111
94 void 112 void
95 drawable_stack_draw(struct drawable_stack *st) 113 drawable_stack_draw(struct drawable_stack *st)
96 { 114 {
101 DRAWABLE_FOREACH(st, dw) 119 DRAWABLE_FOREACH(st, dw)
102 if (dw) 120 if (dw)
103 drawable_draw(dw); 121 drawable_draw(dw);
104 } 122 }
105 123
124 bool
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 false;
134
135 return true;
136 }
137
106 void 138 void
107 drawable_stack_finish(struct drawable_stack *st) 139 drawable_stack_finish(struct drawable_stack *st)
108 { 140 {
109 for (size_t i = 0; i < DRAWABLE_STACK_MAX; ++i) { 141 assert(st);
110 struct drawable *dw = st->objects[i];
111 142
112 if (dw && dw->finish) 143 struct drawable *dw;
113 dw->finish(dw); 144
145 DRAWABLE_FOREACH(st, dw) {
146 if (dw) {
147 drawable_end(dw);
148 drawable_finish(dw);
149 }
114 } 150 }
115 151
116 memset(st, 0, sizeof (*st)); 152 memset(st, 0, sizeof (*st));
117 } 153 }