generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuState.cpp
280 lines (238 loc) · 6.84 KB
/
MenuState.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* MenuState.cpp - part of 32Blox (revised edition!)
*
* Copyright (C) 2020 Pete Favelle <32blit@ahnlak.com>
*
* This file is released under the MIT License; see LICENSE for details
*
* The Menu state is an out-of-machine state used to handle the in-game menu;
* it overrides all other states, and as such is handled by the top level
* update/render functions as a whole different thing.
*
* The practical upshot of this is that it pauses the general game whenever
* it's active, which is what you want from an in-game menu!
*/
/* System headers. */
#include <string.h>
/* Local headers. */
#include "32blit.hpp"
#include "32blox.hpp"
#include "assets_images.hpp"
#include "MenuState.hpp"
/* Functions. */
/*
* constructor - create the contents of this State container.
*/
MenuState::MenuState( void )
{
/* And compute the gradient colours for the background. */
for ( uint16_t i = 0; i < MENUSTATE_GRADIENT_HEIGHT / 2; i++ )
{
gradient_pen[i] = gradient_pen[MENUSTATE_GRADIENT_HEIGHT - i - 1]
= blit::Pen( 40 - i / 2, 10 + i, 30 + i / 2 );
}
/* The font pen will be simpler. */
plain_pen = blit::Pen( 255, 255, 0 );
font_pen = blit::Pen( 255, 255, 0 );
font_tween.init( blit::tween_sine, 255.0f, 100.0f, 500 );
}
/*
* init - called whenever the game engine is switching to this state
*
* GameStateInterface * - the state we were most recently in
*/
void MenuState::init( GameStateInterface *p_previous )
{
/* Set the tweens running. */
font_tween.start();
/* And work out the size of menu entries. */
menu_size = blit::screen.measure_text( "Haptic <OFF>", assets.message_font );
/* And set the cursor to the first option. */
cursor = 0;
/* All done. */
return;
}
/*
* fini - called whenever the game engine turns off this state.
*
* GameStateInterface * - the state we were switching to
*/
void MenuState::fini( GameStateInterface *p_next )
{
/* Stop the tweens. */
font_tween.stop();
/* And we're done. */
return;
}
/*
* update - called every tick (~10ms) to update the state of the game.
*
* uint32_t - the elapsed time (in ms) since the game launched.
*/
gamestate_t MenuState::update( uint32_t p_time )
{
/* In this state, we'll update the background gradient, to make it look */
/* pretty (or at least, moving so it's obvious we haven't crashed) */
if ( MENUSTATE_GRADIENT_HEIGHT < ++gradient_offset )
{
gradient_offset = 0;
}
/* The font pen we use will pulse more subtlely. */
font_pen.g = font_tween.value;
/* Fade any active vibrations. */
if ( blit::vibration > 0.0f )
{
blit::vibration -= 0.05f;
}
if ( blit::vibration < 0.05f )
{
blit::vibration = 0.0f;
}
/* Check for the user pressing up or down, to move the cursor. */
if ( ( blit::buttons.pressed & blit::Button::DPAD_UP ) && ( cursor > 0 ) )
{
blit::vibration = 0.25f;
cursor--;
}
if ( ( blit::buttons.pressed & blit::Button::DPAD_DOWN ) && ( cursor < 2 ) )
{
blit::vibration = 0.25f;
cursor++;
}
/* Left or right just toggles. */
if ( ( blit::buttons.pressed & blit::Button::DPAD_LEFT ) ||
( blit::buttons.pressed & blit::Button::DPAD_RIGHT ) )
{
blit::vibration = 0.2f;
switch( cursor )
{
case 0: /* Sound. */
output.enable_sound( !output.sound_enabled() );
break;
case 1: /* Music. */
output.enable_music( !output.music_enabled() );
break;
case 2: /* Haptic. */
output.enable_haptic( !output.haptic_enabled() );
break;
default: /* Should never be reached. */
break;
}
}
/* For this state, the return value is meaningless. */
return STATE_NONE;
}
/*
* render - called every frame (~20ms) to render the screen.
*
* uint32_t - the elapsed time (in ms) since the game launched.
*/
void MenuState::render( uint32_t p_time )
{
const char *l_charptr;
/* Clear the screen down. */
blit::screen.clear();
/* Draw an animated background gradient, to look pretty. */
for ( uint16_t i = 0; i < blit::screen.bounds.h; i++ )
{
blit::screen.pen = gradient_pen[( i + gradient_offset ) % MENUSTATE_GRADIENT_HEIGHT];
blit::screen.h_span( blit::Point( 0, i ), blit::screen.bounds.w );
}
/* Place the logo in the middle of the screen. */
blit::Point l_pos;
l_pos.x = (blit::screen.bounds.w - assets.surface_long_logo->bounds.w) / 2;
l_pos.y = 10;
blit::screen.blit( assets.surface_long_logo, assets.surface_long_logo->clip, l_pos );
/* Now render all our menu options, highlighting what we're currently on. */
blit::screen.pen = plain_pen;
blit::screen.text(
assets.get_text( STR_MENU_SOUND ),
assets.message_font,
blit::Point( ( blit::screen.bounds.w - menu_size.w ) / 2, 100 ),
true,
blit::TextAlign::center_left
);
if ( output.sound_enabled() )
{
l_charptr = assets.get_text( STR_MENU_ON );
}
else
{
l_charptr = assets.get_text( STR_MENU_OFF );
}
blit::screen.pen = ( cursor == 0 ) ? font_pen : plain_pen;
blit::screen.text(
l_charptr,
assets.message_font,
blit::Point( blit::screen.bounds.w / 2, 100 ),
true,
blit::TextAlign::center_left
);
blit::screen.pen = plain_pen;
blit::screen.text(
assets.get_text( STR_MENU_MUSIC ),
assets.message_font,
blit::Point( ( blit::screen.bounds.w - menu_size.w ) / 2, 130 ),
true,
blit::TextAlign::center_left
);
if ( output.music_enabled() )
{
l_charptr = assets.get_text( STR_MENU_ON );
}
else
{
l_charptr = assets.get_text( STR_MENU_OFF );
}
blit::screen.pen = ( cursor == 1 ) ? font_pen : plain_pen;
blit::screen.text(
l_charptr,
assets.message_font,
blit::Point( blit::screen.bounds.w / 2, 130 ),
true,
blit::TextAlign::center_left
);
blit::screen.pen = plain_pen;
blit::screen.text(
assets.get_text( STR_MENU_HAPTIC ),
assets.message_font,
blit::Point( ( blit::screen.bounds.w - menu_size.w ) / 2, 160 ),
true,
blit::TextAlign::center_left
);
if ( output.haptic_enabled() )
{
l_charptr = assets.get_text( STR_MENU_ON );
}
else
{
l_charptr = assets.get_text( STR_MENU_OFF );
}
blit::screen.pen = ( cursor == 2 ) ? font_pen : plain_pen;
blit::screen.text(
l_charptr,
assets.message_font,
blit::Point( blit::screen.bounds.w / 2, 160 ),
true,
blit::TextAlign::center_left
);
blit::screen.pen = plain_pen;
blit::screen.text(
assets.get_text( STR_MENU_TO_EXIT ),
assets.number_font,
blit::Point( blit::screen.bounds.w / 2, 200 ),
true,
blit::TextAlign::bottom_center
);
/* Lastly some gratuitous self-promotion. */
blit::screen.text(
assets.get_text( STR_MENU_URL ),
assets.number_font,
blit::Point( blit::screen.bounds.w / 2, blit::screen.bounds.h - 10 ),
true,
blit::TextAlign::bottom_center
);
/* All done. */
return;
}
/* End of MenuState.cpp */