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