-
Notifications
You must be signed in to change notification settings - Fork 0
/
title_screen.c
457 lines (385 loc) · 12.3 KB
/
title_screen.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <jo/jo.h>
#include "main.h"
#include "assets/assets.h"
#include "title_screen.h"
#include "objects/disasteroid.h"
#include "objects/ship.h"
#include "objects/star.h"
// globals for menu options
int g_TitleScreenChoice = 0;
int g_TitleTypeChoice = 0;
int g_TitleHudChoice = 0;
int g_TitleLivesChoice = 0;
int g_TitleScreenCursorColor = 0;
int g_TitleLives[]= {3, 6, 9, 12, 1};
typedef enum _MENU_OPTIONS
{
TITLE_OPTION_GAME_TYPE = 0,
TITLE_OPTION_GAME_LIVES = 1,
TITLE_OPTION_GAME_HUD = 2,
TITLE_OPTION_GAME_START = 3,
TITLE_OPTION_MAX,
} MENU_OPTIONS;
static void drawTitleRectangle(void);
static void drawTitle(void);
static void drawOptions(void);
static void drawVersion(void);
static void drawMenuCursor(void);
static bool moreThanOnePlayer(void);
//
// Title screen callbacks
//
// initializations for title screen
void titleScreen_init(void)
{
// random ship color for the cursor
int index = jo_random(COUNTOF(g_Assets.randomizedColors)) - 1;
g_TitleScreenCursorColor = g_Assets.randomizedColors[index];
// starting selected option
g_TitleScreenChoice = 0;
g_Game.numLives = g_TitleLives[0];
// reset the asteroids if needed
initTitleScreenDisasteroids();
initStars();
}
// handles input for the title screen
// only player one can control the title screen
void titleScreen_input(void)
{
int titleScreenChoice = 0;
if(g_Game.gameState != GAME_STATE_TITLE_SCREEN)
{
return;
}
titleScreenChoice = g_TitleScreenChoice;
if (jo_is_pad1_key_pressed(JO_KEY_UP))
{
if(g_Game.input.pressedUp == false)
{
titleScreenChoice--;
}
g_Game.input.pressedUp = true;
}
else
{
g_Game.input.pressedUp = false;
}
if (jo_is_pad1_key_pressed(JO_KEY_DOWN))
{
if(g_Game.input.pressedDown == false)
{
titleScreenChoice++;
}
g_Game.input.pressedDown = true;
}
else
{
g_Game.input.pressedDown = false;
}
// keep title screen choice in range
sanitizeValue(&titleScreenChoice, 0, TITLE_OPTION_MAX);
g_TitleScreenChoice = titleScreenChoice;
// select an option here
if (jo_is_pad1_key_pressed(JO_KEY_START) ||
jo_is_pad1_key_pressed(JO_KEY_A) ||
jo_is_pad1_key_pressed(JO_KEY_C) ||
jo_is_pad1_key_pressed(JO_KEY_RIGHT) ||
jo_is_pad1_key_pressed(JO_KEY_LEFT))
{
if(g_Game.input.pressedStart == false)
{
switch(titleScreenChoice)
{
case TITLE_OPTION_GAME_TYPE:
{
if(jo_is_pad1_key_pressed(JO_KEY_LEFT))
{
g_TitleTypeChoice--;
}
else
{
g_TitleTypeChoice++;
}
sanitizeValue(&g_TitleTypeChoice, 0, GAME_TYPE_MAX);
g_Game.gameType = g_TitleTypeChoice;
break;
}
case TITLE_OPTION_GAME_LIVES:
{
if(jo_is_pad1_key_pressed(JO_KEY_LEFT))
{
g_TitleLivesChoice--;
}
else
{
g_TitleLivesChoice++;
}
sanitizeValue(&g_TitleLivesChoice, 0, COUNTOF(g_TitleLives));
g_Game.numLives = g_TitleLives[g_TitleLivesChoice];
break;
}
case TITLE_OPTION_GAME_HUD:
{
if(jo_is_pad1_key_pressed(JO_KEY_LEFT))
{
g_TitleHudChoice--;
}
else
{
g_TitleHudChoice++;
}
sanitizeValue(&g_TitleHudChoice, 0, COUNTOF(g_Assets.hudColors));
g_Game.hudColor = g_Assets.hudColors[g_TitleHudChoice];
break;
}
case TITLE_OPTION_GAME_START:
{
if(jo_is_pad1_key_pressed(JO_KEY_RIGHT) ||
jo_is_pad1_key_pressed(JO_KEY_LEFT))
{
// ignore left\right on start
break;
}
// prevent game from starting if not enough players for GAME_TYPE_VERSUS
if(g_Game.gameType == GAME_TYPE_VERSUS)
{
bool allowVersus = false;
// make sure we have at least two players for versus mode
allowVersus = moreThanOnePlayer();
if(allowVersus == false)
{
// not enough players
jo_audio_play_sound(&g_Assets.popPCM);
break;
}
// enough players, fall through to game start
}
transitionState(GAME_STATE_GAMEPLAY);
break;
}
}
}
g_Game.input.pressedStart = true;
}
else
{
g_Game.input.pressedStart = false;
}
return;
}
// update callback for the title screens
void titleScreen_update(void)
{
if(g_Game.gameState != GAME_STATE_TITLE_SCREEN)
{
return;
}
updateTitleScreenShips();
updateTitleScreenDisasteroids();
updateStars();
return;
}
// draws the title screen
void titleScreen_draw(void)
{
if(g_Game.gameState != GAME_STATE_TITLE_SCREEN)
{
return;
}
drawStars();
drawOptions();
drawVersion();
drawMenuCursor();
drawTitleScreenShips();
drawTitleScreenDisasteroids();
drawTitle();
return;
}
// used for changing the HUD color
void incrementHudColor(void)
{
g_TitleHudChoice++;
if(g_TitleHudChoice < 0)
{
g_TitleHudChoice = COUNTOF(g_Assets.hudColors) - 1;
}
else if(g_TitleHudChoice > (int)(COUNTOF(g_Assets.hudColors) - 1))
{
g_TitleHudChoice = 0;
}
g_Game.hudColor = g_Assets.hudColors[g_TitleHudChoice];
}
//
// Title screen private functions
//
// returns true if there are two or more controllers connected
static bool moreThanOnePlayer(void)
{
int count = 0;
for(unsigned int i = 0; i < COUNTOF(g_Players); i++)
{
if(!jo_is_input_available(i))
{
// controller not connected
continue;
}
count++;
if(count >= 2)
{
// found at least two connected controllers
return true;
}
}
// didn't find two controllers
return false;
}
//
// title screen draw functions
//
// draw a black rectangle under the title text
// this will mask the Disasteroids and make it easier to read the title text
static void drawTitleRectangle(void)
{
jo_3d_mesh* mesh = g_Assets.rectangle;
int xPos = 0;
int yPos = -70;
jo_3d_push_matrix();
{
jo_3d_camera_look_at(&g_Game.camera);
jo_3d_set_mesh_color(mesh, JO_COLOR_Black);
jo_3d_translate_matrix(xPos, yPos, 0);
jo_3d_mesh_draw(mesh);
}
jo_3d_pop_matrix();
}
// "DISASTEROIDS" title text
static void drawTitle(void)
{
int color = g_Game.hudColor;
int xPos = -129;
int yPos = -58;
int letterSpacing = 22;
int xScale = 4;
int yScale = 6;
// black rectangle under DISASTEROIDS title
// used to mask out disasteroids
drawTitleRectangle();
//
// DISASTEROIDS title
//
drawLetter('D', color, xPos + letterSpacing * 0, yPos, xScale, yScale);
drawLetter('I', color, xPos + letterSpacing * 1, yPos, xScale, yScale);
drawLetter('S', color, xPos + letterSpacing * 2, yPos, xScale, yScale);
drawLetter('A', color, xPos + letterSpacing * 3, yPos, xScale, yScale);
drawLetter('S', color, xPos + letterSpacing * 4, yPos, xScale, yScale);
drawLetter('T', color, xPos + letterSpacing * 5, yPos, xScale, yScale);
drawLetter('E', color, xPos + letterSpacing * 6, yPos, xScale, yScale);
drawLetter('R', color, xPos + letterSpacing * 7, yPos, xScale, yScale);
drawLetter('O', color, xPos + letterSpacing * 8, yPos, xScale, yScale);
drawLetter('I', color, xPos + letterSpacing * 9, yPos, xScale, yScale);
drawLetter('D', color, xPos + letterSpacing * 10, yPos, xScale, yScale);
drawLetter('S', color, xPos + letterSpacing * 11, yPos, xScale, yScale);
}
// Options menu + values
static void drawOptions(void)
{
int color = g_Game.hudColor;
int xPos = 0;
int yPos = 0;
int letterSpacing = 11;
int xScale = 2;
int yScale = 2;
char upperDigit = 0;
char lowerDigit = 0;
//
// game mode (coop or vs)
//
xPos = 11;
yPos = 0;
drawLetter('M', color, xPos - letterSpacing * 4, yPos, xScale, yScale);
drawLetter('O', color, xPos - letterSpacing * 3, yPos, xScale, yScale);
drawLetter('D', color, xPos - letterSpacing * 2, yPos, xScale, yScale);
drawLetter(':', color, xPos - letterSpacing * 1, yPos, xScale, yScale);
if(g_Game.gameType == GAME_TYPE_COOP)
{
drawLetter('C', color, xPos - letterSpacing * 0, yPos, xScale, yScale);
drawLetter('O', color, xPos - letterSpacing * -1, yPos, xScale, yScale);
}
else if(g_Game.gameType == GAME_TYPE_VERSUS)
{
drawLetter('V', color, xPos - letterSpacing * 0, yPos, xScale, yScale);
drawLetter('S', color, xPos - letterSpacing * -1, yPos, xScale, yScale);
}
//
// tries (num lives, 1,3,6,9,12)
//
yPos = 15;
drawLetter('T', color, xPos - letterSpacing * 4, yPos, xScale, yScale);
drawLetter('R', color, xPos - letterSpacing * 3, yPos, xScale, yScale);
drawLetter('Y', color, xPos - letterSpacing * 2, yPos, xScale, yScale);
drawLetter(':', color, xPos - letterSpacing * 1, yPos, xScale, yScale);
// display the number of lives value
upperDigit = (g_Game.numLives / 10) + '0';
lowerDigit = (g_Game.numLives % 10) + '0';
drawLetter(upperDigit, color, xPos - letterSpacing * 0, yPos, xScale, yScale);
drawLetter(lowerDigit, color, xPos - letterSpacing * -1, yPos, xScale, yScale);
//
// HUD color
//
yPos = 30;
drawLetter('H', color, xPos - letterSpacing * 4, yPos, xScale, yScale);
drawLetter('U', color, xPos - letterSpacing * 3, yPos, xScale, yScale);
drawLetter('D', color, xPos - letterSpacing * 2, yPos, xScale, yScale);
drawLetter(':', color, xPos - letterSpacing * 1, yPos, xScale, yScale);
// display the HUD value
upperDigit = ((g_TitleHudChoice + 1) / 10) + '0';
lowerDigit = ((g_TitleHudChoice + 1) % 10) + '0';
drawLetter(upperDigit, color, xPos - letterSpacing * 0, yPos, xScale, yScale);
drawLetter(lowerDigit, color, xPos - letterSpacing * -1, yPos, xScale, yScale);
//
// START
//
xPos += 6;
yPos = 45;
drawLetter('S', color, xPos - letterSpacing * 4, yPos, xScale, yScale);
drawLetter('T', color, xPos - letterSpacing * 3, yPos, xScale, yScale);
drawLetter('A', color, xPos - letterSpacing * 2, yPos, xScale, yScale);
drawLetter('R', color, xPos - letterSpacing * 1, yPos, xScale, yScale);
drawLetter('T', color, xPos - letterSpacing * 0, yPos, xScale, yScale);
drawLetter('!', color, xPos - letterSpacing * -1, yPos, xScale, yScale);
}
// version text
static void drawVersion(void)
{
int color = g_Game.hudColor;
int xPos = 110;
int yPos = 95;
int letterSpacing = 6;
int xScale = 1;
int yScale = 1;
// dynamically draw the game version
for(unsigned int i = 0; i < sizeof(GAME_VERSION); i++)
{
drawLetter(GAME_VERSION[i], color, xPos + letterSpacing * i, yPos, xScale, yScale);
}
}
// ship used as the menu cursor
static void drawMenuCursor(void)
{
int color = g_TitleScreenCursorColor;
jo_3d_mesh* mesh = g_Assets.cursorShip;
int xPos = 0;
int yPos = 0;
int ang = 90; // face right
xPos = -40;
yPos = (g_TitleScreenChoice * 15) - 4; // vertical position varies based on selection
jo_3d_push_matrix();
{
jo_3d_camera_look_at(&g_Game.camera);
jo_3d_set_mesh_color(mesh, color);
jo_3d_translate_matrix(xPos, yPos, 0);
jo_3d_rotate_matrix(0, 0, ang);
jo_3d_mesh_draw(mesh);
}
jo_3d_pop_matrix();
}