Skip to content

Commit

Permalink
Stop using a global object for button graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
sago007 committed Nov 10, 2023
1 parent 883f549 commit 60b2bac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
22 changes: 9 additions & 13 deletions source/code/MenuSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ Source information and contacts persons can be found at
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();
Expand All @@ -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()) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand All @@ -133,18 +129,18 @@ 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);
}


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;
Expand Down
11 changes: 6 additions & 5 deletions source/code/MenuSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -67,6 +67,7 @@ class Button
//Where is the button on the screen
int x = 0;
int y = 0;
ButtonGfx standardButton;

Button() = default;
Button(const Button& b);
Expand Down
9 changes: 2 additions & 7 deletions source/code/menudef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ void Button_testMusic::doAction() {
Mix_PlayMusic(bgMusic.get(), -1);
}

void InitMenues() {
standardButton.setSurfaces();
}

static void runSinglePlayerEndless0() {
runGame(Gametype::SinglePlayerEndless, 0);
Expand Down Expand Up @@ -449,13 +446,13 @@ class ThemesMenu : public Menu {
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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") );
Expand Down

0 comments on commit 60b2bac

Please sign in to comment.