diff --git a/source/code/MenuSystem.cpp b/source/code/MenuSystem.cpp index bd14469..3227275 100644 --- a/source/code/MenuSystem.cpp +++ b/source/code/MenuSystem.cpp @@ -298,14 +298,15 @@ bool isConfirmEvent(const SDL_Event& event) { void Menu::run() { running = true; bool bMouseUp = false; - long oldmousex = mousex; - long oldmousey = mousey; + int oldmousex = mousex; + int oldmousey = mousey; while (running && !Config::getInstance()->isShuttingDown()) { if (!(highPriority)) { SDL_Delay(10); } SDL_Event event; while ( SDL_PollEvent(&event) ) { + UpdateMouseCoordinates(event, mousex, mousey); if ( event.type == SDL_QUIT ) { Config::getInstance()->setShuttingDown(5); running = false; @@ -347,7 +348,7 @@ void Menu::run() { buttons.at(i)->marked = (i == marked); } exit.marked = (marked == (int)buttons.size()); - Uint8 buttonState = SDL_GetMouseState(&mousex,&mousey); + Uint8 buttonState = SDL_GetMouseState(nullptr,nullptr); // If the mouse button is released, make bMouseUp equal true if ( (buttonState&SDL_BUTTON(1))==0) { bMouseUp=true; diff --git a/source/code/ScoresDisplay.cpp b/source/code/ScoresDisplay.cpp index 6e42914..e2ba857 100644 --- a/source/code/ScoresDisplay.cpp +++ b/source/code/ScoresDisplay.cpp @@ -170,6 +170,8 @@ void ScoresDisplay::Draw(SDL_Renderer*) { void ScoresDisplay::ProcessInput(const SDL_Event& event, bool& processed) { + UpdateMouseCoordinates(event, mousex, mousey); + if (isLeftEvent(event)) { page++; if (page>=numberOfPages) { @@ -193,9 +195,6 @@ void ScoresDisplay::ProcessInput(const SDL_Event& event, bool& processed) { } void ScoresDisplay::Update() { - int mousex, mousey; - SDL_GetMouseState(&mousex,&mousey); - // If the mouse button is released, make bMouseUp equal true if (!SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) { bMouseUp=true; diff --git a/source/code/ScoresDisplay.hpp b/source/code/ScoresDisplay.hpp index 20922e7..f8f5d3a 100644 --- a/source/code/ScoresDisplay.hpp +++ b/source/code/ScoresDisplay.hpp @@ -43,6 +43,8 @@ public: int scoreY = 0; int buttonXsize = 0; int buttonYsize = 0; + int mousex = 0; + int mousey = 0; private: void DrawHighscores(int x, int y, bool endless); void DrawStats(); diff --git a/source/code/global.hpp b/source/code/global.hpp index 71753b7..10ada65 100644 --- a/source/code/global.hpp +++ b/source/code/global.hpp @@ -39,6 +39,7 @@ enum class Gametype { SinglePlayerEndless=0, SinglePlayerTimeTrial=1, StageClear int runGame(Gametype gametype,int level); bool OpenDialogbox(int x, int y, std::string& name, const std::string& header); void DrawBackground(SDL_Renderer* target); +void UpdateMouseCoordinates(const SDL_Event& event, int& mousex, int& mousey); extern sago::SagoSprite menuMarked; extern sago::SagoSprite menuUnmarked; diff --git a/source/code/main.cpp b/source/code/main.cpp index 51aaf0b..176c571 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp @@ -280,6 +280,29 @@ void DrawBackground(SDL_Renderer* target) { backgroundImage.DrawScaled(target, SDL_GetTicks(), 0, 0, xsize, ysize); } +/** + * 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; + } +} + //The small things that are faaling when you clear something class ABall { private: @@ -598,10 +621,10 @@ void RunGameState(sago::GameStateInterface& state ) { SDL_Delay(1); SDL_Event event; - SDL_GetMouseState(&mousex,&mousey); bool mustWriteScreenshot = false; while ( SDL_PollEvent(&event) ) { + UpdateMouseCoordinates(event, mousex, mousey); if ( event.type == SDL_QUIT ) { Config::getInstance()->setShuttingDown(5); done = true; @@ -913,6 +936,8 @@ int PuzzleLevelSelect(int Type) { SDL_Event event; while ( SDL_PollEvent(&event) ) { + UpdateMouseCoordinates(event, mousex, mousey); + if ( event.type == SDL_QUIT ) { Config::getInstance()->setShuttingDown(5); levelNr = -1; @@ -954,20 +979,22 @@ int PuzzleLevelSelect(int Type) { SDL_GetKeyboardState(nullptr); - SDL_GetMouseState(&mousex,&mousey); if (mousex != oldmousex || mousey != oldmousey) { int tmpSelected = -1; int j; - for (j = 0; (tmpSelected == -1) && ( (jsetShuttingDown(5); done = 1; @@ -1943,8 +1971,6 @@ int runGame(Gametype gametype, int level) { } } //while event PollEvent - read keys - SDL_GetMouseState(&mousex,&mousey); - // If the mouse button is released, make bMouseUp equal true if (!SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) { bMouseUp=true; @@ -2102,7 +2128,6 @@ int runGame(Gametype gametype, int level) { //Once evrything has been checked, update graphics MoveBlockGameSdls(theGame, theGame2); DrawEverything(xsize,ysize,&theGame,&theGame2); - SDL_GetMouseState(&mousex,&mousey); //Draw the mouse: mouse.Draw(screen, SDL_GetTicks(), mousex, mousey); SDL_RenderPresent(screen);