Skip to content

Commit a52d0fe

Browse files
committed
Code changes
1 parent 0c4f1a9 commit a52d0fe

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

Chapter01/Game.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Game::Game()
2424
bool Game::Initialize()
2525
{
2626
// Initialize SDL
27-
int sdlResult = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
27+
int sdlResult = SDL_Init(SDL_INIT_VIDEO);
2828
if (sdlResult != 0)
2929
{
3030
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
@@ -47,7 +47,7 @@ bool Game::Initialize()
4747
return false;
4848
}
4949

50-
// Create SDL renderer
50+
//// Create SDL renderer
5151
mRenderer = SDL_CreateRenderer(
5252
mWindow, // Window to create renderer for
5353
-1, // Usually -1
@@ -59,7 +59,7 @@ bool Game::Initialize()
5959
SDL_Log("Failed to create renderer: %s", SDL_GetError());
6060
return false;
6161
}
62-
62+
//
6363
mPaddlePos.x = 10.0f;
6464
mPaddlePos.y = 768.0f/2.0f;
6565
mBallPos.x = 1024.0f/2.0f;
@@ -82,25 +82,26 @@ void Game::RunLoop()
8282
void Game::ProcessInput()
8383
{
8484
SDL_Event event;
85-
// Use poll event to figure out if user
86-
// is trying to quit
8785
while (SDL_PollEvent(&event))
8886
{
8987
switch (event.type)
9088
{
89+
// If we get an SDL_QUIT event, end loop
9190
case SDL_QUIT:
9291
mIsRunning = false;
9392
break;
9493
}
9594
}
9695

9796
// Get state of keyboard
98-
const Uint8 *state = SDL_GetKeyboardState(NULL);
97+
const Uint8* state = SDL_GetKeyboardState(NULL);
98+
// If escape is pressed, also end loop
9999
if (state[SDL_SCANCODE_ESCAPE])
100100
{
101101
mIsRunning = false;
102102
}
103103

104+
// Update paddle direction based on W/S keys
104105
mPaddleDir = 0;
105106
if (state[SDL_SCANCODE_W])
106107
{
@@ -114,7 +115,6 @@ void Game::ProcessInput()
114115

115116
void Game::UpdateGame()
116117
{
117-
// Compute delta time
118118
// Wait until 16ms has elapsed since last frame
119119
while (!SDL_TICKS_PASSED(SDL_GetTicks(), mTicksCount + 16))
120120
;
@@ -136,6 +136,7 @@ void Game::UpdateGame()
136136
if (mPaddleDir != 0)
137137
{
138138
mPaddlePos.y += mPaddleDir * 300.0f * deltaTime;
139+
// Make sure paddle doesn't move off screen!
139140
if (mPaddlePos.y < (paddleH/2.0f + thickness))
140141
{
141142
mPaddlePos.y = paddleH/2.0f + thickness;
@@ -158,7 +159,6 @@ void Game::UpdateGame()
158159
&& mBallVel.x < 0.0f)
159160
{
160161
mBallVel.x *= -1.0f;
161-
mBallPos.x = 26.0f;
162162
}
163163
// Did the ball go off the screen? (if so, end game)
164164
else if (mBallPos.x <= 0.0f)
@@ -169,35 +169,35 @@ void Game::UpdateGame()
169169
else if (mBallPos.x >= (1024.0f - thickness) && mBallVel.x > 0.0f)
170170
{
171171
mBallVel.x *= -1.0f;
172-
mBallPos.x = 1024.0f - thickness;
173172
}
174173

175174
// Did the ball collide with the top wall?
176175
if (mBallPos.y <= thickness && mBallVel.y < 0.0f)
177176
{
178177
mBallVel.y *= -1;
179-
mBallPos.y = thickness;
180178
}
181-
// Did the ball collide with the right wall?
179+
// Did the ball collide with the bottom wall?
182180
else if (mBallPos.y >= (768 - thickness) &&
183181
mBallVel.y > 0.0f)
184182
{
185183
mBallVel.y *= -1;
186-
mBallPos.y = 768 - thickness;
187184
}
188185
}
189186

190187
void Game::GenerateOutput()
191188
{
189+
// Set draw color to blue
192190
SDL_SetRenderDrawColor(
193191
mRenderer,
194192
0, // R
195193
0, // G
196194
255, // B
197195
255 // A
198196
);
199-
SDL_RenderClear(mRenderer);
200197

198+
// Clear back buffer
199+
SDL_RenderClear(mRenderer);
200+
201201
// Draw walls
202202
SDL_SetRenderDrawColor(mRenderer, 255, 255, 255, 255);
203203

@@ -231,14 +231,15 @@ void Game::GenerateOutput()
231231
SDL_RenderFillRect(mRenderer, &paddle);
232232

233233
// Draw ball
234-
SDL_Rect ball{
234+
SDL_Rect ball{
235235
static_cast<int>(mBallPos.x - thickness/2),
236236
static_cast<int>(mBallPos.y - thickness/2),
237237
thickness,
238238
thickness
239239
};
240240
SDL_RenderFillRect(mRenderer, &ball);
241241

242+
// Swap front buffer and back buffer
242243
SDL_RenderPresent(mRenderer);
243244
}
244245

Chapter01/Game.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Game
4040
SDL_Renderer* mRenderer;
4141
// Number of ticks since start of game
4242
Uint32 mTicksCount;
43-
// Game is still running
43+
// Game should continue to run
4444
bool mIsRunning;
4545

4646
// Pong specific

0 commit comments

Comments
 (0)