-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetrisClone.cpp
165 lines (140 loc) · 5.22 KB
/
TetrisClone.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
#include "TetrisClone.h"
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
/* Perform initial set-up for the game */
void TetrisClone::Init( SDL_Renderer* newRenderer )
{
// set game renderer
mainRenderer = newRenderer;
if ( mainRenderer == NULL ) // no renderer set
{
printf( "No valid renderer given. Exiting...\n" );
isRunning = false; // request game to stop running
return;
}
// set the initial game state
currentState.reset( new TitleGameState() );
// initiate game state
if ( !currentState->Init( mainRenderer ) ) // game state could not be initiated
{
printf( "State could not be initiated. Exiting...\n" );
isRunning = false; // request game to stop running
return;
}
fadeAlpha = 0xFF; // set fade state to be fully faded out
fadeLastTime = SDL_GetTicks(); // set time of last fade step to current time
isRunning = true;
}
/* Perform cleanup for the current game state and finish the game */
void TetrisClone::Cleanup()
{
currentState->Cleanup();
isRunning = false; // request game to stop running
}
/* Handle events for the current game state */
void TetrisClone::HandleEvents()
{
while ( SDL_PollEvent( &e ) != 0 ) // there is an event yet to be handled
{
if ( e.type == SDL_QUIT ) // the event is a request to quit
isRunning = false; // request game to stop runnning
else if ( currentState->GetGameStateRequest() == -1 ) // no game state being requested
currentState->HandleEvent( e );
}
}
/* Update the current game state */
void TetrisClone::Update()
{
// Check for game state request
int requestedState = currentState->GetGameStateRequest();
if ( requestedState != -1 ) // request found
{
if ( !FadeOut() && ( SDL_GetTicks() - fadeLastTime > 500 || requestedState == GAME_QUIT ) )
// finished fading out (and pausing, if between states)
{
// set new game state (or quit game) according to request
switch ( requestedState )
{
case GAME_TITLE:
currentState.reset( new TitleGameState() );
currentState->Init( mainRenderer );
break;
case GAME_PLAY:
currentState.reset( new PlayGameState() );
currentState->Init( mainRenderer );
break;
case GAME_SCORE:
currentState.reset( new ScoreGameState( currentState->GetGlobalScore() ) );
currentState->Init( mainRenderer );
break;
case GAME_QUIT:
isRunning = false;
break;
default:
printf( "Requested game state invalid. Exiting...\n" );
isRunning = false;
break;
}
}
}
else // no request
{
currentState->Update();
if ( fadeAlpha > 0x00 ) // not fully faded in
FadeIn();
}
}
/* Render the game */
void TetrisClone::Render()
{
// render the current state
currentState->Render( mainRenderer );
if ( fadeAlpha > 0x00 ) // not fully faded in
{
// render black for fade in/out transition
SDL_SetRenderDrawColor( mainRenderer, 0x00, 0x00, 0x00, fadeAlpha );
SDL_SetRenderDrawBlendMode( mainRenderer, SDL_BLENDMODE_BLEND );
SDL_RenderFillRect( mainRenderer, NULL );
SDL_SetRenderDrawBlendMode( mainRenderer, SDL_BLENDMODE_NONE );
}
// display final image
SDL_RenderPresent( mainRenderer );
}
/* Step fade value towards fading in to the current state */
bool TetrisClone::FadeIn()
{
if ( fadeAlpha == 0x00 ) // already fully faded in
return false; // confirm - no longer fading in
if ( fadeAlpha == 0xFF ) // first fade step
// update time of last fade step
fadeLastTime = SDL_GetTicks() - fadeDelay;
// get time passed since last fade step
int timePassed = SDL_GetTicks() - fadeLastTime;
if ( timePassed >= fadeDelay ) // sufficient time has passed
{
// step fade value according to time passed, and within lower bound
fadeAlpha = max( fadeAlpha - timePassed / fadeDelay, 0x00 );
fadeLastTime += timePassed; // record time of fade step
}
return true; // confirm - still fading in
}
/* Step fade value towards fading out of the current state */
bool TetrisClone::FadeOut()
{
if ( fadeAlpha == 0xFF ) // already fully faded out
return false; // confirm - no longer fading out
if ( fadeAlpha == 0x00 ) // first fade step
// update time of last fade step
fadeLastTime = SDL_GetTicks() - fadeDelay;
// get time passed since last fade step
int timePassed = SDL_GetTicks() - fadeLastTime;
if ( timePassed >= fadeDelay ) // sufficient time has passed
{
// step fade value according to time passed, and within upper bound
fadeAlpha = min( fadeAlpha + timePassed / fadeDelay, 0xFF );
fadeLastTime += timePassed; // record time of fade step
}
return true; // confirm - still fading out
}