From 76efeac01629edd8e20b02cc49ed78e5037ad532 Mon Sep 17 00:00:00 2001 From: Wouter Wijsman Date: Fri, 22 Nov 2024 23:13:10 +0100 Subject: [PATCH] Make more screens able to resize in real time --- src/states/GameOverState.cpp | 2 + src/states/HighscoreState.cpp | 45 ++++++++++++---- src/states/HighscoreState.hpp | 2 + src/states/MenuState.cpp | 2 +- src/states/MenuState.hpp | 1 - src/states/ModeSelectState.cpp | 93 ++++++++++++++++++++++------------ src/states/ModeSelectState.hpp | 2 + 7 files changed, 103 insertions(+), 44 deletions(-) diff --git a/src/states/GameOverState.cpp b/src/states/GameOverState.cpp index 6cb1eb4..9128bbd 100644 --- a/src/states/GameOverState.cpp +++ b/src/states/GameOverState.cpp @@ -29,6 +29,8 @@ void GameOverState::handleEvents(std::vector events) { } else if (event == Event::QUIT) { this->next_state = State::EXIT; this->done = true; + } else if (event == Event::WINDOW_RESIZE) { + this->screen_text.updateSizing(); } } } diff --git a/src/states/HighscoreState.cpp b/src/states/HighscoreState.cpp index 2dc6979..f9b7157 100644 --- a/src/states/HighscoreState.cpp +++ b/src/states/HighscoreState.cpp @@ -9,15 +9,7 @@ HighscoreState::HighscoreState(SDL_Renderer * renderer, FontManager * fonts, SoundManager * sounds, OptionManager * options) : renderer(renderer), fonts(fonts), sounds(sounds), options(options), theme(renderer, options, Theme::MENU) { - this->text_title = fonts->getTexture(renderer, _("High Scores"), FontType::TITLE, {COLOR_MENU_TITLE.r, COLOR_MENU_TITLE.g, COLOR_MENU_TITLE.b, COLOR_MENU_TITLE.a}); - this->text_bottom = fonts->getTexture(renderer, _("press confirm to go back"), FontType::NORMAL, {255, 255, 255, 255}); - - std::string standard_mode_completed = options->getStandardModeCompleted() ? _("yes") : _("no"); - texts.push_back(fonts->getTexture(renderer, _("standard mode completed: ") + standard_mode_completed, FontType::NORMAL, {255, 255, 255, 255})); - texts.push_back(fonts->getTexture(renderer, _("highest level reached in challenge mode: ") + std::to_string(options->getChallengeModeHighscore()), FontType::NORMAL, {255, 255, 255, 255})); - texts.push_back(fonts->getTexture(renderer, _("matches in relaxed mode: ") + std::to_string(options->getRelaxedModeScore()), FontType::NORMAL, {255, 255, 255, 255})); - - this->text_start_y = getTextY(0); + this->loadTexts(); } HighscoreState::~HighscoreState() { @@ -41,6 +33,9 @@ void HighscoreState::handleEvents(std::vector events) { case Event::CONFIRM: this->done = true; break; + case Event::WINDOW_RESIZE: + this->updateSizing(); + break; default: break; } @@ -55,6 +50,10 @@ void HighscoreState::update() { void HighscoreState::draw(SDL_Renderer * renderer) { this->theme.draw(renderer); + if (this->texts.empty()) { + loadTexts(); + } + // Draw title SDL_Rect rect_title = {this->options->getScreenWidth() / 2, this->options->getScreenHeight() / 8, 0, 0}; SDL_QueryTexture(text_title, NULL, NULL, &rect_title.w, &rect_title.h); @@ -79,6 +78,34 @@ void HighscoreState::draw(SDL_Renderer * renderer) { SDL_RenderCopy(renderer, text_bottom, NULL, &rect_bottom); } +void HighscoreState::loadTexts() { + this->text_title = fonts->getTexture(renderer, _("High Scores"), FontType::TITLE, {COLOR_MENU_TITLE.r, COLOR_MENU_TITLE.g, COLOR_MENU_TITLE.b, COLOR_MENU_TITLE.a}); + this->text_bottom = fonts->getTexture(renderer, _("press confirm to go back"), FontType::NORMAL, {255, 255, 255, 255}); + + std::string standard_mode_completed = options->getStandardModeCompleted() ? _("yes") : _("no"); + this->texts.push_back(fonts->getTexture(renderer, _("standard mode completed: ") + standard_mode_completed, FontType::NORMAL, {255, 255, 255, 255})); + this->texts.push_back(fonts->getTexture(renderer, _("highest level reached in challenge mode: ") + std::to_string(options->getChallengeModeHighscore()), FontType::NORMAL, {255, 255, 255, 255})); + this->texts.push_back(fonts->getTexture(renderer, _("matches in relaxed mode: ") + std::to_string(options->getRelaxedModeScore()), FontType::NORMAL, {255, 255, 255, 255})); + + this->text_start_y = getTextY(0); +} + +void HighscoreState::updateSizing() { + for (size_t i = 0; i < texts.size(); i++) { + SDL_DestroyTexture(texts[i]); + texts[i] = NULL; + } + if (this->text_title) { + SDL_DestroyTexture(this->text_title); + this->text_title = NULL; + } + if (this->text_bottom) { + SDL_DestroyTexture(this->text_bottom); + this->text_bottom = NULL; + } + this->texts.clear(); +} + int HighscoreState::getTextY(int number) { return this->options->getScreenHeight()/(((int) texts.size())+this->text_offset*2)*(number+this->text_offset); } diff --git a/src/states/HighscoreState.hpp b/src/states/HighscoreState.hpp index 7d3cd4a..c75df36 100644 --- a/src/states/HighscoreState.hpp +++ b/src/states/HighscoreState.hpp @@ -30,6 +30,8 @@ class HighscoreState : public BaseState { State next_state = State::MENU; int getTextY(int number); + void loadTexts(); + void updateSizing(); public: HighscoreState(SDL_Renderer * renderer, FontManager * fonts, SoundManager * sounds, OptionManager * options); ~HighscoreState(); diff --git a/src/states/MenuState.cpp b/src/states/MenuState.cpp index 927e628..d401ea3 100644 --- a/src/states/MenuState.cpp +++ b/src/states/MenuState.cpp @@ -160,7 +160,7 @@ void MenuState::loadTexts() { } void MenuState::updateSizing() { - for (int i = 0; i < texts.size(); i++) { + for (size_t i = 0; i < texts.size(); i++) { SDL_DestroyTexture(texts[i]); texts[i] = NULL; if (this->sub_texts[i]) { diff --git a/src/states/MenuState.hpp b/src/states/MenuState.hpp index 6fc949a..2f8ca30 100644 --- a/src/states/MenuState.hpp +++ b/src/states/MenuState.hpp @@ -30,7 +30,6 @@ class MenuState : public BaseState { int getOptionY(int number); void loadTexts(); - void updateSizing(); public: MenuState(SDL_Renderer * renderer, FontManager * fonts, SoundManager * sounds, OptionManager * options); diff --git a/src/states/ModeSelectState.cpp b/src/states/ModeSelectState.cpp index f8c2d95..9b07c77 100644 --- a/src/states/ModeSelectState.cpp +++ b/src/states/ModeSelectState.cpp @@ -10,39 +10,7 @@ ModeSelectState::ModeSelectState(SDL_Renderer * renderer, FontManager * fonts, SoundManager * sounds, OptionManager * options) : renderer(renderer), fonts(fonts), sounds(sounds), options(options), theme(renderer, options, Theme::MENU) { - this->text_title = fonts->getTexture(renderer, "Select a Mode", FontType::TITLE, {COLOR_MENU_TITLE.r, COLOR_MENU_TITLE.g, COLOR_MENU_TITLE.b, COLOR_MENU_TITLE.a}); - this->text_start_y = this->options->getScreenHeight() / 4; - - for (int i = 0; i < ((int) Mode::GO_BACK + 1); i++) { - std::string option_text; - std::string option_sub_text; - switch ((Mode) i) { - case Mode::STANDARD: - option_text = _("standard mode"); - option_sub_text = _("play pre-made levels"); - break; - case Mode::CHALLENGE: - option_text = _("challenge mode"); - option_sub_text = _("ends only when lives run out"); - break; - case Mode::RELAXED: - option_text = _("relaxed mode"); - option_sub_text = _("match without consequences"); - break; - case Mode::GO_BACK: - option_text = _("return to menu"); - break; - default: - option_text = "?????????"; - break; - } - texts.push_back(fonts->getTexture(renderer, option_text, FontType::NORMAL, {255, 255, 255, 255})); - if (!option_sub_text.empty()) { - sub_texts.push_back(fonts->getTexture(renderer, option_sub_text, FontType::SMALL, {255, 255, 255, 255})); - } else { - sub_texts.push_back(NULL); - } - } + this->loadTexts(); } ModeSelectState::~ModeSelectState() { @@ -94,6 +62,8 @@ void ModeSelectState::handleEvents(std::vector events) { } } break; + case Event::WINDOW_RESIZE: + this->updateSizing(); default: break; } @@ -108,6 +78,10 @@ void ModeSelectState::update() { void ModeSelectState::draw(SDL_Renderer * renderer) { this->theme.draw(renderer); + if (this->texts.empty()) { + this->loadTexts(); + } + // Draw title SDL_Rect rect_title = {this->options->getScreenWidth() / 2, this->text_start_y / 2, 0, 0}; SDL_QueryTexture(text_title, NULL, NULL, &rect_title.w, &rect_title.h); @@ -150,6 +124,59 @@ void ModeSelectState::draw(SDL_Renderer * renderer) { } } +void ModeSelectState::loadTexts() { + this->text_title = fonts->getTexture(renderer, "Select a Mode", FontType::TITLE, {COLOR_MENU_TITLE.r, COLOR_MENU_TITLE.g, COLOR_MENU_TITLE.b, COLOR_MENU_TITLE.a}); + this->text_start_y = this->options->getScreenHeight() / 4; + + for (int i = 0; i < ((int) Mode::GO_BACK + 1); i++) { + std::string option_text; + std::string option_sub_text; + switch ((Mode) i) { + case Mode::STANDARD: + option_text = _("standard mode"); + option_sub_text = _("play pre-made levels"); + break; + case Mode::CHALLENGE: + option_text = _("challenge mode"); + option_sub_text = _("ends only when lives run out"); + break; + case Mode::RELAXED: + option_text = _("relaxed mode"); + option_sub_text = _("match without consequences"); + break; + case Mode::GO_BACK: + option_text = _("return to menu"); + break; + default: + option_text = "?????????"; + break; + } + texts.push_back(fonts->getTexture(renderer, option_text, FontType::NORMAL, {255, 255, 255, 255})); + if (!option_sub_text.empty()) { + sub_texts.push_back(fonts->getTexture(renderer, option_sub_text, FontType::SMALL, {255, 255, 255, 255})); + } else { + sub_texts.push_back(NULL); + } + } +} + +void ModeSelectState::updateSizing() { + for (size_t i = 0; i < texts.size(); i++) { + SDL_DestroyTexture(texts[i]); + texts[i] = NULL; + if (this->sub_texts[i]) { + SDL_DestroyTexture(sub_texts[i]); + sub_texts[i] = NULL; + } + } + if (this->text_title) { + SDL_DestroyTexture(this->text_title); + this->text_title = NULL; + } + this->texts.clear(); + this->sub_texts.clear(); +} + int ModeSelectState::getOptionY(int number) { return this->text_start_y + (int)(((this->options->getScreenHeight() - this->text_start_y) / ((int) texts.size()) * number)); } diff --git a/src/states/ModeSelectState.hpp b/src/states/ModeSelectState.hpp index 467c69b..f936fcb 100644 --- a/src/states/ModeSelectState.hpp +++ b/src/states/ModeSelectState.hpp @@ -29,6 +29,8 @@ class ModeSelectState : public BaseState { int selection = 0; int getOptionY(int number); + void loadTexts(); + void updateSizing(); public: ModeSelectState(SDL_Renderer * renderer, FontManager * fonts, SoundManager * sounds, OptionManager * options); ~ModeSelectState();