Skip to content

Commit

Permalink
Make more screens able to resize in real time
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkwouter committed Nov 22, 2024
1 parent 82b1748 commit 76efeac
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 44 deletions.
2 changes: 2 additions & 0 deletions src/states/GameOverState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void GameOverState::handleEvents(std::vector<Event> events) {
} else if (event == Event::QUIT) {
this->next_state = State::EXIT;
this->done = true;
} else if (event == Event::WINDOW_RESIZE) {
this->screen_text.updateSizing();
}
}
}
Expand Down
45 changes: 36 additions & 9 deletions src/states/HighscoreState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -41,6 +33,9 @@ void HighscoreState::handleEvents(std::vector<Event> events) {
case Event::CONFIRM:
this->done = true;
break;
case Event::WINDOW_RESIZE:
this->updateSizing();
break;
default:
break;
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/states/HighscoreState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/states/MenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
1 change: 0 additions & 1 deletion src/states/MenuState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
93 changes: 60 additions & 33 deletions src/states/ModeSelectState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -94,6 +62,8 @@ void ModeSelectState::handleEvents(std::vector<Event> events) {
}
}
break;
case Event::WINDOW_RESIZE:
this->updateSizing();
default:
break;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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));
}
Expand Down
2 changes: 2 additions & 0 deletions src/states/ModeSelectState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 76efeac

Please sign in to comment.