comparison examples/example-gridmenu/example-gridmenu.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-gridmenu.c -- show how to use grid menu
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/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 struct state *states[1];
40
41 static void
42 init(void)
43 {
44 if (core_init("fr.malikania", "example-gridmenu") < 0 || ui_init() < 0)
45 panic();
46 if (window_open("Example - Grid menu", W, H) < 0)
47 panic();
48 }
49
50 static void
51 quit(void)
52 {
53 window_finish();
54 ui_finish();
55 core_finish();
56 }
57
58 static void
59 handle(struct state *st, const union event *ev)
60 {
61 struct gridmenu *menu = st->data;
62
63 switch (ev->type) {
64 case EVENT_QUIT:
65 game_quit();
66 break;
67 default:
68 if (gridmenu_handle(st->data, ev))
69 tracef("selected index: %zu (%s)", menu->selected, menu->items[menu->selected]);
70 break;
71 }
72 }
73
74 static void
75 draw(struct state *st)
76 {
77 painter_set_color(0x4f8fbaff);
78 painter_clear();
79 gridmenu_draw(st->data);
80 painter_present();
81 }
82
83 static void
84 run(void)
85 {
86 const char * const items[] = {
87 "Feu mineur",
88 "Feu majeur",
89 "Feu septième",
90 "Glace mineure",
91 "Glace majeure",
92 "Glace septième",
93 "Foudre mineure",
94 "Foudre majeure",
95 "Foudre septième",
96 "Choc mineur",
97 "Choc majeur",
98 "Choc septième",
99 "Portée",
100 "Escapade",
101 "Destruction",
102 "Résurrection",
103 "Double tour"
104 };
105
106 struct gridmenu menu = {0};
107 struct state state = {
108 .data = &menu,
109 .handle = handle,
110 .draw = draw,
111 };
112
113 gridmenu_init(&menu, 3, 2, items, UTIL_SIZE(items));
114 gridmenu_resize(&menu, 0, 0, 300, 100);
115
116 align(ALIGN_CENTER, &menu.x, &menu.y, menu.w, menu.h, 0, 0, W, H);
117
118 game_init(states, UTIL_SIZE(states));
119 game_push(&state);
120 game_loop();
121 }
122
123 int
124 main(int argc, char **argv)
125 {
126 (void)argc;
127 (void)argv;
128
129 init();
130 run();
131 quit();
132 }