From 18e081de6520ee9632425bf34e4a27da5334b642 Mon Sep 17 00:00:00 2001 From: ThePythonator <49040244+ThePythonator@users.noreply.github.com> Date: Mon, 16 Sep 2024 20:30:32 +0100 Subject: [PATCH 1/3] chore: Copy across minor changes from SSB:R Fixed a couple of comments and added an operator overload for scaling Rects by a constant. --- include/framework/BaseStage.hpp | 1 + include/framework/File.hpp | 2 +- include/framework/Maths.hpp | 3 +++ src/framework/Maths.cpp | 8 ++++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/framework/BaseStage.hpp b/include/framework/BaseStage.hpp index aba61ae..60396b2 100644 --- a/include/framework/BaseStage.hpp +++ b/include/framework/BaseStage.hpp @@ -16,6 +16,7 @@ namespace Framework { // Called when stage stops being the current stage virtual void end(); + // Returns false if the application should close virtual bool update(float dt) = 0; virtual void render() = 0; diff --git a/include/framework/File.hpp b/include/framework/File.hpp index 99f6ec5..6279118 100644 --- a/include/framework/File.hpp +++ b/include/framework/File.hpp @@ -56,7 +56,7 @@ namespace Framework { // This shouldn't ever be needed, so it isn't implemented //void write(std::string filepath, TMX data, TMXFormat file_format = TMXFormat::AUTO); - /// Determines the Tiled level file format from the file name + // Determines the Tiled level file format from the file name TMXFormat get_format(std::string filepath); // Changes the null tile index from 0 to the value specified, reducing all other indices by 1 diff --git a/include/framework/Maths.hpp b/include/framework/Maths.hpp index 991eb3e..c5fae6c 100644 --- a/include/framework/Maths.hpp +++ b/include/framework/Maths.hpp @@ -43,6 +43,9 @@ namespace Framework { vec2 size; }; + Rect operator*(const Rect& r, const float s); + Rect operator*(const float s, const Rect& r); + extern const Rect RECT_NULL; bool colliding(Rect a, vec2 b); diff --git a/src/framework/Maths.cpp b/src/framework/Maths.cpp index e48bcf4..3821b6a 100644 --- a/src/framework/Maths.cpp +++ b/src/framework/Maths.cpp @@ -47,6 +47,14 @@ namespace Framework { return position + size; } + Rect operator*(const Rect& r, const float s) { + return Rect(r.position * s, r.size * s); + } + + Rect operator*(const float s, const Rect & r) { + return r * s; + } + bool colliding(Rect a, vec2 b) { return b.x >= a.position.x && b.x <= a.position.x + a.size.x && b.y >= a.position.y && b.y <= a.position.y + a.size.y; From 1d5e6f92721f3b4108a5e0653895c3f0a2b28801 Mon Sep 17 00:00:00 2001 From: ThePythonator <49040244+ThePythonator@users.noreply.github.com> Date: Mon, 16 Sep 2024 20:59:01 +0100 Subject: [PATCH 2/3] refactor: Split Stages file into MenuStages and GameStages --- CMakeLists.txt | 3 +- include/game/Game.hpp | 2 +- include/game/{Stages.hpp => GameStages.hpp} | 13 +--- include/game/MenuStages.hpp | 18 +++++ src/game/{Stages.cpp => GameStages.cpp} | 84 +-------------------- src/game/MenuStages.cpp | 83 ++++++++++++++++++++ 6 files changed, 107 insertions(+), 96 deletions(-) rename include/game/{Stages.hpp => GameStages.hpp} (71%) create mode 100644 include/game/MenuStages.hpp rename src/game/{Stages.cpp => GameStages.cpp} (51%) create mode 100644 src/game/MenuStages.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d760d5..1178fa5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,8 @@ set(GAME_SOURCES "Application.cpp" "Game.cpp" - "Stages.cpp" + "MenuStages.cpp" + "GameStages.cpp" ) # To be used instead of set(GAME_SOURCES ...) diff --git a/include/game/Game.hpp b/include/game/Game.hpp index 7caaed6..7c08ec9 100644 --- a/include/game/Game.hpp +++ b/include/game/Game.hpp @@ -2,7 +2,7 @@ #include "BaseGame.hpp" -#include "Stages.hpp" +#include "MenuStages.hpp" #include "FadeTransition.hpp" diff --git a/include/game/Stages.hpp b/include/game/GameStages.hpp similarity index 71% rename from include/game/Stages.hpp rename to include/game/GameStages.hpp index c737e4f..5cbbb62 100644 --- a/include/game/Stages.hpp +++ b/include/game/GameStages.hpp @@ -4,16 +4,7 @@ #include "Constants.hpp" -class TitleStage : public Framework::BaseStage { -public: - void start(); - - bool update(float dt); - void render(); - -private: - Framework::Timer _timer; -}; +#include "MenuStages.hpp" class GameStage : public Framework::BaseStage { public: @@ -34,4 +25,4 @@ class PausedStage : public Framework::BaseStage { private: BaseStage* _background_stage; -}; \ No newline at end of file +}; diff --git a/include/game/MenuStages.hpp b/include/game/MenuStages.hpp new file mode 100644 index 0000000..761549d --- /dev/null +++ b/include/game/MenuStages.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "BaseStage.hpp" + +#include "Constants.hpp" + +#include "GameStages.hpp" + +class TitleStage : public Framework::BaseStage { +public: + void start(); + + bool update(float dt); + void render(); + +private: + Framework::Timer _timer; +}; diff --git a/src/game/Stages.cpp b/src/game/GameStages.cpp similarity index 51% rename from src/game/Stages.cpp rename to src/game/GameStages.cpp index aeb78d6..88012a2 100644 --- a/src/game/Stages.cpp +++ b/src/game/GameStages.cpp @@ -1,86 +1,4 @@ -#include "Stages.hpp" - -// TitleStage - -void TitleStage::start() { - // Create buttons - buttons.clear(); - buttons.emplace_back( - Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, 32), Framework::Vec(256, 64)), - graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD], - Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Play", COLOURS::BLACK, 4.0f), - BUTTONS::TITLE::PLAY - ); - buttons.emplace_back( - Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, -64), Framework::Vec(256, 64)), - graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD], - Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Quit", COLOURS::BLACK, 4.0f), - BUTTONS::TITLE::QUIT - ); - - // Set transition - set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]); - - // Start transition - transition->open(); - - // Start timer used for rotations - _timer.start(); -} - -bool TitleStage::update(float dt) { - transition->update(dt); - _timer.update(dt); - - // Update buttons - for (Framework::Button& button : buttons) { - button.update(input); - - if (button.pressed() && transition->is_open()) { - button_selected = button.get_id(); - transition->close(); - } - } - - if (transition->is_closed()) { - // Next stage! - switch (button_selected) { - case BUTTONS::TITLE::PLAY: - finish(new GameStage()); - break; - - case BUTTONS::TITLE::QUIT: - // Returning false causes program to exit - return false; - // (so we don't need the break) - - default: - break; - } - } - - return true; -} - -void TitleStage::render() { - graphics_objects->graphics_ptr->fill(COLOURS::BLUE); - - graphics_objects->graphics_ptr->render_circle(Framework::Vec(20, 20), 10, COLOURS::BLACK); - graphics_objects->graphics_ptr->render_rect(Framework::Rect(40, 20, 10, 30), COLOURS::BLACK); - - graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(0, Framework::Vec(64, 48)); - - graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(2, Framework::Vec(16, 48)); - graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(2, Framework::Vec(32, 48), Framework::SpriteTransform::ROTATE_90_ACW); - - graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(1, Framework::Vec(96, 48), SPRITES::SCALE, _timer.time() * 90); - - for (const Framework::Button& button : buttons) { - button.render(); - } - - transition->render(); -} +#include "GameStages.hpp" // GameStage diff --git a/src/game/MenuStages.cpp b/src/game/MenuStages.cpp new file mode 100644 index 0000000..d202e47 --- /dev/null +++ b/src/game/MenuStages.cpp @@ -0,0 +1,83 @@ +#include "MenuStages.hpp" + +// TitleStage + +void TitleStage::start() { + // Create buttons + buttons.clear(); + buttons.emplace_back( + Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, 32), Framework::Vec(256, 64)), + graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD], + Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Play", COLOURS::BLACK, 4.0f), + BUTTONS::TITLE::PLAY + ); + buttons.emplace_back( + Framework::Rect(WINDOW::SIZE_HALF - Framework::Vec(128, -64), Framework::Vec(256, 64)), + graphics_objects->button_image_groups[GRAPHICS_OBJECTS::BUTTON_IMAGE_GROUPS::STANDARD], + Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Quit", COLOURS::BLACK, 4.0f), + BUTTONS::TITLE::QUIT + ); + + // Set transition + set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]); + + // Start transition + transition->open(); + + // Start timer used for rotations + _timer.start(); +} + +bool TitleStage::update(float dt) { + transition->update(dt); + _timer.update(dt); + + // Update buttons + for (Framework::Button& button : buttons) { + button.update(input); + + if (button.pressed() && transition->is_open()) { + button_selected = button.get_id(); + transition->close(); + } + } + + if (transition->is_closed()) { + // Next stage! + switch (button_selected) { + case BUTTONS::TITLE::PLAY: + finish(new GameStage()); + break; + + case BUTTONS::TITLE::QUIT: + // Returning false causes program to exit + return false; + // (so we don't need the break) + + default: + break; + } + } + + return true; +} + +void TitleStage::render() { + graphics_objects->graphics_ptr->fill(COLOURS::BLUE); + + graphics_objects->graphics_ptr->render_circle(Framework::Vec(20, 20), 10, COLOURS::BLACK); + graphics_objects->graphics_ptr->render_rect(Framework::Rect(40, 20, 10, 30), COLOURS::BLACK); + + graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(0, Framework::Vec(64, 48)); + + graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(2, Framework::Vec(16, 48)); + graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(2, Framework::Vec(32, 48), Framework::SpriteTransform::ROTATE_90_ACW); + + graphics_objects->spritesheet_ptrs[GRAPHICS_OBJECTS::SPRITESHEETS::MAIN_SPRITESHEET]->sprite(1, Framework::Vec(96, 48), SPRITES::SCALE, _timer.time() * 90); + + for (const Framework::Button& button : buttons) { + button.render(); + } + + transition->render(); +} From e291f4301675b9fa04dce1064345ccfb3c57cc58 Mon Sep 17 00:00:00 2001 From: ThePythonator <49040244+ThePythonator@users.noreply.github.com> Date: Mon, 16 Sep 2024 21:16:24 +0100 Subject: [PATCH 3/3] feat: Add IntroStage Added a stage which can be used to display a logo or some introduction text. --- include/game/Constants.hpp | 4 ++-- include/game/MenuStages.hpp | 14 ++++++++++++ src/game/Game.cpp | 4 ++-- src/game/MenuStages.cpp | 44 +++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/include/game/Constants.hpp b/include/game/Constants.hpp index f608ea6..f84acca 100644 --- a/include/game/Constants.hpp +++ b/include/game/Constants.hpp @@ -104,7 +104,7 @@ namespace COLOURS { } namespace TIMINGS { - + const float INTRO_OPEN_TIME = 4.0f; } namespace TRANSITIONS { @@ -152,4 +152,4 @@ namespace BUTTONS { namespace GAME { -} \ No newline at end of file +} diff --git a/include/game/MenuStages.hpp b/include/game/MenuStages.hpp index 761549d..8c7e7c1 100644 --- a/include/game/MenuStages.hpp +++ b/include/game/MenuStages.hpp @@ -1,11 +1,25 @@ #pragma once #include "BaseStage.hpp" +#include "Font.hpp" +#include "Timer.hpp" #include "Constants.hpp" #include "GameStages.hpp" +class IntroStage : public Framework::BaseStage { +public: + void start(); + + bool update(float dt); + void render(); + +private: + Framework::Timer intro_timer; + Framework::Text intro_text; +}; + class TitleStage : public Framework::BaseStage { public: void start(); diff --git a/src/game/Game.cpp b/src/game/Game.cpp index 485ab12..7553728 100644 --- a/src/game/Game.cpp +++ b/src/game/Game.cpp @@ -5,7 +5,7 @@ Game::Game() : BaseGame() { } void Game::start() { - stage = new TitleStage(); + stage = new IntroStage(); } void Game::end() { @@ -64,4 +64,4 @@ void Game::clear_data() { // Don't need to clear up graphics objects items - it's done for us in BaseGame // Clear up anything else we need to -} \ No newline at end of file +} diff --git a/src/game/MenuStages.cpp b/src/game/MenuStages.cpp index d202e47..a064183 100644 --- a/src/game/MenuStages.cpp +++ b/src/game/MenuStages.cpp @@ -1,5 +1,49 @@ #include "MenuStages.hpp" +// IntroStage + +void IntroStage::start() { + intro_text = Framework::Text(graphics_objects->font_ptrs[GRAPHICS_OBJECTS::FONTS::MAIN_FONT], "Some intro text", COLOURS::BLACK); + + intro_timer.stop(); + + // Start transition + set_transition(graphics_objects->transition_ptrs[GRAPHICS_OBJECTS::TRANSITIONS::FADE_TRANSITION]); + transition->open(); +} + +bool IntroStage::update(float dt) { + transition->update(dt); + intro_timer.update(dt); + + if (transition->is_open()) { + if (intro_timer.running()) { + if (intro_timer.time() >= TIMINGS::INTRO_OPEN_TIME) { + transition->close(); + } + } + else { + intro_timer.reset(); + intro_timer.start(); + } + } + else if (transition->is_closed()) { + // Finish intro + finish(new TitleStage()); + } + + return true; +} + +void IntroStage::render() { + graphics_objects->graphics_ptr->fill(COLOURS::BLUE); + + // Display some intro text in the centre of the display + intro_text.render(WINDOW::SIZE_HALF); + + transition->render(); +} + // TitleStage void TitleStage::start() {