Mercurial > molko
comparison examples/example-gridmenu/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-gridmenu.c@133926e08d6e |
children | 76afe639fd72 |
comparison
equal
deleted
inserted
replaced
208:c0e0d4accae8 | 209:23a844fdc911 |
---|---|
1 /* | |
2 * example-gridmenu.c -- show how to use grid menu | |
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/trace.h> | |
27 #include <core/util.h> | |
28 #include <core/window.h> | |
29 | |
30 #include <ui/align.h> | |
31 #include <ui/gridmenu.h> | |
32 #include <ui/label.h> | |
33 #include <ui/theme.h> | |
34 #include <ui/ui.h> | |
35 | |
36 #define W (1280) | |
37 #define H (720) | |
38 | |
39 static void | |
40 init(void) | |
41 { | |
42 if (!core_init() || !ui_init()) | |
43 panic(); | |
44 if (!window_open("Example - Grid menu", W, H)) | |
45 panic(); | |
46 } | |
47 | |
48 static void | |
49 quit(void) | |
50 { | |
51 window_finish(); | |
52 ui_finish(); | |
53 core_finish(); | |
54 } | |
55 | |
56 static void | |
57 handle(struct state *st, const union event *ev) | |
58 { | |
59 switch (ev->type) { | |
60 case EVENT_QUIT: | |
61 game_quit(); | |
62 break; | |
63 default: | |
64 gridmenu_handle(st->data, ev); | |
65 break; | |
66 } | |
67 } | |
68 | |
69 static void | |
70 update(struct state *st, unsigned int ticks) | |
71 { | |
72 (void)ticks; | |
73 | |
74 struct gridmenu *menu = st->data; | |
75 | |
76 if (menu->state == GRIDMENU_STATE_ACTIVATED) { | |
77 tracef("selected index: %u", (unsigned int)menu->selected); | |
78 gridmenu_reset(menu); | |
79 } | |
80 } | |
81 | |
82 static void | |
83 draw(struct state *st) | |
84 { | |
85 painter_set_color(0x4f8fbaff); | |
86 painter_clear(); | |
87 gridmenu_draw(st->data); | |
88 painter_present(); | |
89 } | |
90 | |
91 static void | |
92 run(void) | |
93 { | |
94 struct gridmenu menu = { | |
95 .menu = { | |
96 "Feu mineur", | |
97 "Feu majeur", | |
98 "Feu septième", | |
99 "Glace mineure", | |
100 "Glace majeure", | |
101 "Glace septième", | |
102 "Foudre mineure", | |
103 "Foudre majeure", | |
104 "Foudre septième", | |
105 "Choc mineur", | |
106 "Choc majeur", | |
107 "Choc septième", | |
108 }, | |
109 .w = 300, | |
110 .h = 100, | |
111 .nrows = 3, | |
112 .ncols = 2 | |
113 }; | |
114 struct state state = { | |
115 .data = &menu, | |
116 .handle = handle, | |
117 .update = update, | |
118 .draw = draw, | |
119 }; | |
120 | |
121 align(ALIGN_CENTER, &menu.x, &menu.y, menu.w, menu.h, 0, 0, W, H); | |
122 | |
123 /* Need to repaint at least once. */ | |
124 gridmenu_repaint(&menu); | |
125 | |
126 game_switch(&state, true); | |
127 game_loop(); | |
128 } | |
129 | |
130 int | |
131 main(int argc, char **argv) | |
132 { | |
133 (void)argc; | |
134 (void)argv; | |
135 | |
136 init(); | |
137 run(); | |
138 quit(); | |
139 } |