diff --git a/Chapter01/Chapter01-mac.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Chapter01/Chapter01-mac.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
new file mode 100644
index 00000000..18d98100
--- /dev/null
+++ b/Chapter01/Chapter01-mac.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
@@ -0,0 +1,8 @@
+
+
+
+
+ IDEDidComputeMac32BitWarning
+
+
+
diff --git a/Chapter01/Game.cpp b/Chapter01/Game.cpp
index 3b18199e..e9bad05f 100644
--- a/Chapter01/Game.cpp
+++ b/Chapter01/Game.cpp
@@ -10,13 +10,15 @@
const int thickness = 15;
const float paddleH = 100.0f;
+const int numBalls = 2;
Game::Game()
:mWindow(nullptr)
,mRenderer(nullptr)
,mTicksCount(0)
,mIsRunning(true)
-,mPaddleDir(0)
+,mPaddleLeftDir(0)
+,mPaddleRightDir(0)
{
}
@@ -60,12 +62,27 @@ bool Game::Initialize()
return false;
}
//
- mPaddlePos.x = 10.0f;
- mPaddlePos.y = 768.0f/2.0f;
- mBallPos.x = 1024.0f/2.0f;
- mBallPos.y = 768.0f/2.0f;
- mBallVel.x = -200.0f;
- mBallVel.y = 235.0f;
+ mPaddleLeftPos.x = 10.0f;
+ mPaddleLeftPos.y = 768.0f/2.0f;
+
+ mPaddleRightPos.x = 1024.0f - 10.0 - thickness;
+ mPaddleRightPos.y = mPaddleLeftPos.y;
+
+ mBalls = std::vector(numBalls);
+
+ for(int i=0; i (768.0f - paddleH/2.0f - thickness))
+ else if (mPaddleLeftPos.y > (768.0f - paddleH/2.0f - thickness))
{
- mPaddlePos.y = 768.0f - paddleH/2.0f - thickness;
+ mPaddleLeftPos.y = 768.0f - paddleH/2.0f - thickness;
}
}
+
+ if (mPaddleRightDir != 0)
+ {
+ mPaddleRightPos.y += mPaddleRightDir * 300.0f * deltaTime;
+ if (mPaddleRightPos.y < (paddleH/2.0f + thickness))
+ {
+ mPaddleRightPos.y = paddleH/2.0f + thickness;
+ }
+ else if (mPaddleRightPos.y > (768.0f - paddleH/2.0f - thickness))
+ {
+ mPaddleRightPos.y = 768.0f - paddleH/2.0f - thickness;
+ }
+ }
- // Update ball position based on ball velocity
- mBallPos.x += mBallVel.x * deltaTime;
- mBallPos.y += mBallVel.y * deltaTime;
-
- // Bounce if needed
- // Did we intersect with the paddle?
- float diff = mPaddlePos.y - mBallPos.y;
- // Take absolute value of difference
- diff = (diff > 0.0f) ? diff : -diff;
- if (
- // Our y-difference is small enough
- diff <= paddleH / 2.0f &&
- // We are in the correct x-position
- mBallPos.x <= 25.0f && mBallPos.x >= 20.0f &&
- // The ball is moving to the left
- mBallVel.x < 0.0f)
- {
- mBallVel.x *= -1.0f;
- }
- // Did the ball go off the screen? (if so, end game)
- else if (mBallPos.x <= 0.0f)
- {
- mIsRunning = false;
- }
- // Did the ball collide with the right wall?
- else if (mBallPos.x >= (1024.0f - thickness) && mBallVel.x > 0.0f)
- {
- mBallVel.x *= -1.0f;
- }
-
- // Did the ball collide with the top wall?
- if (mBallPos.y <= thickness && mBallVel.y < 0.0f)
- {
- mBallVel.y *= -1;
- }
- // Did the ball collide with the bottom wall?
- else if (mBallPos.y >= (768 - thickness) &&
- mBallVel.y > 0.0f)
- {
- mBallVel.y *= -1;
- }
+ for(auto &ball : mBalls) {
+ // Update ball position based on ball velocity
+ ball.position.x += ball.velocity.x * deltaTime;
+ ball.position.y += ball.velocity.y * deltaTime;
+
+ // Bounce if needed
+ // Did we intersect with the paddle?
+ float diffL = mPaddleLeftPos.y - ball.position.y;
+ float diffR = mPaddleRightPos.y - ball.position.y;
+
+ // Take absolute value of difference
+ diffL = (diffL > 0.0f) ? diffL : -diffL;
+ diffR = (diffR > 0.0f) ? diffR : -diffR;
+ if (
+ // Our y-difference is small enough
+ diffL <= paddleH / 2.0f &&
+ // We are in the correct x-position
+ ball.position.x <= 25.0f && ball.position.x >= 20.0f &&
+ // The ball is moving to the left
+ ball.velocity.x < 0.0f)
+ {
+ ball.velocity.x *= -1.0f;
+ }
+ else if (
+ diffR <= paddleH / 2.0f &&
+ ball.position.x >= 1024.0f - 25.0f && ball.position.x >= 1024.0f - 20.0f &&
+ ball.velocity.x > 0.0f)
+ {
+ ball.velocity.x *= -1.0f;
+ }
+ // Did the ball go off the screen? (if so, end game)
+ else if (ball.position.x <= 0.0f || ball.position.x >= 1024.0f)
+ {
+ mIsRunning = false;
+ }
+
+ // Did the ball collide with the top wall?
+ if (ball.position.y <= thickness && ball.velocity.y < 0.0f)
+ {
+ ball.velocity.y *= -1;
+ }
+ // Did the ball collide with the bottom wall?
+ else if (ball.position.y >= (768 - thickness) &&
+ ball.velocity.y > 0.0f)
+ {
+ ball.velocity.y *= -1;
+ }
+ }
}
void Game::GenerateOutput()
@@ -220,30 +268,33 @@ void Game::GenerateOutput()
wall.y = 768 - thickness;
SDL_RenderFillRect(mRenderer, &wall);
- // Draw right wall
- wall.x = 1024 - thickness;
- wall.y = 0;
- wall.w = thickness;
- wall.h = 1024;
- SDL_RenderFillRect(mRenderer, &wall);
-
// Draw paddle
- SDL_Rect paddle{
- static_cast(mPaddlePos.x),
- static_cast(mPaddlePos.y - paddleH/2),
+ SDL_Rect paddleL{
+ static_cast(mPaddleLeftPos.x),
+ static_cast(mPaddleLeftPos.y - paddleH/2),
thickness,
static_cast(paddleH)
};
- SDL_RenderFillRect(mRenderer, &paddle);
+ SDL_RenderFillRect(mRenderer, &paddleL);
+
+ SDL_Rect paddleR{
+ static_cast(mPaddleRightPos.x),
+ static_cast(mPaddleRightPos.y - paddleH/2),
+ thickness,
+ static_cast(paddleH)
+ };
+ SDL_RenderFillRect(mRenderer, &paddleR);
- // Draw ball
- SDL_Rect ball{
- static_cast(mBallPos.x - thickness/2),
- static_cast(mBallPos.y - thickness/2),
- thickness,
- thickness
- };
- SDL_RenderFillRect(mRenderer, &ball);
+ for(const auto &ball : mBalls) {
+ // Draw ball
+ SDL_Rect ballRect{
+ static_cast(ball.position.x - thickness/2),
+ static_cast(ball.position.y - thickness/2),
+ thickness,
+ thickness
+ };
+ SDL_RenderFillRect(mRenderer, &ballRect);
+ }
// Swap front buffer and back buffer
SDL_RenderPresent(mRenderer);
diff --git a/Chapter01/Game.h b/Chapter01/Game.h
index 3d59a468..29962c6d 100644
--- a/Chapter01/Game.h
+++ b/Chapter01/Game.h
@@ -8,6 +8,7 @@
#pragma once
#include "SDL/SDL.h"
+#include
// Vector2 struct just stores x/y coordinates
// (for now)
@@ -17,6 +18,12 @@ struct Vector2
float y;
};
+struct Ball
+{
+ Vector2 position;
+ Vector2 velocity;
+};
+
// Game class
class Game
{
@@ -44,12 +51,14 @@ class Game
bool mIsRunning;
// Pong specific
- // Direction of paddle
- int mPaddleDir;
+ // Direction of left player paddle
+ int mPaddleLeftDir;
// Position of paddle
- Vector2 mPaddlePos;
- // Position of ball
- Vector2 mBallPos;
- // Velocity of ball
- Vector2 mBallVel;
+ Vector2 mPaddleLeftPos;
+
+ // Direction of right player paddle
+ int mPaddleRightDir;
+ Vector2 mPaddleRightPos;
+
+ std::vector mBalls;
};