-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreGameState.cpp
More file actions
103 lines (85 loc) · 2.76 KB
/
ScoreGameState.cpp
File metadata and controls
103 lines (85 loc) · 2.76 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
95
96
97
98
99
100
101
102
103
#include "ScoreGameState.h"
#include <stdio.h>
#include <SDL2/SDL_ttf.h>
bool ScoreGameState::Init( SDL_Renderer* renderer )
{
gameStateRequest = -1; // -1 = no request
backgroundTexture.LoadFromFile( renderer, "images/backgroundScore.png" );
// Initialise titleTexture
TTF_Font* loadedFont = TTF_OpenFont( "the_first_fontstruction.ttf", 48 );
if( loadedFont == NULL )
{
printf( "Failed to load font. SDL_ttf error: %s\n", TTF_GetError() );
printf( "Failed to load media for score state.\n" );
return false;
}
if( !headingTexture.LoadFromRenderedText( renderer, "Hi-Scores", { 0xFF, 0xFF, 0xFF, 0x00 }, loadedFont ) )
return false;
TTF_CloseFont( loadedFont );
loadedFont = NULL;
// Initialise board
if ( !board.LoadScoreData( "score.bin" ) )
return false;
board.SetPlayerScore( newScore );
if ( board.GetPlayerScore() != -1 )
board.RankPlayerScore();
board.LoadTextures( renderer );
return true;
}
void ScoreGameState::Cleanup()
{
}
void ScoreGameState::HandleEvent( SDL_Event& e )
{
if ( e.type == SDL_KEYDOWN )
{
switch ( e.key.keysym.sym )
{
case SDLK_ESCAPE:
printf( "Key press Esc\n" );
if ( !board.PlayerBeingRanked() )
gameStateRequest = GAME_TITLE;
break;
case SDLK_RETURN:
printf( "Key press return\n" );
if ( board.PlayerBeingRanked() )
board.CharEnter( '.' );
else
gameStateRequest = GAME_TITLE;
break;
case SDLK_BACKSPACE:
printf( "Key press backspace\n" );
if ( board.PlayerBeingRanked() )
board.CharBack();
break;
default:
if ( board.PlayerBeingRanked() )
{
SDL_Keycode code = e.key.keysym.sym;
if ( code >= SDLK_0 && code <= SDLK_9 )
{
printf( "Key press number\n" );
board.CharEnter( code - SDLK_0 + '0' );
}
else if ( code >= SDLK_a && code <= SDLK_z )
{
printf( "Key press letter\n" );
board.CharEnter( code - SDLK_a + 'A' );
}
}
break;
}
}
}
void ScoreGameState::Update()
{
board.UpdateCursor();
}
void ScoreGameState::Render( SDL_Renderer* renderer )
{
SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0x99, 0xFF );
SDL_RenderClear( renderer );
backgroundTexture.Render( renderer, 0, 0 );
headingTexture.Render( renderer, 50, 25 );
board.Render( renderer, 90, 90 );
}