From d4ec09a8fba319f84d2fb3b829fb20f75fc20a21 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 28 Jan 2024 21:07:18 +0100 Subject: [PATCH] Break out default font --- CMakeLists.txt | 3 ++- src/Game.h | 41 ++++++++++++++++++++++++++++++-------- src/VisibleObjects/Score.h | 11 +--------- src/defaultfont.h | 16 +++++++++++++++ 4 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 src/defaultfont.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 96d2659..6be0ebb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,8 @@ add_executable(Pong src/main.cpp src/Vec2d/Bump.h src/VisibleObjects/PlayerPaddle.h src/VisibleObjects/Side.h - src/VisibleObjects/Score.h) + src/VisibleObjects/Score.h + src/defaultfont.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 2a5d6c1..ffb6173 100644 --- a/src/Game.h +++ b/src/Game.h @@ -10,6 +10,9 @@ #include "VisibleObjects/PlayerPaddle.h" #include "VisibleObjects/Score.h" +enum class GameState { + START_SCREEN, GAME, END_SCREEN +}; class Game : public SdlWrapper { private: @@ -17,6 +20,9 @@ private: Score *score; PlayerPaddle *leftPaddle, *rightPaddle; +protected: + GameState gameState; + public: explicit Game(SDL_Point screenSize) : SdlWrapper("Pong", screenSize, 60) { leftPaddle = new PlayerPaddle(&this->screenSize, Side::LEFT); @@ -29,6 +35,7 @@ public: }; score = new Score(5, &this->screenSize, func); ball = new Ball(&this->screenSize, leftPaddle, rightPaddle, score); + gameState = GameState::START_SCREEN; } ~Game() override { @@ -43,18 +50,36 @@ public: SDL_SetRenderDrawColor(renderer, 128, 0, 128, 0); SDL_RenderClear(renderer); - ball->draw(renderer); - score->draw(renderer); - leftPaddle->draw(renderer); - rightPaddle->draw(renderer); + + switch (gameState) { + case GameState::START_SCREEN: + break; + case GameState::GAME: + ball->draw(renderer); + score->draw(renderer); + leftPaddle->draw(renderer); + rightPaddle->draw(renderer); + break; + case GameState::END_SCREEN: + break; + } + SDL_RenderPresent(renderer); } bool update() override { - ball->update(); - leftPaddle->update(); - rightPaddle->update(); - score->update(); + switch (gameState) { + case GameState::START_SCREEN: + break; + case GameState::GAME: + ball->update(); + leftPaddle->update(); + rightPaddle->update(); + score->update(); + break; + case GameState::END_SCREEN: + break; + } return true; } diff --git a/src/VisibleObjects/Score.h b/src/VisibleObjects/Score.h index 1e016ce..3760d99 100644 --- a/src/VisibleObjects/Score.h +++ b/src/VisibleObjects/Score.h @@ -9,22 +9,13 @@ #include #include "Side.h" #include "SDL_ttf.h" +#include "../defaultfont.h" #include #include #include -#if defined(_WIN32) || defined(_WIN64) -const char* defaultFontPath = "C:\\Windows\\Fonts\\Arial.ttf"; -#elif defined(__linux__) -const char *defaultFontPath = "/usr/share/fonts/truetype/DejaVuSans-Bold.ttf"; -#elif defined(__APPLE__) || defined(__MACH__) -const char *defaultFontPath = "/System/Library/Fonts/Supplemental/Arial.ttf"; -#else -const char* defaultFontPath = nullptr; -#endif - class Score { private: diff --git a/src/defaultfont.h b/src/defaultfont.h new file mode 100644 index 0000000..e8b2e94 --- /dev/null +++ b/src/defaultfont.h @@ -0,0 +1,16 @@ +// +// Created by love on 2024-01-24. +// + +#pragma once + +#if defined(_WIN32) || defined(_WIN64) +const char* defaultFontPath = "C:\\Windows\\Fonts\\Arial.ttf"; +#elif defined(__linux__) +const char *defaultFontPath = "/usr/share/fonts/truetype/DejaVuSans-Bold.ttf"; +#elif defined(__APPLE__) || defined(__MACH__) +const char *defaultFontPath = "/System/Library/Fonts/Supplemental/Arial.ttf"; +#else +const char* defaultFontPath = nullptr; +#endif +