From 18ce5670e42241d1a26ed8dc4675ed72c1f3ec26 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Mon, 29 Jan 2024 10:46:27 +0100 Subject: [PATCH] add support for endscreen aswell --- CMakeLists.txt | 2 +- src/Game.h | 23 +++++++++++++++++------ src/{StartScreen.h => OptionScreen.h} | 6 +++--- 3 files changed, 21 insertions(+), 10 deletions(-) rename src/{StartScreen.h => OptionScreen.h} (87%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c889a60..46b8cea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,7 +35,7 @@ add_executable(Pong src/main.cpp src/VisibleObjects/Score.h src/TextScreen.h src/defaultfont.h - src/StartScreen.h) + src/OptionScreen.h) # Now link the libraries to the target target_link_libraries(Pong ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARY} ${SDL2_TTF_LIBRARY}) diff --git a/src/Game.h b/src/Game.h index c8e49fd..aa09887 100644 --- a/src/Game.h +++ b/src/Game.h @@ -10,7 +10,7 @@ #include "VisibleObjects/PlayerPaddle.h" #include "VisibleObjects/Score.h" #include "TextScreen.h" -#include "StartScreen.h" +#include "OptionScreen.h" enum class GameState { START_SCREEN, GAME, END_SCREEN @@ -21,8 +21,7 @@ private: Ball *ball; Score *score; PlayerPaddle *leftPaddle, *rightPaddle; - StartScreen *startScreen; - TextScreen *endScreen; + OptionScreen *startScreen, *endScreen; protected: GameState gameState; @@ -39,8 +38,8 @@ public: }; score = new Score(5, &this->screenSize, func); ball = new Ball(&this->screenSize, leftPaddle, rightPaddle, score); - startScreen = new StartScreen(&this->screenSize, 4); - endScreen = new TextScreen("", &this->screenSize); + startScreen = new OptionScreen("Welcome to Pong!\nPress any key to get started...", &this->screenSize, 4); + endScreen = nullptr; gameState = GameState::START_SCREEN; } @@ -78,8 +77,10 @@ public: switch (gameState) { case GameState::START_SCREEN: startScreen->update(); - if (startScreen->isDone()) + if (startScreen->isDone()) { gameState = GameState::GAME; + delete startScreen; // We will never get back to this state + } break; case GameState::GAME: ball->update(); @@ -88,6 +89,16 @@ public: score->update(); break; case GameState::END_SCREEN: + if (endScreen == nullptr) { + std::stringstream ss; + ss << "Player " << " won" << "\nWould you like to play again?" << "\nIf so, press any button..."; + endScreen = new OptionScreen(ss.str(), &screenSize, 4); + } + endScreen->update(); + if (endScreen->isDone()) { + gameState = GameState::GAME; + delete endScreen; // The text will not be the same if we get back here. We might as well free it. + } break; } } diff --git a/src/StartScreen.h b/src/OptionScreen.h similarity index 87% rename from src/StartScreen.h rename to src/OptionScreen.h index 4f1da49..63f024b 100644 --- a/src/StartScreen.h +++ b/src/OptionScreen.h @@ -16,7 +16,7 @@ int_least64_t getCurrentEpochTimeMillis() { } -class StartScreen : public TextScreen { +class OptionScreen : public TextScreen { private: bool hasStartedCounting_ = false; int_least64_t nextMsEpoch = 0; @@ -32,8 +32,8 @@ public: } public: - StartScreen(SDL_Point *screenSize, int seconds) - : TextScreen("Welcome to Pong!\nPress any key to get started...", screenSize), stepsToDo(seconds) { + OptionScreen(const std::string &text, SDL_Point *screenSize, int seconds) + : TextScreen(text, screenSize), stepsToDo(seconds) { } void update() override {