comparison examples/example-label.c @ 145:7f1af54bb35a

core: rework label alignment, closes #2494 @1h Now labels can be positioned on a bounding box with much more convenient possibilities.
author David Demelier <markand@malikania.fr>
date Wed, 14 Oct 2020 18:11:38 +0200
parents
children c577c15df07f
comparison
equal deleted inserted replaced
144:28d9bb62fcb1 145:7f1af54bb35a
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/clock.h>
20 #include <core/event.h>
21 #include <core/label.h>
22 #include <core/painter.h>
23 #include <core/panic.h>
24 #include <core/sys.h>
25 #include <core/theme.h>
26 #include <core/util.h>
27 #include <core/window.h>
28
29 #define W (1280)
30 #define H (720)
31
32 static void
33 init(void)
34 {
35 if (!sys_init() ||
36 !window_init("Example - Label", W, H) ||
37 !theme_init())
38 panic();
39 }
40
41 static void
42 quit(void)
43 {
44 theme_finish();
45 window_finish();
46 sys_finish();
47 }
48
49 static void
50 run(void)
51 {
52 struct clock clock = {0};
53 struct label labels[] = {
54 {
55 .x = 0,
56 .y = 0,
57 .w = W,
58 .h = H,
59 .text = "Top left",
60 .align = LABEL_ALIGN_TOP_LEFT
61 },
62 {
63 .x = 0,
64 .y = 0,
65 .w = W,
66 .h = H,
67 .text = "Top",
68 .align = LABEL_ALIGN_TOP
69 },
70 {
71 .x = 0,
72 .y = 0,
73 .w = W,
74 .h = H,
75 .text = "Top right",
76 .align = LABEL_ALIGN_TOP_RIGHT
77 },
78 {
79 .x = 0,
80 .y = 0,
81 .w = W,
82 .h = H,
83 .text = "Right",
84 .align = LABEL_ALIGN_RIGHT
85 },
86 {
87 .x = 0,
88 .y = 0,
89 .w = W,
90 .h = H,
91 .text = "Bottom right",
92 .align = LABEL_ALIGN_BOTTOM_RIGHT
93 },
94 {
95 .x = 0,
96 .y = 0,
97 .w = W,
98 .h = H,
99 .text = "Bottom",
100 .align = LABEL_ALIGN_BOTTOM
101 },
102 {
103 .x = 0,
104 .y = 0,
105 .w = W,
106 .h = H,
107 .text = "Bottom left",
108 .align = LABEL_ALIGN_BOTTOM_LEFT
109 },
110 {
111 .x = 0,
112 .y = 0,
113 .w = W,
114 .h = H,
115 .text = "Left",
116 .align = LABEL_ALIGN_LEFT
117 },
118 {
119 .x = 0,
120 .y = 0,
121 .w = W,
122 .h = H,
123 .text = "The world is Malikania.",
124 .flags = LABEL_FLAGS_SHADOW
125 },
126 };
127
128 clock_start(&clock);
129
130 for (;;) {
131 union event ev;
132 unsigned int elapsed = clock_elapsed(&clock);
133
134 clock_start(&clock);
135
136 while (event_poll(&ev)) {
137 switch (ev.type) {
138 case EVENT_QUIT:
139 return;
140 default:
141 break;
142 }
143 }
144
145 painter_set_color(0x4f8fbaff);
146 painter_clear();
147
148 for (size_t i = 0; i < NELEM(labels); ++i)
149 label_draw(&labels[i]);
150
151 painter_present();
152
153 if ((elapsed = clock_elapsed(&clock)) < 20)
154 delay(20 - elapsed);
155 }
156 }
157
158 int
159 main(int argc, char **argv)
160 {
161 (void)argc;
162 (void)argv;
163
164 init();
165 run();
166 quit();
167 }
168