git repos / blockattack-game

commit 60b2bac0

Poul Sander · 2023-11-10 20:43
60b2bac034bf2223d2d27d73f541095dc77ff524 patch · browse files
parent 883f5498558a4afe562cc3abdb72efe63f80ea6e

Stop using a global object for button graphics

Changed files

M source/code/MenuSystem.cpp before
M source/code/MenuSystem.h before
M source/code/menudef.cpp before
diff --git a/source/code/MenuSystem.cpp b/source/code/MenuSystem.cpp index 803cad6..51e4c56 100644 --- a/source/code/MenuSystem.cpp +++ b/source/code/MenuSystem.cpp
@@ -31,11 +31,6 @@ https://blockattack.net
static int oldmousex = 0;
static int oldmousey = 0;
-const char* const menu_marked = "menu_marked";
-const char* const menu_unmarked = "menu_unmarked";
-
-ButtonGfx standardButton;
-
void ButtonGfx::setSurfaces() {
this->xsize = globalData.spriteHolder->GetSprite(menu_marked).GetWidth();
this->ysize = globalData.spriteHolder->GetSprite(menu_marked).GetHeight();
@@ -44,7 +39,7 @@ void ButtonGfx::setSurfaces() {
}
}
-sago::SagoTextField* ButtonGfx::getLabel(const std::string& text, bool marked) {
+sago::SagoTextField* ButtonGfx::getLabel(const std::string& text, bool marked) const {
if (!marked) {
const auto& theLabel = labels.find(text);
if (theLabel != labels.end()) {
@@ -85,6 +80,7 @@ Button& Button::operator=(const Button& other) {
void Button::setLabel(const std::string& text) {
label = text;
+ standardButton.setSurfaces();
}
void Button::setAction(void (*action2run)(void)) {
@@ -109,19 +105,19 @@ bool Button::isPopOnRun() const {
static void drawToScreen(const Button& b) {
if (b.marked) {
- globalData.spriteHolder->GetSprite(menu_marked).Draw(globalData.screen, SDL_GetTicks(), b.x, b.y);
+ globalData.spriteHolder->GetSprite(b.standardButton.menu_marked).Draw(globalData.screen, SDL_GetTicks(), b.x, b.y);
}
else {
- globalData.spriteHolder->GetSprite(menu_unmarked).Draw(globalData.screen, SDL_GetTicks(), b.x, b.y);
+ globalData.spriteHolder->GetSprite(b.standardButton.menu_unmarked).Draw(globalData.screen, SDL_GetTicks(), b.x, b.y);
}
- standardButton.getLabel(b.getLabel(), b.marked)->Draw(globalData.screen, b.x+standardButton.xsize/2,b.y+standardButton.ysize/2,
+ b.standardButton.getLabel(b.getLabel(), b.marked)->Draw(globalData.screen, b.x+b.standardButton.xsize/2,b.y+b.standardButton.ysize/2,
sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::center);
}
static bool isClicked(const Button& b, int x,int y) {
- if ( x >= b.x && y >= b.y && x<= b.x+standardButton.xsize && y <= b.y + standardButton.ysize) {
+ if ( x >= b.x && y >= b.y && x<= b.x+b.standardButton.xsize && y <= b.y + b.standardButton.ysize) {
return true;
}
return false;
@@ -133,7 +129,7 @@ void Menu::drawSelf(SDL_Renderer* target) {
drawToScreen(*b);
}
drawToScreen(exit);
- standardButton.getLabel(title, false)->Draw(target, 50, 50);
+ exit.standardButton.getLabel(title, false)->Draw(target, 50, 50);
}
@@ -141,10 +137,10 @@ void Menu::placeButtons() {
int nextY = 100;
int X = 50;
for (Button* it : buttons) {
- X = (globalData.xsize - standardButton.xsize)/2;
+ X = (globalData.xsize - it->standardButton.xsize)/2;
it->x = X;
it->y = nextY;
- nextY += standardButton.ysize+10;
+ nextY += it->standardButton.ysize+10;
}
exit.x = X;
exit.y = nextY;
diff --git a/source/code/MenuSystem.h b/source/code/MenuSystem.h index c4db24f..8be082d 100644 --- a/source/code/MenuSystem.h +++ b/source/code/MenuSystem.h
@@ -40,15 +40,15 @@ struct ButtonGfx
//The size of the buttons, so we don't have to ask w and h from the SDL Surfaces each time
int xsize = 0;
int ysize = 0;
- sago::SagoTextField* getLabel(const std::string& text, bool marked);
+ sago::SagoTextField* getLabel(const std::string& text, bool marked) const;
void setSurfaces();
+ std::string menu_marked = "menu_marked";
+ std::string menu_unmarked = "menu_unmarked";
private:
- std::map<std::string, std::shared_ptr<sago::SagoTextField> > labels;
- std::map<std::string, std::shared_ptr<sago::SagoTextField> > labels_marked;
+ mutable std::map<std::string, std::shared_ptr<sago::SagoTextField> > labels;
+ mutable std::map<std::string, std::shared_ptr<sago::SagoTextField> > labels_marked;
};
-extern ButtonGfx standardButton;
-
//A button
class Button
{
@@ -67,6 +67,7 @@ public:
//Where is the button on the screen
int x = 0;
int y = 0;
+ ButtonGfx standardButton;
Button() = default;
Button(const Button& b);
diff --git a/source/code/menudef.cpp b/source/code/menudef.cpp index 62df59a..317c40d 100644 --- a/source/code/menudef.cpp +++ b/source/code/menudef.cpp
@@ -218,9 +218,6 @@ void Button_testMusic::doAction() {
Mix_PlayMusic(bgMusic.get(), -1);
}
-void InitMenues() {
- standardButton.setSurfaces();
-}
static void runSinglePlayerEndless0() {
runGame(Gametype::SinglePlayerEndless, 0);
@@ -449,13 +446,13 @@ public:
game->putSampleBlocks();
}
- void placeButtons() {
+ void placeButtons() override {
int nextY = 100;
int X = 50;
for (Button* it : buttons) {
it->x = X;
it->y = nextY;
- nextY += standardButton.ysize+10;
+ nextY += it->standardButton.ysize+10;
}
exit.x = X;
exit.y = nextY;
@@ -623,7 +620,6 @@ void SafeModeMenu() {
if (Config::getInstance()->getInt("always-software")) {
return;
}
- InitMenues();
Menu safeMode(globalData.screen, _("Game did not shutdown as it should"), true);
Button bOnce;
Button bAlways;
@@ -640,7 +636,6 @@ void SafeModeMenu() {
}
void MainMenu() {
- InitMenues();
Menu m(globalData.screen,_("Block Attack - Rise of the blocks"),false);
Button bHi, bMulti, bConfigure, bHighscore, bHelp;
bHi.setLabel(_("Single player") );