From 2a29f01fc2847aa2737c5c535335825ec00dbf4d Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Mon, 29 Jan 2024 12:30:00 +0100 Subject: [PATCH] cleanup --- src/Game.h | 31 +++++++++++++------------------ src/VisibleObjects/Ball.h | 19 +++++++------------ src/VisibleObjects/PlayerPaddle.h | 7 ++----- src/text/Score.h | 7 +++---- src/text/TextScreen.h | 26 +++++--------------------- 5 files changed, 30 insertions(+), 60 deletions(-) diff --git a/src/Game.h b/src/Game.h index d541eac..8ec9656 100644 --- a/src/Game.h +++ b/src/Game.h @@ -19,29 +19,24 @@ enum class GameState { class Game : public SdlWrapper { private: - Ball *ball; Score *score; PlayerPaddle *leftPaddle, *rightPaddle; OptionScreen *startScreen, *endScreen; + Ball *ball; protected: GameState gameState; public: - explicit Game(SDL_Point screenSize) : SdlWrapper("Pong", screenSize, 60) { - leftPaddle = new PlayerPaddle(&this->screenSize, Side::LEFT); - rightPaddle = new PlayerPaddle(&this->screenSize, Side::RIGHT); - - auto func = [this](Side side) { - const char *player = side == Side::LEFT ? "one" : "two"; - std::cout << "Player " << player << " won" << std::endl; - this->running = false; - }; - score = new Score(&this->screenSize, 5, func); - ball = new Ball(&this->screenSize, leftPaddle, rightPaddle, score); - startScreen = new OptionScreen("Welcome to Pong!\nPress any key to get started...", &this->screenSize, 4); - endScreen = nullptr; - gameState = GameState::START_SCREEN; + explicit Game(SDL_Point screenSize) : SdlWrapper("Pong", screenSize, 60), + leftPaddle(new PlayerPaddle(&this->screenSize, Side::LEFT)), + rightPaddle(new PlayerPaddle(&this->screenSize, Side::RIGHT)), + score(new Score(&this->screenSize, 5)), + ball(new Ball(&this->screenSize, leftPaddle, rightPaddle, score)), + startScreen( + new OptionScreen("Welcome to Pong!\nPress any key to get started...", + &this->screenSize, 4)), endScreen(nullptr), + gameState(GameState::START_SCREEN) { } ~Game() override { @@ -56,7 +51,6 @@ public: SDL_SetRenderDrawColor(renderer, 128, 0, 128, 0); SDL_RenderClear(renderer); - switch (gameState) { case GameState::START_SCREEN: startScreen->draw(renderer); @@ -93,8 +87,9 @@ public: if (score->sideWon().has_value()) { const char *player = score->sideWon().value() == Side::LEFT ? "left" : "right"; std::stringstream ss; - ss << "The " << player << " player won with " << std::to_string(score->leftScore) << " - " << std::to_string(score->rightScore) - << "\nWould you like to play again?" << "\nIf so, press any button..."; + ss << "The " << player << " player won with " << std::to_string(score->leftScore) << " - " + << std::to_string(score->rightScore) << "\nWould you like to play again?" + << "\nIf so, press any button..."; score->resetScore(); endScreen = new OptionScreen(ss.str(), &screenSize, 4); gameState = GameState::END_SCREEN; diff --git a/src/VisibleObjects/Ball.h b/src/VisibleObjects/Ball.h index 00373b9..8d2b1e6 100644 --- a/src/VisibleObjects/Ball.h +++ b/src/VisibleObjects/Ball.h @@ -14,18 +14,17 @@ class Ball { private: static const uint8_t RADIUS = 15; - const SDL_Point *screen; + const SDL_Point *const screen; Sint16 x, y; Vec2d *vec2d; static const uint32_t color = 0xCD5C5CFF; - const PlayerPaddle *leftPaddle, *rightPaddle; - Score *score; + const PlayerPaddle *const leftPaddle, *const rightPaddle; + Score *const score; public: explicit Ball(const SDL_Point *screen, const PlayerPaddle *leftPaddle, const PlayerPaddle *rightPaddle, - Score *score) : - score(score), screen(screen), leftPaddle(leftPaddle), rightPaddle(rightPaddle), x(screen->x / 2), - y(screen->y / 2), vec2d(new Vec2d(6)) { + Score *score) : score(score), screen(screen), leftPaddle(leftPaddle), rightPaddle(rightPaddle), + x(screen->x / 2), y(screen->y / 2), vec2d(new Vec2d(6)) { } void resetPosition() { @@ -76,15 +75,11 @@ private: [[nodiscard]] std::optional collidedPaddle() const { // Right paddle - if (x + RADIUS >= rightPaddle->x && - y >= rightPaddle->y && - y <= rightPaddle->y + rightPaddle->h) { + if (x + RADIUS >= rightPaddle->x && y >= rightPaddle->y && y <= rightPaddle->y + rightPaddle->h) { return Side::RIGHT; } // Left paddle - if (x - RADIUS <= leftPaddle->x + leftPaddle->w && - y >= leftPaddle->y && - y <= leftPaddle->y + leftPaddle->h) { + if (x - RADIUS <= leftPaddle->x + leftPaddle->w && y >= leftPaddle->y && y <= leftPaddle->y + leftPaddle->h) { return Side::LEFT; } return std::nullopt; diff --git a/src/VisibleObjects/PlayerPaddle.h b/src/VisibleObjects/PlayerPaddle.h index 3bf8f67..47888d1 100644 --- a/src/VisibleObjects/PlayerPaddle.h +++ b/src/VisibleObjects/PlayerPaddle.h @@ -12,18 +12,15 @@ class PlayerPaddle : public SDL_Rect { private: static const int MOVE_PER_TICK = 5; const SDL_Point *screen; - bool movingUp, movingDown; + bool movingUp = false, movingDown = false; uint8_t color[4]{}; public: - PlayerPaddle(const SDL_Point *screen, const Side side) : SDL_Rect() { + PlayerPaddle(const SDL_Point *screen, const Side side) : SDL_Rect() , screen(screen){ w = 20; h = 80; x = side == Side::LEFT ? 0 : screen->x - w; y = (screen->y - h) / 2; - movingUp = false; - movingDown = false; - this->screen = screen; color[0] = 255; color[1] = 234; diff --git a/src/text/Score.h b/src/text/Score.h index c971dff..73b5f25 100644 --- a/src/text/Score.h +++ b/src/text/Score.h @@ -18,7 +18,6 @@ class Score : public TextScreen { private: const uint8_t MAX_SCORE; - const std::function whenWon; std::optional sideWon_; public: @@ -29,9 +28,9 @@ public: } public: - explicit Score(SDL_Point *screenSize, uint8_t max_score, const std::function &whenWon) : MAX_SCORE( - max_score), whenWon(whenWon), leftScore(0), rightScore(0), TextScreen("", screenSize, std::make_optional( - SDL_Point{screenSize->x / 2 - 50, 10})) { + explicit Score(SDL_Point *screenSize, uint8_t max_score) : MAX_SCORE(max_score), leftScore(0), rightScore(0), + TextScreen("", screenSize, std::make_optional( + SDL_Point{screenSize->x / 2 - 50, 10})) { } void update() override { diff --git a/src/text/TextScreen.h b/src/text/TextScreen.h index bc75595..e0a84ad 100644 --- a/src/text/TextScreen.h +++ b/src/text/TextScreen.h @@ -29,7 +29,6 @@ private: const SDL_Color shadowColor = {243, 156, 18, 100}; const int shadowOffset = 3; - protected: std::vector lines; bool hasUpdated; @@ -46,7 +45,7 @@ public: std::cerr << "Font path is not set for this platform (null)" << std::endl; exit(-1); } - this->font = TTF_OpenFont(defaultFontPath, 42); + font = TTF_OpenFont(defaultFontPath, 42); if (font == nullptr) { std::cerr << "Failed to load font: " << TTF_GetError() << std::endl; exit(-1); @@ -86,8 +85,9 @@ private: public: ~TextScreen() { - if (font) - TTF_CloseFont(font); + // TTF_CLoseFont & SDL_FreeSurface are null-safe + + TTF_CloseFont(font); for (auto *surface: surfaces) SDL_FreeSurface(surface); for (auto *surface: shadowSurfaces) @@ -118,22 +118,6 @@ public: initPositions(replaceText); } - void replaceCharAtIndex(char c, int line, int index) { - if (lines.size() <= line) { - if (lines[line].length() <= index) { - std::cerr << "string lines is of length " << lines.size() << ", but line index is " << index - << std::endl; - return; - } - } - if (lines[line].length() <= index) { - std::cerr << "text string is of length " << lines[line].length() << ", but index is " << index << std::endl; - return; - } - hasUpdated = false; - lines[line][index] = c; - } - virtual void update() { if (hasUpdated) return; @@ -163,7 +147,7 @@ public: } private: - static std::vector splitString(const std::string &string, const char delim) { + static std::vector splitString(const std::string &string, const char &delim) { int size = 0; for (char c: string) if (c == delim) size++;