comparison examples/example-cursor/example-cursor.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-cursor.c -- example on how to change cursor
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 <stdio.h>
20
21 #include <core/core.h>
22 #include <core/event.h>
23 #include <core/game.h>
24 #include <core/key.h>
25 #include <core/painter.h>
26 #include <core/panic.h>
27 #include <core/state.h>
28 #include <core/sys.h>
29 #include <core/util.h>
30 #include <core/window.h>
31
32 #include <ui/label.h>
33 #include <ui/ui.h>
34
35 #define W 1280
36 #define H 720
37
38 static struct state *states[1];
39 static char help_text[128];
40 static enum window_cursor cursor = WINDOW_CURSOR_ARROW;
41
42 static struct label help = {
43 .x = 10,
44 .y = 10,
45 .text = help_text,
46 .flags = LABEL_FLAGS_SHADOW
47 };
48
49 static void
50 init(void)
51 {
52 if (core_init("fr.malikania", "example-cursor") < 0 || ui_init() < 0)
53 panic();
54 if (window_open("Example - Cursor", W, H) < 0)
55 panic();
56 }
57
58 static void
59 change(enum window_cursor cursor)
60 {
61 static const char *names[] = {
62 [WINDOW_CURSOR_ARROW] = "WINDOW_CURSOR_ARROW",
63 [WINDOW_CURSOR_EDIT] = "WINDOW_CURSOR_EDIT",
64 [WINDOW_CURSOR_WAIT] = "WINDOW_CURSOR_WAIT",
65 [WINDOW_CURSOR_CROSSHAIR] = "WINDOW_CURSOR_CROSSHAIR",
66 [WINDOW_CURSOR_SIZE] = "WINDOW_CURSOR_SIZE",
67 [WINDOW_CURSOR_NO] = "WINDOW_CURSOR_NO",
68 [WINDOW_CURSOR_HAND] = "WINDOW_CURSOR_HAND"
69 };
70
71 snprintf(help_text, sizeof (help_text), "Keys: <Left>/<Right> to change cursor. Current: %s", names[cursor]);
72 window_set_cursor(cursor);
73 }
74
75 static void
76 handle(struct state *st, const union event *ev)
77 {
78 (void)st;
79
80 switch (ev->type) {
81 case EVENT_KEYDOWN:
82 switch (ev->key.key) {
83 case KEY_LEFT:
84 if (cursor > 0)
85 change(--cursor);
86 break;
87 case KEY_RIGHT:
88 if (cursor + 1 < WINDOW_CURSOR_LAST)
89 change(++cursor);
90 break;
91 default:
92 break;
93 }
94
95
96 break;
97 case EVENT_QUIT:
98 game_quit();
99 break;
100 default:
101 break;
102 }
103 }
104
105 static void
106 draw(struct state *st)
107 {
108 (void)st;
109
110 painter_set_color(0xebede9ff);
111 painter_clear();
112 label_draw(&help);
113 painter_present();
114 }
115
116 static void
117 run(void)
118 {
119 struct state state = {
120 .handle = handle,
121 .draw = draw
122 };
123
124 change(cursor);
125
126 game_init(states, UTIL_SIZE(states));
127 game_push(&state);
128 game_loop();
129 }
130
131 static void
132 quit(void)
133 {
134 window_finish();
135 ui_finish();
136 core_finish();
137 }
138
139 int
140 main(int argc, char **argv)
141 {
142 (void)argc;
143 (void)argv;
144
145 init();
146 run();
147 quit();
148 }