Mercurial > molko
comparison libadventure/adventure/mainmenu_state.c @ 121:789b23e01f52
misc: reorganize hierarchy, closes #2490
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 05 Oct 2020 13:25:06 +0200 |
parents | src/adventure/mainmenu_state.c@c7e993455985 |
children | c679e08b32b2 |
comparison
equal
deleted
inserted
replaced
120:b3429b26d60d | 121:789b23e01f52 |
---|---|
1 /* | |
2 * mainmenu_state.c -- game main 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 <assert.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 | |
23 #include <core/event.h> | |
24 #include <core/font.h> | |
25 #include <core/game.h> | |
26 #include <core/image.h> | |
27 #include <core/map_state.h> | |
28 #include <core/painter.h> | |
29 #include <core/panic.h> | |
30 #include <core/state.h> | |
31 #include <core/sys.h> | |
32 #include <core/texture.h> | |
33 #include <core/window.h> | |
34 | |
35 #include "mainmenu_state.h" | |
36 #include "splashscreen_state.h" | |
37 | |
38 #define SPEED 120 | |
39 #define SEC 1000 | |
40 | |
41 enum substate { | |
42 SUBSTATE_MOVING, | |
43 SUBSTATE_WAITING | |
44 }; | |
45 | |
46 static int x; | |
47 static int y; | |
48 static unsigned int selection; | |
49 static int destination; | |
50 static enum substate substate; | |
51 static struct texture image; | |
52 | |
53 /* Menu items. */ | |
54 static struct { | |
55 struct texture texture; | |
56 int x; | |
57 int y; | |
58 } items[3]; | |
59 | |
60 static void | |
61 enter(void) | |
62 { | |
63 struct font font = { 0 }; | |
64 | |
65 if (!font_open(&font, sys_datapath("fonts/pirata-one.ttf"), 30)) | |
66 panic(); | |
67 | |
68 substate = SUBSTATE_MOVING; | |
69 x = splashscreen_state_data.x; | |
70 y = splashscreen_state_data.y; | |
71 destination = window.h / 4; | |
72 | |
73 /* TODO: change continue color if no game exists. */ | |
74 font_render(&font, &items[0].texture, "New game"); | |
75 items[0].x = (window.w / 2) - (items[0].texture.w / 2); | |
76 items[0].y = window.h * 0.75; | |
77 | |
78 font_render(&font, &items[1].texture, "Continue"); | |
79 items[1].x = items[0].x; | |
80 items[1].y = items[0].y + items[0].texture.h; | |
81 | |
82 font_render(&font, &items[2].texture, "Quit"); | |
83 items[2].x = items[0].x; | |
84 items[2].y = items[1].y + items[1].texture.h; | |
85 | |
86 font_finish(&font); | |
87 } | |
88 | |
89 static void | |
90 new(void) | |
91 { | |
92 /* TODO: convenient map_state_start function? */ | |
93 | |
94 /* Prepare map. */ | |
95 if (!map_data_open(&map_state_data.map.data, sys_datapath("maps/test.map"))) | |
96 panic(); | |
97 if (!map_init(&map_state_data.map.map, &map_state_data.map.data)) | |
98 panic(); | |
99 | |
100 /* Prepare image and sprite. */ | |
101 if (!(image_open(&image, sys_datapath("sprites/test-walk.png")))) | |
102 panic(); | |
103 | |
104 sprite_init(&map_state_data.player.sprite, &image, 48, 48); | |
105 game_switch(&map_state, false); | |
106 } | |
107 | |
108 static void | |
109 resume(void) | |
110 { | |
111 } | |
112 | |
113 static void | |
114 quit(void) | |
115 { | |
116 game_quit(); | |
117 } | |
118 | |
119 static void | |
120 perform(void) | |
121 { | |
122 assert(selection < 3); | |
123 | |
124 static void (*handlers[])(void) = { | |
125 [0] = new, | |
126 [1] = resume, | |
127 [2] = quit | |
128 }; | |
129 | |
130 handlers[selection](); | |
131 } | |
132 | |
133 static void | |
134 handle(const union event *event) | |
135 { | |
136 if (substate != SUBSTATE_WAITING) | |
137 return; | |
138 | |
139 switch (event->type) { | |
140 case EVENT_KEYDOWN: | |
141 switch (event->key.key) { | |
142 case KEY_UP: | |
143 selection = selection == 0 ? 2 : selection - 1; | |
144 break; | |
145 case KEY_DOWN: | |
146 selection = (selection + 1) % 3; | |
147 break; | |
148 case KEY_ENTER: | |
149 perform(); | |
150 default: | |
151 break; | |
152 } | |
153 break; | |
154 default: | |
155 break; | |
156 } | |
157 } | |
158 | |
159 static void | |
160 update(unsigned int ticks) | |
161 { | |
162 switch (substate) { | |
163 case SUBSTATE_MOVING: | |
164 y -= SPEED * ticks / SEC; | |
165 | |
166 if (y <= destination) { | |
167 y = destination; | |
168 substate = SUBSTATE_WAITING; | |
169 } | |
170 | |
171 break; | |
172 default: | |
173 break; | |
174 } | |
175 } | |
176 | |
177 static void | |
178 draw(void) | |
179 { | |
180 painter_set_color(0xffffffff); | |
181 painter_clear(); | |
182 texture_draw(&splashscreen_state_data.text, x, y); | |
183 | |
184 if (substate == SUBSTATE_WAITING) { | |
185 texture_draw(&items[0].texture, items[0].x, items[0].y); | |
186 texture_draw(&items[1].texture, items[1].x, items[1].y); | |
187 texture_draw(&items[2].texture, items[2].x, items[2].y); | |
188 | |
189 /* Selection cursor. */ | |
190 | |
191 /* TODO: a sword here. */ | |
192 painter_set_color(0x000000ff); | |
193 painter_draw_rectangle(items[selection].x - 30, | |
194 items[selection].y + 11, 15, 15); | |
195 } | |
196 } | |
197 | |
198 static void | |
199 leave(void) | |
200 { | |
201 texture_finish(&items[0].texture); | |
202 texture_finish(&items[1].texture); | |
203 memset(items, 0, sizeof (items)); | |
204 } | |
205 | |
206 struct state mainmenu_state = { | |
207 .enter = enter, | |
208 .handle = handle, | |
209 .update = update, | |
210 .draw = draw, | |
211 .leave = leave | |
212 }; |