Mercurial > molko
comparison examples/example-font.c @ 144:28d9bb62fcb1
core: add font_box function, closes #2495
While here, add an example of usage.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 14 Oct 2020 16:40:34 +0200 |
parents | |
children | c577c15df07f |
comparison
equal
deleted
inserted
replaced
143:4f4795cba52f | 144:28d9bb62fcb1 |
---|---|
1 /* | |
2 * example-font.c -- show how to use fonts | |
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/font.h> | |
22 #include <core/painter.h> | |
23 #include <core/panic.h> | |
24 #include <core/sys.h> | |
25 #include <core/texture.h> | |
26 #include <core/theme.h> | |
27 #include <core/util.h> | |
28 #include <core/window.h> | |
29 | |
30 #define W (1280) | |
31 #define H (720) | |
32 | |
33 /* Friendly taken from: https://lospec.com/palette-list/apollo */ | |
34 static const unsigned long colors[] = { | |
35 0x3c5e8bff, /* Blue. */ | |
36 0x468232ff, /* Green. */ | |
37 0xad7757ff, /* Light brown. */ | |
38 0x884b2bff, /* Brown. */ | |
39 0x752438ff, /* Red. */ | |
40 0x7a367bff, /* Magenta. */ | |
41 0x151d28ff, /* Dark */ | |
42 0xc7cfccff, /* Christian Grey. */ | |
43 }; | |
44 | |
45 static void | |
46 init(void) | |
47 { | |
48 if (!sys_init() || | |
49 !window_init("Example - Font", W, H) || | |
50 !theme_init()) | |
51 panic(); | |
52 } | |
53 | |
54 static void | |
55 run(void) | |
56 { | |
57 struct clock clock = {0}; | |
58 struct font *font = theme_default()->fonts[THEME_FONT_INTERFACE]; | |
59 int ci = 0; | |
60 enum font_style style = font->style; | |
61 | |
62 clock_start(&clock); | |
63 | |
64 for (;;) { | |
65 struct texture tex; | |
66 union event ev; | |
67 unsigned int elapsed = clock_elapsed(&clock); | |
68 | |
69 clock_start(&clock); | |
70 | |
71 while (event_poll(&ev)) { | |
72 switch (ev.type) { | |
73 case EVENT_KEYDOWN: | |
74 switch (ev.key.key) { | |
75 case KEY_LEFT: | |
76 if (ci > 0) | |
77 ci--; | |
78 break; | |
79 case KEY_RIGHT: | |
80 if ((size_t)ci < NELEM(colors)) | |
81 ci++; | |
82 break; | |
83 case KEY_SPACE: | |
84 if (style == FONT_STYLE_ANTIALIASED) | |
85 style = FONT_STYLE_NONE; | |
86 else | |
87 style = FONT_STYLE_ANTIALIASED; | |
88 default: | |
89 break; | |
90 } | |
91 break; | |
92 case EVENT_QUIT: | |
93 return; | |
94 default: | |
95 break; | |
96 } | |
97 } | |
98 | |
99 painter_set_color(0xffffffff); | |
100 painter_clear(); | |
101 | |
102 font->color = colors[ci]; | |
103 font->style = style; | |
104 | |
105 if (!font_render(font, &tex, "Example of text. Use <Left>/<Right> to change color and <Space> to toggle antialiasing.")) | |
106 panic(); | |
107 | |
108 texture_draw(&tex, 10, 10); | |
109 painter_present(); | |
110 texture_finish(&tex); | |
111 | |
112 if ((elapsed = clock_elapsed(&clock)) < 20) | |
113 delay(20 - elapsed); | |
114 } | |
115 } | |
116 | |
117 static void | |
118 quit(void) | |
119 { | |
120 theme_finish(); | |
121 window_finish(); | |
122 sys_finish(); | |
123 } | |
124 | |
125 int | |
126 main(int argc, char **argv) | |
127 { | |
128 (void)argc; | |
129 (void)argv; | |
130 | |
131 init(); | |
132 run(); | |
133 quit(); | |
134 | |
135 return 0; | |
136 } |