comparison examples/example-message/example-message.c @ 534:88e9bd420a28

rpg: add delegate/style support to mlk_message
author David Demelier <markand@malikania.fr>
date Sun, 05 Mar 2023 10:51:25 +0100
parents f45a023f6690
children c7664b679a95
comparison
equal deleted inserted replaced
533:79afc6d5cc7e 534:88e9bd420a28
56 switch (ev->type) { 56 switch (ev->type) {
57 case MLK_EVENT_QUIT: 57 case MLK_EVENT_QUIT:
58 mlk_game_quit(); 58 mlk_game_quit();
59 break; 59 break;
60 default: 60 default:
61 message_handle(st->data, ev); 61 mlk_message_handle(st->data, ev);
62 break; 62 break;
63 } 63 }
64 } 64 }
65 65
66 static void 66 static void
67 update(struct mlk_state *st, unsigned int ticks) 67 update(struct mlk_state *st, unsigned int ticks)
68 { 68 {
69 if (message_update(st->data, ticks)) 69 if (mlk_message_update(st->data, ticks))
70 mlk_game_quit(); 70 mlk_game_quit();
71 } 71 }
72 72
73 static void 73 static void
74 draw(struct mlk_state *st) 74 draw(struct mlk_state *st)
75 { 75 {
76 mlk_painter_set_color(MLK_EXAMPLE_BG); 76 mlk_painter_set_color(MLK_EXAMPLE_BG);
77 mlk_painter_clear(); 77 mlk_painter_clear();
78 message_draw(st->data); 78 mlk_message_draw(st->data);
79 mlk_painter_present(); 79 mlk_painter_present();
80 } 80 }
81 81
82 static void 82 static void
83 run(struct message *msg) 83 run(struct mlk_message *msg)
84 { 84 {
85 struct mlk_state state = { 85 struct mlk_state state = {
86 .data = msg, 86 .data = msg,
87 .handle = handle, 87 .handle = handle,
88 .update = update, 88 .update = update,
89 .draw = draw 89 .draw = draw
90 }; 90 };
91 91
92 message_start(msg); 92 mlk_message_start(msg);
93 93
94 mlk_game_init(); 94 mlk_game_init();
95 mlk_game_push(&state); 95 mlk_game_push(&state);
96 mlk_game_loop(); 96 mlk_game_loop();
97 } 97 }
113 const char * const text[] = { 113 const char * const text[] = {
114 "This is a basic message.", 114 "This is a basic message.",
115 "Vertical spacing is automatically computed.", 115 "Vertical spacing is automatically computed.",
116 "You need to press <Enter> to close it.", 116 "You need to press <Enter> to close it.",
117 }; 117 };
118 struct message msg = { 118 struct mlk_message msg = {
119 .x = MX, 119 .x = MX,
120 .y = MY, 120 .y = MY,
121 .w = MW, 121 .w = MW,
122 .spacing = 12,
123 .lines = text, 122 .lines = text,
124 .linesz = 3 123 .linesz = 3
125 }; 124 };
126 125
127 message_query(&msg, NULL, &msg.h); 126 mlk_message_query(&msg, NULL, &msg.h);
128 run(&msg); 127 run(&msg);
129 } 128 }
130 129
131 static void 130 static void
132 automatic(void) 131 automatic(void)
134 const char * const text[] = { 133 const char * const text[] = {
135 "This is a an automatic message.", 134 "This is a an automatic message.",
136 "It will disappear in a few seconds.", 135 "It will disappear in a few seconds.",
137 "You can still press <Enter> to close it quicker." 136 "You can still press <Enter> to close it quicker."
138 }; 137 };
139 struct message msg = { 138 struct mlk_message msg = {
140 .x = MX, 139 .x = MX,
141 .y = MY, 140 .y = MY,
142 .w = MW, 141 .w = MW,
143 .timeout = MESSAGE_TIMEOUT_DEFAULT,
144 .lines = text, 142 .lines = text,
145 .linesz = 3, 143 .linesz = 3,
146 .flags = MESSAGE_FLAGS_AUTOMATIC 144 .flags = MLK_MESSAGE_FLAGS_AUTOMATIC
147 }; 145 };
148 146
149 message_query(&msg, NULL, &msg.h); 147 mlk_message_query(&msg, NULL, &msg.h);
150 run(&msg); 148 run(&msg);
151 } 149 }
152 150
153 static void 151 static void
154 fadein(void) 152 fadein(void)
155 { 153 {
156 const char * const text[] = { 154 const char * const text[] = {
157 "This message will fade in." 155 "This message will fade in."
158 }; 156 };
159 struct message msg = { 157 struct mlk_message msg = {
160 .x = MX, 158 .x = MX,
161 .y = MY, 159 .y = MY,
162 .w = MW, 160 .w = MW,
163 .delay = MESSAGE_DELAY_DEFAULT,
164 .lines = text, 161 .lines = text,
165 .linesz = 1, 162 .linesz = 1,
166 .flags = MESSAGE_FLAGS_FADEIN 163 .flags = MLK_MESSAGE_FLAGS_FADEIN
167 }; 164 };
168 165
169 message_query(&msg, NULL, &msg.h); 166 mlk_message_query(&msg, NULL, &msg.h);
170 run(&msg); 167 run(&msg);
171 } 168 }
172 169
173 static void 170 static void
174 fadeout(void) 171 fadeout(void)
175 { 172 {
176 const char * const text[] = { 173 const char * const text[] = {
177 "This message will fade out." 174 "This message will fade out."
178 }; 175 };
179 struct message msg = { 176 struct mlk_message msg = {
180 .x = MX, 177 .x = MX,
181 .y = MY, 178 .y = MY,
182 .w = MW, 179 .w = MW,
183 .delay = MESSAGE_DELAY_DEFAULT,
184 .lines = text, 180 .lines = text,
185 .linesz = 1, 181 .linesz = 1,
186 .flags = MESSAGE_FLAGS_FADEOUT 182 .flags = MLK_MESSAGE_FLAGS_FADEOUT
187 }; 183 };
188 184
189 message_query(&msg, NULL, &msg.h); 185 mlk_message_query(&msg, NULL, &msg.h);
190 run(&msg); 186 run(&msg);
191 } 187 }
192 188
193 static void 189 static void
194 fade(void) 190 fade(void)
195 { 191 {
196 const char * const text[] = { 192 const char * const text[] = {
197 "This message will fade in and out." 193 "This message will fade in and out."
198 }; 194 };
199 struct message msg = { 195 struct mlk_message msg = {
200 .x = MX, 196 .x = MX,
201 .y = MY, 197 .y = MY,
202 .w = MW, 198 .w = MW,
203 .delay = MESSAGE_DELAY_DEFAULT,
204 .lines = text, 199 .lines = text,
205 .linesz = 1, 200 .linesz = 1,
206 .flags = MESSAGE_FLAGS_FADEIN | MESSAGE_FLAGS_FADEOUT 201 .flags = MLK_MESSAGE_FLAGS_FADEIN | MLK_MESSAGE_FLAGS_FADEOUT
207 }; 202 };
208 203
209 message_query(&msg, NULL, &msg.h); 204 mlk_message_query(&msg, NULL, &msg.h);
210 run(&msg); 205 run(&msg);
211 } 206 }
212 207
213 static void 208 static void
214 question(void) 209 question(void)
215 { 210 {
216 const char * const text[] = { 211 const char * const text[] = {
217 "Okay, I've understood.", 212 "Okay, I've understood.",
218 "Nevermind, I'll do it again." 213 "Nevermind, I'll do it again."
219 }; 214 };
220 struct message msg = { 215 struct mlk_message msg = {
221 .x = MX, 216 .x = MX,
222 .y = MY, 217 .y = MY,
223 .w = MW, 218 .w = MW,
224 .lines = text, 219 .lines = text,
225 .linesz = 2, 220 .linesz = 2,
226 .flags = MESSAGE_FLAGS_QUESTION 221 .flags = MLK_MESSAGE_FLAGS_QUESTION
227 }; 222 };
228 223
229 message_query(&msg, NULL, &msg.h); 224 mlk_message_query(&msg, NULL, &msg.h);
230 run(&msg); 225 run(&msg);
231 } 226 }
232 227
233 static void 228 static void
234 smallbottom(void) 229 smallbottom(void)
239 const int y = (mlk_window.h - h - 10); 234 const int y = (mlk_window.h - h - 10);
240 const char * const text[] = { 235 const char * const text[] = {
241 "This one is small here." 236 "This one is small here."
242 }; 237 };
243 238
244 struct message msg = { 239 struct mlk_message msg = {
245 .x = x, 240 .x = x,
246 .y = y, 241 .y = y,
247 .w = w, 242 .w = w,
248 .h = h, 243 .h = h,
249 .delay = MESSAGE_DELAY_DEFAULT, 244 .flags = MLK_MESSAGE_FLAGS_FADEIN | MLK_MESSAGE_FLAGS_FADEOUT,
250 .flags = MESSAGE_FLAGS_FADEIN | MESSAGE_FLAGS_FADEOUT,
251 .lines = text, 245 .lines = text,
252 .linesz = 1 246 .linesz = 1
253 }; 247 };
254 248
255 run(&msg); 249 run(&msg);
260 { 254 {
261 const char * const text[] = { 255 const char * const text[] = {
262 "This one is too small in height and will emit a warning.", 256 "This one is too small in height and will emit a warning.",
263 "Because this line will be incomplete." 257 "Because this line will be incomplete."
264 }; 258 };
265 struct message msg = { 259 struct mlk_message msg = {
266 .x = MX, 260 .x = MX,
267 .y = MY, 261 .y = MY,
268 .w = MW, 262 .w = MW,
269 .h = 40, 263 .h = 40,
270 .lines = text, 264 .lines = text,
278 toosmallw(void) 272 toosmallw(void)
279 { 273 {
280 const char * const text[] = { 274 const char * const text[] = {
281 "This one is too small in width." 275 "This one is too small in width."
282 }; 276 };
283 struct message msg = { 277 struct mlk_message msg = {
284 .x = MX, 278 .x = MX,
285 .y = MY, 279 .y = MY,
286 .w = 160, 280 .w = 160,
287 .h = MH, 281 .h = MH,
288 .lines = text, 282 .lines = text,
293 } 287 }
294 288
295 static void 289 static void
296 custom(void) 290 custom(void)
297 { 291 {
298 #if 0
299 const char * const text[] = { 292 const char * const text[] = {
300 "This one will destroy your eyes.", 293 "This one will destroy your eyes.",
301 "Because it use a terrible custom theme." 294 "Because it use a terrible custom theme."
302 }; 295 };
303 struct mlk_theme theme; 296 struct mlk_message_style style = mlk_message_style;
304 struct message msg = { 297 struct mlk_message msg = {
305 .x = MX, 298 .x = MX,
306 .y = MY, 299 .y = MY,
307 .w = MW, 300 .w = MW,
308 .h = MH,
309 .lines = text, 301 .lines = text,
310 .linesz = 2, 302 .linesz = 2,
311 .theme = &theme 303 .style = &style
312 }; 304 };
313 305
314 /* Borrow default theme and change its frame drawing. */ 306 style.bg_color = 0xf85d80ff;
315 theme.draw_frame = my_draw_frame; 307 style.border_color = 0xd94a69ff;
316 theme.colors[MLK_THEME_COLOR_NORMAL] = 0x0000ffff; 308 style.text_color = 0xffffffff;
317 309
318 run(&msg); 310 mlk_message_query(&msg, NULL, &msg.h);
319 #endif 311 run(&msg);
320 } 312 }
321 313
322 static void 314 static void
323 quit(void) 315 quit(void)
324 { 316 {