Mercurial > molko
comparison src/examples/example-label/main.c @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | examples/example-label/main.c@d01e83210ca2 |
children |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * example-label.c -- show how to use label and alignments | |
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 <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/align.h> | |
32 #include <ui/label.h> | |
33 #include <ui/theme.h> | |
34 #include <ui/ui.h> | |
35 | |
36 #define W (1280) | |
37 #define H (720) | |
38 | |
39 struct { | |
40 enum align align; | |
41 struct label label; | |
42 } table[] = { | |
43 { | |
44 .align = ALIGN_TOP_LEFT, | |
45 .label = { | |
46 .text = "Top left" | |
47 } | |
48 }, | |
49 { | |
50 .align = ALIGN_TOP, | |
51 .label = { | |
52 .text = "Top", | |
53 } | |
54 }, | |
55 { | |
56 .align = ALIGN_TOP_RIGHT, | |
57 .label = { | |
58 .text = "Top right", | |
59 } | |
60 }, | |
61 { | |
62 .align = ALIGN_RIGHT, | |
63 .label = { | |
64 .text = "Right", | |
65 } | |
66 }, | |
67 { | |
68 .align = ALIGN_BOTTOM_RIGHT, | |
69 .label = { | |
70 .text = "Bottom right", | |
71 } | |
72 }, | |
73 { | |
74 .align = ALIGN_BOTTOM, | |
75 .label = { | |
76 .text = "Bottom", | |
77 } | |
78 }, | |
79 { | |
80 .align = ALIGN_BOTTOM_LEFT, | |
81 .label = { | |
82 .text = "Bottom left", | |
83 } | |
84 }, | |
85 { | |
86 .align = ALIGN_LEFT, | |
87 .label = { | |
88 .text = "Left", | |
89 } | |
90 }, | |
91 { | |
92 .align = ALIGN_CENTER, | |
93 .label = { | |
94 .text = "The world is Malikania.", | |
95 .flags = LABEL_FLAGS_SHADOW | |
96 } | |
97 } | |
98 }; | |
99 | |
100 static struct label mlabel = { | |
101 .text = "This one follows your mouse and is not aligned." | |
102 }; | |
103 | |
104 static void | |
105 init(void) | |
106 { | |
107 if (core_init("fr.malikania", "label") < 0 || ui_init() < 0) | |
108 panic(); | |
109 if (window_open("Example - Label", W, H) < 0) | |
110 panic(); | |
111 | |
112 for (size_t i = 0; i < UTIL_SIZE(table); ++i) { | |
113 struct label *l = &table[i].label; | |
114 unsigned int w, h; | |
115 | |
116 label_query(l, &w, &h); | |
117 align(table[i].align, &l->x, &l->y, w, h, 0, 0, W, H); | |
118 } | |
119 } | |
120 | |
121 static void | |
122 quit(void) | |
123 { | |
124 window_finish(); | |
125 ui_finish(); | |
126 core_finish(); | |
127 } | |
128 | |
129 static void | |
130 handle(struct state *st, const union event *ev) | |
131 { | |
132 (void)st; | |
133 | |
134 switch (ev->type) { | |
135 case EVENT_MOUSE: | |
136 mlabel.x = ev->mouse.x; | |
137 mlabel.y = ev->mouse.y; | |
138 break; | |
139 case EVENT_QUIT: | |
140 game_quit(); | |
141 break; | |
142 default: | |
143 break; | |
144 } | |
145 } | |
146 | |
147 static void | |
148 draw(struct state *st) | |
149 { | |
150 (void)st; | |
151 | |
152 painter_set_color(0x4f8fbaff); | |
153 painter_clear(); | |
154 | |
155 for (size_t i = 0; i < UTIL_SIZE(table); ++i) | |
156 label_draw(&table[i].label); | |
157 | |
158 label_draw(&mlabel); | |
159 painter_present(); | |
160 } | |
161 | |
162 static void | |
163 run(void) | |
164 { | |
165 struct state state = { | |
166 .handle = handle, | |
167 .draw = draw | |
168 }; | |
169 | |
170 game_push(&state); | |
171 game_loop(); | |
172 } | |
173 | |
174 int | |
175 main(int argc, char **argv) | |
176 { | |
177 (void)argc; | |
178 (void)argv; | |
179 | |
180 init(); | |
181 run(); | |
182 quit(); | |
183 } |