comparison examples/example-font/example-font.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-font.c -- show how to use fonts
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 <core/core.h>
20 #include <core/event.h>
21 #include <core/font.h>
22 #include <core/game.h>
23 #include <core/painter.h>
24 #include <core/panic.h>
25 #include <core/state.h>
26 #include <core/sys.h>
27 #include <core/texture.h>
28 #include <core/util.h>
29 #include <core/window.h>
30
31 #include <ui/theme.h>
32 #include <ui/ui.h>
33
34 #define W (1280)
35 #define H (720)
36
37 /* Friendly taken from: https://lospec.com/palette-list/apollo */
38 static const unsigned long colors[] = {
39 0x3c5e8bff, /* Blue. */
40 0x468232ff, /* Green. */
41 0xad7757ff, /* Light brown. */
42 0x884b2bff, /* Brown. */
43 0x752438ff, /* Red. */
44 0x7a367bff, /* Magenta. */
45 0x151d28ff, /* Dark */
46 0xc7cfccff, /* Christian Grey. */
47 };
48
49 static struct state *states[1];
50 static int ci = 0;
51 static enum font_style style = FONT_STYLE_ANTIALIASED;
52
53 static void
54 init(void)
55 {
56 if (core_init("fr.malikania", "example-font") < 0 || ui_init() < 0)
57 panic();
58 if (window_open("Example - Font", W, H) < 0)
59 panic();
60 }
61
62 static void
63 handle(struct state *st, const union event *ev)
64 {
65 (void)st;
66
67 switch (ev->type) {
68 case EVENT_KEYDOWN:
69 switch (ev->key.key) {
70 case KEY_LEFT:
71 if (ci > 0)
72 ci--;
73 break;
74 case KEY_RIGHT:
75 if ((size_t)ci < UTIL_SIZE(colors))
76 ci++;
77 break;
78 case KEY_SPACE:
79 if (style == FONT_STYLE_ANTIALIASED)
80 style = FONT_STYLE_NONE;
81 else
82 style = FONT_STYLE_ANTIALIASED;
83
84 theme_default()->fonts[THEME_FONT_INTERFACE]->style = style;
85 default:
86 break;
87 }
88 break;
89 case EVENT_QUIT:
90 game_quit();
91 break;
92 default:
93 break;
94 }
95 }
96
97 static void
98 draw(struct state *st)
99 {
100 (void)st;
101
102 struct font *font = theme_default()->fonts[THEME_FONT_INTERFACE];
103 struct texture tex;
104
105 painter_set_color(0xffffffff);
106 painter_clear();
107
108 if (font_render(font, &tex, "Example of text. Use <Left>/<Right> to change color and <Space> to toggle antialiasing.", colors[ci]) < 0)
109 panic();
110
111 texture_draw(&tex, 10, 10);
112 painter_present();
113 texture_finish(&tex);
114 }
115
116 static void
117 run(void)
118 {
119 struct state state = {
120 .handle = handle,
121 .draw = draw
122 };
123
124 game_init(states, UTIL_SIZE(states));
125 game_push(&state);
126 game_loop();
127 }
128
129 static void
130 quit(void)
131 {
132 window_finish();
133 ui_finish();
134 core_finish();
135 }
136
137 int
138 main(int argc, char **argv)
139 {
140 (void)argc;
141 (void)argv;
142
143 init();
144 run();
145 quit();
146
147 return 0;
148 }