Mercurial > molko
comparison examples/example-message/example-message.c @ 415:a5b98db4fd87
misc: fix .hgignore too greedy
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 09 Oct 2022 13:53:33 +0200 |
parents | |
children | 8f59201dc76b |
comparison
equal
deleted
inserted
replaced
414:6947c1fefe5c | 415:a5b98db4fd87 |
---|---|
1 /* | |
2 * example-message.c -- show how to use messages | |
3 * | |
4 * Copyright (c) 2020-2022 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 <stddef.h> | |
20 | |
21 #include <core/core.h> | |
22 #include <core/event.h> | |
23 #include <core/game.h> | |
24 #include <core/painter.h> | |
25 #include <core/panic.h> | |
26 #include <core/state.h> | |
27 #include <core/sys.h> | |
28 #include <core/util.h> | |
29 #include <core/window.h> | |
30 | |
31 #include <ui/frame.h> | |
32 #include <ui/theme.h> | |
33 #include <ui/ui.h> | |
34 | |
35 #include <rpg/message.h> | |
36 #include <rpg/rpg.h> | |
37 | |
38 #define W (1280) | |
39 #define H (720) | |
40 | |
41 #define MW (W * 0.75) | |
42 #define MH (H * 0.120) | |
43 #define MX ((W / 2) - (MW / 2)) | |
44 #define MY (100) | |
45 | |
46 static struct state *states[1]; | |
47 | |
48 static void | |
49 init(void) | |
50 { | |
51 if (core_init("fr.malikania", "example-message") < 0 || ui_init() < 0 || rpg_init() < 0) | |
52 panic(); | |
53 if (window_open("Example - Message", W, H) < 0) | |
54 panic(); | |
55 } | |
56 | |
57 static void | |
58 quit(void) | |
59 { | |
60 window_finish(); | |
61 rpg_finish(); | |
62 ui_finish(); | |
63 core_finish(); | |
64 } | |
65 | |
66 static void | |
67 handle(struct state *st, const union event *ev) | |
68 { | |
69 switch (ev->type) { | |
70 case EVENT_QUIT: | |
71 game_quit(); | |
72 break; | |
73 default: | |
74 message_handle(st->data, ev); | |
75 break; | |
76 } | |
77 } | |
78 | |
79 static void | |
80 update(struct state *st, unsigned int ticks) | |
81 { | |
82 if (message_update(st->data, ticks)) | |
83 game_quit(); | |
84 } | |
85 | |
86 static void | |
87 draw(struct state *st) | |
88 { | |
89 painter_set_color(0xffffffff); | |
90 painter_clear(); | |
91 message_draw(st->data); | |
92 painter_present(); | |
93 } | |
94 | |
95 static void | |
96 run(struct message *msg) | |
97 { | |
98 struct state state = { | |
99 .data = msg, | |
100 .handle = handle, | |
101 .update = update, | |
102 .draw = draw | |
103 }; | |
104 | |
105 message_start(msg); | |
106 | |
107 game_init(states, UTIL_SIZE(states)); | |
108 game_push(&state); | |
109 game_loop(); | |
110 } | |
111 | |
112 static void | |
113 my_draw_frame(const struct theme *th, const struct frame *f) | |
114 { | |
115 (void)th; | |
116 | |
117 painter_set_color(0xff0000ff); | |
118 painter_draw_rectangle(f->x, f->y, f->w, f->h); | |
119 } | |
120 | |
121 static void | |
122 basic(void) | |
123 { | |
124 const char * const text[] = { | |
125 "This is a basic message.", | |
126 "Vertical spacing is automatically computed.", | |
127 "You need to press <Enter> to close it.", | |
128 }; | |
129 struct message msg = { | |
130 .x = MX, | |
131 .y = MY, | |
132 .w = MW, | |
133 .spacing = 12, | |
134 .lines = text, | |
135 .linesz = 3 | |
136 }; | |
137 | |
138 message_query(&msg, NULL, &msg.h); | |
139 run(&msg); | |
140 } | |
141 | |
142 static void | |
143 automatic(void) | |
144 { | |
145 const char * const text[] = { | |
146 "This is a an automatic message.", | |
147 "It will disappear in a few seconds.", | |
148 "You can still press <Enter> to close it quicker." | |
149 }; | |
150 struct message msg = { | |
151 .x = MX, | |
152 .y = MY, | |
153 .w = MW, | |
154 .timeout = MESSAGE_TIMEOUT_DEFAULT, | |
155 .lines = text, | |
156 .linesz = 3, | |
157 .flags = MESSAGE_FLAGS_AUTOMATIC | |
158 }; | |
159 | |
160 message_query(&msg, NULL, &msg.h); | |
161 run(&msg); | |
162 } | |
163 | |
164 static void | |
165 fadein(void) | |
166 { | |
167 const char * const text[] = { | |
168 "This message will fade in." | |
169 }; | |
170 struct message msg = { | |
171 .x = MX, | |
172 .y = MY, | |
173 .w = MW, | |
174 .delay = MESSAGE_DELAY_DEFAULT, | |
175 .lines = text, | |
176 .linesz = 1, | |
177 .flags = MESSAGE_FLAGS_FADEIN | |
178 }; | |
179 | |
180 message_query(&msg, NULL, &msg.h); | |
181 run(&msg); | |
182 } | |
183 | |
184 static void | |
185 fadeout(void) | |
186 { | |
187 const char * const text[] = { | |
188 "This message will fade out." | |
189 }; | |
190 struct message msg = { | |
191 .x = MX, | |
192 .y = MY, | |
193 .w = MW, | |
194 .delay = MESSAGE_DELAY_DEFAULT, | |
195 .lines = text, | |
196 .linesz = 1, | |
197 .flags = MESSAGE_FLAGS_FADEOUT | |
198 }; | |
199 | |
200 message_query(&msg, NULL, &msg.h); | |
201 run(&msg); | |
202 } | |
203 | |
204 static void | |
205 fade(void) | |
206 { | |
207 const char * const text[] = { | |
208 "This message will fade in and out." | |
209 }; | |
210 struct message msg = { | |
211 .x = MX, | |
212 .y = MY, | |
213 .w = MW, | |
214 .delay = MESSAGE_DELAY_DEFAULT, | |
215 .lines = text, | |
216 .linesz = 1, | |
217 .flags = MESSAGE_FLAGS_FADEIN | MESSAGE_FLAGS_FADEOUT | |
218 }; | |
219 | |
220 message_query(&msg, NULL, &msg.h); | |
221 run(&msg); | |
222 } | |
223 | |
224 static void | |
225 question(void) | |
226 { | |
227 const char * const text[] = { | |
228 "Okay, I've understood.", | |
229 "Nevermind, I'll do it again." | |
230 }; | |
231 struct message msg = { | |
232 .x = MX, | |
233 .y = MY, | |
234 .w = MW, | |
235 .lines = text, | |
236 .linesz = 2, | |
237 .flags = MESSAGE_FLAGS_QUESTION | |
238 }; | |
239 | |
240 message_query(&msg, NULL, &msg.h); | |
241 run(&msg); | |
242 } | |
243 | |
244 static void | |
245 smallbottom(void) | |
246 { | |
247 const unsigned int w = window.w / 4; | |
248 const unsigned int h = MH; | |
249 const int x = (window.w / 2) - (w / 2); | |
250 const int y = (window.h - h - 10); | |
251 const char * const text[] = { | |
252 "This one is small here." | |
253 }; | |
254 | |
255 struct message msg = { | |
256 .x = x, | |
257 .y = y, | |
258 .w = w, | |
259 .h = h, | |
260 .delay = MESSAGE_DELAY_DEFAULT, | |
261 .flags = MESSAGE_FLAGS_FADEIN | MESSAGE_FLAGS_FADEOUT, | |
262 .lines = text, | |
263 .linesz = 1 | |
264 }; | |
265 | |
266 run(&msg); | |
267 } | |
268 | |
269 static void | |
270 toosmallh(void) | |
271 { | |
272 const char * const text[] = { | |
273 "This one is too small in height and will emit a warning.", | |
274 "Because this line will be incomplete." | |
275 }; | |
276 struct message msg = { | |
277 .x = MX, | |
278 .y = MY, | |
279 .w = MW, | |
280 .h = 40, | |
281 .lines = text, | |
282 .linesz = 2 | |
283 }; | |
284 | |
285 run(&msg); | |
286 } | |
287 | |
288 static void | |
289 toosmallw(void) | |
290 { | |
291 const char * const text[] = { | |
292 "This one is too small in width." | |
293 }; | |
294 struct message msg = { | |
295 .x = MX, | |
296 .y = MY, | |
297 .w = 160, | |
298 .h = MH, | |
299 .lines = text, | |
300 .linesz = 1 | |
301 }; | |
302 | |
303 run(&msg); | |
304 } | |
305 | |
306 static void | |
307 custom(void) | |
308 { | |
309 const char * const text[] = { | |
310 "This one will destroy your eyes.", | |
311 "Because it use a terrible custom theme." | |
312 }; | |
313 struct theme theme; | |
314 struct message msg = { | |
315 .x = MX, | |
316 .y = MY, | |
317 .w = MW, | |
318 .h = MH, | |
319 .lines = text, | |
320 .linesz = 2, | |
321 .theme = &theme | |
322 }; | |
323 | |
324 /* Borrow default theme and change its frame drawing. */ | |
325 theme_shallow(&theme, NULL); | |
326 theme.draw_frame = my_draw_frame; | |
327 theme.colors[THEME_COLOR_NORMAL] = 0x0000ffff; | |
328 | |
329 run(&msg); | |
330 } | |
331 | |
332 int | |
333 main(int argc, char **argv) | |
334 { | |
335 (void)argc; | |
336 (void)argv; | |
337 | |
338 init(); | |
339 basic(); | |
340 fadein(); | |
341 fadeout(); | |
342 fade(); | |
343 automatic(); | |
344 question(); | |
345 smallbottom(); | |
346 toosmallh(); | |
347 toosmallw(); | |
348 custom(); | |
349 quit(); | |
350 } |