-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
187 lines (161 loc) · 6.56 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>
#include <SFML/Graphics.hpp>
const unsigned int kWindowWidth = 600;
const float kBoundaryWidth = 500;
const unsigned int kBottomMargin = 40;
const float kBlockWidth = 25;
const float kBallRadius = 12.5;
const int kMsPerTick = 20;
const struct {
sf::Color mystic_mint{217, 232, 227};
sf::Color nocturnal_expedition{17, 76, 90};
sf::Color ocean_noir{23, 43, 54};
} kColorPalette;
struct Ball {
sf::CircleShape shape;
sf::Vector2f vel;
Ball(const sf::Vector2f& pos, const sf::Vector2f& vel, const sf::Color& color) {
this->shape.setRadius(kBallRadius);
this->shape.setOrigin(kBallRadius, kBallRadius);
this->shape.setPosition(pos);
this->shape.setFillColor(color);
this->vel = vel;
}
};
inline sf::Color getOppoColor(sf::Color& color) {
return color == kColorPalette.mystic_mint
? kColorPalette.nocturnal_expedition
: kColorPalette.mystic_mint;
}
int main(void) {
sf::RenderWindow window(sf::VideoMode(kWindowWidth, kWindowWidth + kBottomMargin), "Pong Wars", sf::Style::Titlebar | sf::Style::Close);
sf::View view(sf::Vector2f(kBoundaryWidth / 2, (kBoundaryWidth + kBottomMargin) / 2), sf::Vector2f(kWindowWidth, kWindowWidth + kBottomMargin));
window.setView(view);
// background
sf::VertexArray bg(sf::Quads, 4);
float gap_width = (kWindowWidth - kBoundaryWidth) / 2.0;
bg[0].position = sf::Vector2f(-gap_width, -gap_width);
bg[1].position = sf::Vector2f(kBoundaryWidth + gap_width, -gap_width);
bg[2].position = sf::Vector2f(kBoundaryWidth + gap_width, kBoundaryWidth + gap_width + kBottomMargin);
bg[3].position = sf::Vector2f(-gap_width, kBoundaryWidth + gap_width + kBottomMargin);
bg[0].color = kColorPalette.ocean_noir;
bg[1].color = kColorPalette.ocean_noir;
bg[2].color = kColorPalette.mystic_mint;
bg[3].color = kColorPalette.mystic_mint;
// blocks
int block_num_per_dim = kBoundaryWidth / kBlockWidth;
std::vector<std::vector<sf::RectangleShape>> blocks(
block_num_per_dim,
std::vector<sf::RectangleShape>(block_num_per_dim)
);
for (int i = 0; i < block_num_per_dim; i++) {
for (int j = 0; j < block_num_per_dim; j++) {
auto& block = blocks[i][j];
block.setSize(sf::Vector2f(kBlockWidth, kBlockWidth));
block.setPosition(i * kBlockWidth, j * kBlockWidth);
if (i < block_num_per_dim / 2) {
block.setFillColor(kColorPalette.mystic_mint);
} else {
block.setFillColor(kColorPalette.nocturnal_expedition);
}
}
}
unsigned int block_num_per_color = block_num_per_dim * block_num_per_dim / 2;
unsigned int block_counts[2] = { block_num_per_color, block_num_per_color };
// balls
std::vector<Ball> balls = {
Ball({100, 20}, {-5, 5}, kColorPalette.nocturnal_expedition),
Ball({350, 230}, {5, 5}, kColorPalette.mystic_mint),
};
// text
sf::Font font;
if (!font.loadFromFile("./ComicMono.ttf")) {
std::cerr << "Failed to load the font!" << std::endl;
return 1;
}
sf::Text info;
info.setFont(font);
info.setCharacterSize(16);
info.setFillColor(kColorPalette.ocean_noir);
info.setString("day xxx | night xxx"); // placeholder
float text_x = (kBoundaryWidth - info.getGlobalBounds().getSize().x) / 2;
info.setPosition(sf::Vector2f(text_x, kBoundaryWidth + kBottomMargin / 2.0));
sf::Clock clock;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
if (clock.getElapsedTime().asMilliseconds() % kMsPerTick != 0) {
continue;
}
for (int idx = 0; idx < 2; idx++) {
Ball& ball = balls[idx];
sf::Color ball_color = ball.shape.getFillColor();
sf::Vector2f pos = ball.shape.getPosition();
int i = pos.x / kBlockWidth;
int j = pos.y / kBlockWidth;
int left_side_i = (pos.x - kBallRadius) / kBlockWidth;
int right_side_i = (pos.x + kBallRadius) / kBlockWidth;
int top_side_j = (pos.y - kBallRadius) / kBlockWidth;
int bottom_side_j = (pos.y + kBallRadius) / kBlockWidth;
if (pos.x - kBallRadius < 0 || pos.x + kBallRadius >= kBoundaryWidth) {
ball.vel.x *= -1;
} else {
auto& left_block = blocks[left_side_i][j];
auto& right_block = blocks[right_side_i][j];
if (left_block.getFillColor() == ball_color) {
left_block.setFillColor(getOppoColor(ball_color));
ball.vel.x *= -1;
block_counts[idx]--;
block_counts[idx ^ 1]++;
}
if (right_block.getFillColor() == ball_color) {
right_block.setFillColor(getOppoColor(ball_color));
ball.vel.x *= -1;
block_counts[idx]--;
block_counts[idx ^ 1]++;
}
}
if (pos.y - kBallRadius < 0 || pos.y + kBallRadius >= kBoundaryWidth) {
ball.vel.y *= -1;
} else {
auto& top_block = blocks[i][top_side_j];
auto& bottom_block = blocks[i][bottom_side_j];
if (top_block.getFillColor() == ball_color) {
top_block.setFillColor(getOppoColor(ball_color));
ball.vel.y *= -1;
block_counts[idx]--;
block_counts[idx ^ 1]++;
}
if (bottom_block.getFillColor() == ball_color) {
bottom_block.setFillColor(getOppoColor(ball_color));
ball.vel.y *= -1;
block_counts[idx]--;
block_counts[idx ^ 1]++;
}
}
ball.shape.move(ball.vel.x, ball.vel.y);
}
std::stringstream ss;
ss << "day " << std::setw(3) << block_counts[1] << " | " << "night " << std::setw(3) << block_counts[0];
info.setString(ss.str());
window.draw(bg);
for (int i = 0; i < block_num_per_dim; i++) {
for (int j = 0; j < block_num_per_dim; j++) {
window.draw(blocks[i][j]);
}
}
for (Ball& ball : balls) {
window.draw(ball.shape);
}
window.draw(info);
window.display();
}
return 0;
}