diff --git a/src/MenuSystem.cpp b/src/MenuSystem.cpp index b383a93..54f5666 100644 --- a/src/MenuSystem.cpp +++ b/src/MenuSystem.cpp @@ -57,11 +57,7 @@ sago::SagoTextField* ButtonGfx::getLabel(const std::string& text) { return labels[text].get(); } -Button::Button() { - label = ""; - marked = false; - action = nullptr; - popOnRun = false; +Button::Button() { } Button::~Button() { @@ -71,7 +67,6 @@ Button::Button(const Button& b) { label = b.label; marked = b.marked; action = b.action; - popOnRun = false; } void Button::setLabel(const std::string& text) { @@ -149,14 +144,13 @@ void Menu::addButton(Button* b) { Menu::Menu(SDL_Renderer* screen) { this->screen = screen; - buttons = std::vector(10); + buttons.reserve(10); isSubmenu = true; exit.setLabel( _("Back") ); } Menu::Menu(SDL_Renderer* screen,bool submenu) { this->screen = screen; - buttons = std::vector(0); isSubmenu = submenu; if (isSubmenu) { exit.setLabel( _("Back") ); @@ -168,7 +162,6 @@ Menu::Menu(SDL_Renderer* screen,bool submenu) { Menu::Menu(SDL_Renderer* screen, const std::string& title, bool submenu) { this->screen = screen; - buttons = std::vector(0); isSubmenu = submenu; this->title = title; if (isSubmenu) { @@ -250,12 +243,12 @@ bool Menu::IsActive() { void Menu::Draw(SDL_Renderer* target) { placeButtons(); drawSelf(target); -#if DEBUG +//#if DEBUG static unsigned long int Frames; static unsigned long int Ticks; static char FPS[10]; static sago::SagoTextField fpsField; - sagoTextSetBlueFont(fpsField); + fpsField.SetHolder(globalData.dataHolder); Frames++; if (SDL_GetTicks() >= Ticks + 1000) { if (Frames > 999) { @@ -266,10 +259,11 @@ void Menu::Draw(SDL_Renderer* target) { Ticks = SDL_GetTicks(); } fpsField.SetText(FPS); - fpsField.Draw(globalData.screen, 800, 4); -#endif + fpsField.Draw(globalData.screen, globalData.xsize-4, 4, sago::SagoTextField::Alignment::right); +//#endif } void Menu::ProcessInput(const SDL_Event& event, bool& processed) { + UpdateMouseCoordinates(event, globalData.mousex, globalData.mousey); if (isUpEvent(event)) { marked--; if (marked<0) { diff --git a/src/MenuSystem.h b/src/MenuSystem.h index cf3016f..f6703af 100644 --- a/src/MenuSystem.h +++ b/src/MenuSystem.h @@ -53,7 +53,7 @@ class Button { private: //Pointer to a callback function. - void (*action)(void); + void (*action)(void) = nullptr; //If true the menu should also be closed then the button is clicked bool popOnRun = false; diff --git a/src/global.hpp b/src/global.hpp index b1d73d0..b116be4 100644 --- a/src/global.hpp +++ b/src/global.hpp @@ -26,5 +26,7 @@ https://github.com/sago007/saland #include "saland/globals.hpp" +void UpdateMouseCoordinates(const SDL_Event& event, int& mousex, int& mousey); + #endif /* GLOBAL_HPP */ diff --git a/src/saland.cpp b/src/saland.cpp index 38d7339..d68e87c 100644 --- a/src/saland.cpp +++ b/src/saland.cpp @@ -111,6 +111,29 @@ private: sago::SagoTextBox testBox; }; +/** + * This function reads the mouse coordinates from a relevant event. + * Unlike SDL_GetMouseState this works even if SDL_RenderSetLogicalSize is used + * @param event + * @param mousex + * @param mousey + */ +void UpdateMouseCoordinates(const SDL_Event& event, int& mousex, int& mousey) { + switch (event.type) { + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + mousex = event.button.x; + mousey = event.button.y; + break; + case SDL_MOUSEMOTION: + mousex = event.motion.x; + mousey = event.motion.y; + break; + default: + break; + } +} + void RunGameState(sago::GameStateInterface& state ) { bool done = false; //We are done! while (!done && !globalData.isShuttingDown) {