git repos / blockattack-game

commit 6e6e44b7

sago007 · 2016-10-18 15:30
6e6e44b782941b626f402c64255ada6b83dde28a patch · browse files
parent b5d3c879bf077d8eb40c2f8001c9981392bd8a90

Now updated the mouse coordinates from event rather than reading the raw value. This works better with resizeing

Changed files

M source/code/MenuSystem.cpp before
M source/code/ScoresDisplay.cpp before
M source/code/ScoresDisplay.hpp before
M source/code/global.hpp before
M source/code/main.cpp before
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) && ( (j<nrOfLevels/10)||((j<nrOfLevels/10+1)&&(nrOfLevels%10 != 0)) ); j++)
+ for (j = 0; (tmpSelected == -1) && ( (j<nrOfLevels/10)||((j<nrOfLevels/10+1)&&(nrOfLevels%10 != 0)) ); j++) {
if ((60+j*50<mousey-yplace)&&(mousey-yplace<j*50+92)) {
tmpSelected = j*10;
}
- if (tmpSelected != -1)
- for (int k = 0; (( (!(nrOfLevels%10) || k<nrOfLevels-10*(j-1)) )&&(k<10)); k++)
+ }
+ if (tmpSelected != -1) {
+ for (int k = 0; (( (!(nrOfLevels%10) || k<nrOfLevels-10*(j-1)) )&&(k<10)); k++) {
if ((10+k*50<mousex-xplace)&&(mousex-xplace<k*50+42)) {
tmpSelected +=k;
selected = tmpSelected;
}
+ }
+ }
}
oldmousey = mousey;
oldmousex= mousex;
@@ -1682,6 +1709,7 @@ int runGame(Gametype gametype, int level) {
SDL_Event event;
while ( SDL_PollEvent(&event) ) {
+ UpdateMouseCoordinates(event, mousex, mousey);
if ( event.type == SDL_QUIT ) {
Config::getInstance()->setShuttingDown(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);