-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
94 lines (78 loc) · 2.32 KB
/
Menu.cpp
File metadata and controls
94 lines (78 loc) · 2.32 KB
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
#include "Menu.h"
#include <stdio.h>
Menu::Menu() : currentOption( MENU_PLAY )
{
menuFont = TTF_OpenFont( "the_first_fontstruction.ttf", 28 );
if( menuFont == NULL )
{
printf( "Failed to load font. SDL_ttf error: %s\n", TTF_GetError() );
printf( "Failed to load media for title state.\n" );
}
}
Menu::~Menu()
{
TTF_CloseFont( menuFont );
}
void Menu::LoadTextures( SDL_Renderer* renderer )
{
// Load arrow texture
arrowTexture.LoadFromFile( renderer, "images/arrow.png", { 0x00, 0x80, 0x80, 0xFF } );
// Load text textures
char buffer[12];
SDL_Color textColor = { 0xFF, 0xFF, 0xFF, 0x00 };
textTextures = std::vector<Texture>( 3 );
sprintf( buffer, "Play" );
if( !textTextures[0].LoadFromRenderedText( renderer, buffer, textColor, menuFont ) )
{
textTextures.clear();
return;
}
sprintf( buffer, "View scores" );
if( !textTextures[1].LoadFromRenderedText( renderer, buffer, textColor, menuFont ) )
{
textTextures.clear();
return;
}
sprintf( buffer, "Quit" );
if( !textTextures[2].LoadFromRenderedText( renderer, buffer, textColor, menuFont ) )
{
textTextures.clear();
return;
}
}
void Menu::ChangeOption( int moveDirection )
{
switch ( currentOption )
{
case MENU_PLAY:
if ( moveDirection == MOVE_UP )
currentOption = MENU_QUIT;
if ( moveDirection == MOVE_DOWN )
currentOption = MENU_SCORE;
break;
case MENU_SCORE:
if ( moveDirection == MOVE_UP )
currentOption = MENU_PLAY;
else if ( moveDirection == MOVE_DOWN )
currentOption = MENU_QUIT;
break;
case MENU_QUIT:
if ( moveDirection == MOVE_UP )
currentOption = MENU_SCORE;
else if ( moveDirection == MOVE_DOWN )
currentOption = MENU_PLAY;
break;
default:
break;
}
}
void Menu::Render( SDL_Renderer* renderer, int x, int y )
{
// Render arrow
arrowTexture.Render( renderer, x, y + 5 + currentOption * lineSpacing );
// Render text
for ( unsigned int i = 0; i < textTextures.size(); i++ )
{
textTextures[i].Render( renderer, x + 39, y + i * lineSpacing );
}
}