This commit is contained in:
Love 2024-01-29 10:46:12 +01:00
parent a86a3c170a
commit 62e7eb1e93
3 changed files with 15 additions and 23 deletions

View File

@ -22,14 +22,9 @@ private:
public: public:
explicit Ball(const SDL_Point *screen, const PlayerPaddle *leftPaddle, const PlayerPaddle *rightPaddle, explicit Ball(const SDL_Point *screen, const PlayerPaddle *leftPaddle, const PlayerPaddle *rightPaddle,
Score *score) { Score *score) :
this->score = score; score(score), screen(screen), leftPaddle(leftPaddle), rightPaddle(rightPaddle), x(screen->x / 2),
this->screen = screen; y(screen->y / 2), vec2d(new Vec2d(6)) {
this->leftPaddle = leftPaddle;
this->rightPaddle = rightPaddle;
this->x = screen->x / 2;
this->y = screen->y / 2;
vec2d = new Vec2d(6);
} }
void resetPosition() { void resetPosition() {
@ -66,11 +61,11 @@ public:
} }
private: private:
bool collidedScreenEdgeVertical() { [[nodiscard]]bool collidedScreenEdgeVertical() const {
return y - RADIUS <= 0 || y + RADIUS >= screen->y; return y - RADIUS <= 0 || y + RADIUS >= screen->y;
} }
std::optional<Side> collidedScreenEdgeHorizontal() { [[nodiscard]] std::optional<Side> collidedScreenEdgeHorizontal() const {
if (x + RADIUS >= screen->x) if (x + RADIUS >= screen->x)
return Side::RIGHT; return Side::RIGHT;
else if (x - RADIUS <= 0) else if (x - RADIUS <= 0)
@ -78,7 +73,7 @@ private:
return std::nullopt; return std::nullopt;
} }
std::optional<Side> collidedPaddle() { [[nodiscard]] std::optional<Side> collidedPaddle() const {
// Right paddle // Right paddle
if (x + RADIUS >= rightPaddle->x && if (x + RADIUS >= rightPaddle->x &&
y >= rightPaddle->y && y >= rightPaddle->y &&

View File

@ -44,13 +44,13 @@ public:
SDL_RenderFillRect(renderer, this); SDL_RenderFillRect(renderer, this);
} }
void startMoving(bool up) { void startMoving(const bool up) {
if (up) if (up)
movingUp = true; movingUp = true;
else movingDown = true; else movingDown = true;
} }
void stopMoving(bool up) { void stopMoving(const bool up) {
if (up) if (up)
movingUp = false; movingUp = false;
else movingDown = false; else movingDown = false;
@ -68,11 +68,11 @@ public:
} }
private: private:
bool canMoveDown() { [[nodiscard]] bool canMoveDown() const {
return y + h < screen->y; return y + h < screen->y;
} }
bool canMoveUp() { [[nodiscard]] bool canMoveUp() const {
return y > 0; return y > 0;
} }

View File

@ -36,7 +36,7 @@ private:
const int shadowOffset = 3; const int shadowOffset = 3;
public: public:
explicit Score(uint8_t max_score, SDL_Point *screenSize, const std::function<void(Side)> whenWon) : MAX_SCORE( explicit Score(uint8_t max_score, SDL_Point *screenSize, const std::function<void(Side)> &whenWon) : MAX_SCORE(
max_score), whenWon(whenWon) { max_score), whenWon(whenWon) {
resetScore(); resetScore();
if (defaultFontPath == nullptr) { if (defaultFontPath == nullptr) {
@ -57,17 +57,14 @@ public:
~Score() { ~Score() {
if (font) if (font)
TTF_CloseFont(font); TTF_CloseFont(font);
if (surface) SDL_FreeSurface(surface);
SDL_FreeSurface(surface);
} }
void update() { void update() {
if (!hasIncremented && surface != nullptr && shadowSurface != nullptr) return; if (!hasIncremented && surface != nullptr && shadowSurface != nullptr) return;
if (surface != nullptr) SDL_FreeSurface(surface);
SDL_FreeSurface(surface); SDL_FreeSurface(shadowSurface);
if (shadowSurface != nullptr)
SDL_FreeSurface(shadowSurface);
hasIncremented = false; hasIncremented = false;
char score_text[8]; char score_text[8];
@ -112,7 +109,7 @@ public:
leftScore = rightScore = 0; leftScore = rightScore = 0;
} }
void incrementScore(Side side) { void incrementScore(const Side side) {
hasIncremented = true; hasIncremented = true;
uint8_t temp; uint8_t temp;
switch (side) { switch (side) {