forked from vittorioromeo/cppcon2014
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp08.cpp
More file actions
253 lines (202 loc) · 7.65 KB
/
Copy pathp08.cpp
File metadata and controls
253 lines (202 loc) · 7.65 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) 2014 Vittorio Romeo
// License: MIT License | http://opensource.org/licenses/MIT
// http://vittorioromeo.info | vittorio.romeo@outlook.com
// Before completing our game, let's spend some time refactoring
// the code. There is a lot of unnecessary duplication!
#include <SFML/Graphics.hpp>
constexpr unsigned int wndWidth{800}, wndHeight{600};
// First of all, we have trivial code repetition for our "simple
// getters". It is sufficient to create two base classes our objects
// will inherit from: one for rectangles and one for circles.
struct Rectangle
{
sf::RectangleShape shape;
float x() const noexcept { return shape.getPosition().x; }
float y() const noexcept { return shape.getPosition().y; }
float width() const noexcept { return shape.getSize().x; }
float height() const noexcept { return shape.getSize().y; }
float left() const noexcept { return x() - width() / 2.f; }
float right() const noexcept { return x() + width() / 2.f; }
float top() const noexcept { return y() - height() / 2.f; }
float bottom() const noexcept { return y() + height() / 2.f; }
};
struct Circle
{
sf::CircleShape shape;
float x() const noexcept { return shape.getPosition().x; }
float y() const noexcept { return shape.getPosition().y; }
float radius() const noexcept { return shape.getRadius(); }
float left() const noexcept { return x() - radius(); }
float right() const noexcept { return x() + radius(); }
float top() const noexcept { return y() - radius(); }
float bottom() const noexcept { return y() + radius(); }
};
// Let's adapt our classes to the new hierarchy.
class Ball : public Circle
{
public:
static const sf::Color defColor;
static constexpr float defRadius{10.f};
static constexpr float defVelocity{8.f};
sf::Vector2f velocity{-defVelocity, -defVelocity};
Ball(float mX, float mY)
{
shape.setPosition(mX, mY);
shape.setRadius(defRadius);
shape.setFillColor(defColor);
shape.setOrigin(defRadius, defRadius);
}
void update()
{
shape.move(velocity);
solveBoundCollisions();
}
void draw(sf::RenderWindow& mTarget) { mTarget.draw(shape); }
private:
void solveBoundCollisions() noexcept
{
if(left() < 0) velocity.x = defVelocity;
else if(right() > wndWidth) velocity.x = -defVelocity;
if(top() < 0) velocity.y = defVelocity;
else if(bottom() > wndHeight) velocity.y = -defVelocity;
}
};
const sf::Color Ball::defColor{sf::Color::Red};
class Paddle : public Rectangle
{
public:
static const sf::Color defColor;
static constexpr float defWidth{60.f};
static constexpr float defHeight{20.f};
static constexpr float defVelocity{8.f};
sf::Vector2f velocity;
Paddle(float mX, float mY)
{
shape.setPosition(mX, mY);
shape.setSize({defWidth, defHeight});
shape.setFillColor(defColor);
shape.setOrigin(defWidth / 2.f, defHeight / 2.f);
}
void update()
{
processPlayerInput();
shape.move(velocity);
}
void draw(sf::RenderWindow& mTarget) { mTarget.draw(shape); }
private:
void processPlayerInput()
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)
&& left() > 0) velocity.x = -defVelocity;
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)
&& right() < wndWidth) velocity.x = defVelocity;
else velocity.x = 0;
}
};
const sf::Color Paddle::defColor{sf::Color::Red};
class Brick : public Rectangle
{
public:
static const sf::Color defColor;
static constexpr float defWidth{60.f};
static constexpr float defHeight{20.f};
static constexpr float defVelocity{8.f};
bool destroyed{false};
Brick(float mX, float mY)
{
shape.setPosition(mX, mY);
shape.setSize({defWidth, defHeight});
shape.setFillColor(defColor);
shape.setOrigin(defWidth / 2.f, defHeight / 2.f);
}
void update() { }
void draw(sf::RenderWindow& mTarget) { mTarget.draw(shape); }
};
const sf::Color Brick::defColor{sf::Color::Yellow};
template<typename T1, typename T2>
bool isIntersecting(const T1& mA, const T2& mB) noexcept
{
return mA.right() >= mB.left() && mA.left() <= mB.right()
&& mA.bottom() >= mB.top() && mA.top() <= mB.bottom();
}
void solvePaddleBallCollision(const Paddle& mPaddle, Ball& mBall) noexcept
{
if(!isIntersecting(mPaddle, mBall)) return;
mBall.velocity.y = -Ball::defVelocity;
mBall.velocity.x = mBall.x() < mPaddle.x() ?
-Ball::defVelocity : Ball::defVelocity;
}
void solveBrickBallCollision(Brick& mBrick, Ball& mBall) noexcept
{
if(!isIntersecting(mBrick, mBall)) return;
mBrick.destroyed = true;
float overlapLeft{mBall.right() - mBrick.left()};
float overlapRight{mBrick.right() - mBall.left()};
float overlapTop{mBall.bottom() - mBrick.top()};
float overlapBottom{mBrick.bottom() - mBall.top()};
bool ballFromLeft(std::abs(overlapLeft) < std::abs(overlapRight));
bool ballFromTop(std::abs(overlapTop) < std::abs(overlapBottom));
float minOverlapX{ballFromLeft ? overlapLeft : overlapRight};
float minOverlapY{ballFromTop ? overlapTop : overlapBottom};
if(std::abs(minOverlapX) < std::abs(minOverlapY))
mBall.velocity.x = ballFromLeft ?
-Ball::defVelocity : Ball::defVelocity;
else
mBall.velocity.y = ballFromTop ?
-Ball::defVelocity : Ball::defVelocity;
}
int main()
{
constexpr int brkCountX{11}, brkCountY{4};
constexpr int brkStartColumn{1}, brkStartRow{2};
constexpr float brkSpacing{3.f}, brkOffsetX{22.f};
Ball ball{wndWidth / 2.f, wndHeight / 2.f};
Paddle paddle{wndWidth / 2, wndHeight - 50};
std::vector<Brick> bricks;
for(int iX{0}; iX < brkCountX; ++iX)
for(int iY{0}; iY < brkCountY; ++iY)
{
float x{(iX + brkStartColumn)
* (Brick::defWidth + brkSpacing)};
float y{(iY + brkStartRow)
* (Brick::defHeight + brkSpacing)};
bricks.emplace_back(brkOffsetX + x, y);
}
sf::RenderWindow window{{wndWidth, wndHeight}, "Arkanoid - 8"};
window.setFramerateLimit(60);
while(true)
{
window.clear(sf::Color::Black);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
break;
ball.update();
paddle.update();
for(auto& brick : bricks)
{
brick.update();
solveBrickBallCollision(brick, ball);
}
bricks.erase(
std::remove_if(std::begin(bricks), std::end(bricks),
[](const auto& mBrick){ return mBrick.destroyed; }),
std::end(bricks)
);
solvePaddleBallCollision(paddle, ball);
ball.draw(window);
paddle.draw(window);
for(auto& brick : bricks) brick.draw(window);
window.display();
}
return 0;
}
// By ignoring the comments and slightly altering the formatting,
// we have actually reached our goal: we created a playable arkanoid
// clone from scratch, in under 200 lines of code.
// We can still highly improve the code architecture, though.
// A good idea to improve flexibility and extensibility of the system
// would be creating a base polymorphic `Entity` class that our
// game objects inherit from. Also a `Game` class that handles
// window management and win/lose conditions, and a `Manager` class
// that will help us create/destroy/store entities.
// {Info: class hierarchy}
// {Info: game architecture}