diff --git a/source/code/BlockGame.cpp b/source/code/BlockGame.cpp index fe5fd87..85e7cd0 100644 --- a/source/code/BlockGame.cpp +++ b/source/code/BlockGame.cpp @@ -1161,6 +1161,7 @@ void BlockGame::MoveCursor(char way) { //switches the two blocks at the cursor position, unless game over void BlockGame::SwitchAtCursor() { + Update(); //Ensure that everything is in a stable state. if ((board[cursorx][cursory+1]<7) && (board[cursorx+1][cursory+1]<7) && (!bGameOver) && ((!puzzleMode)||(MovesLeft>0)) && (gameStartedAt& poppedData) { this->garbageSendQueue.clear(); } +void BlockGame::GetMouseCursor(bool& pressed, int& x, int&y) const { + if (mouse_cursorx < 0 || mouse_cursory < 0 || mouse_cursorx >=6 || mouse_cursory > 13) { + pressed = false; + x = 0; + y = 0; + return; + } + pressed = true; + x = mouse_cursorx; + y = mouse_cursory; +} + +void BlockGame::MouseDown(int x, int y) { + if (AI_Enabled) { + //AI may not use mouse move. It must use the controller + return; + } + mouse_cursorx = x; + mouse_cursory = y; +} + +void BlockGame::MouseMove(int x) { + if (AI_Enabled) { + //AI may not use mouse move. It must use the controller + return; + } + if (mouse_cursorx < 0) { + return; + } + if (x < 0 || x >= 6) { + return; + } + if (x > mouse_cursorx) { + MoveCursorTo(mouse_cursorx, mouse_cursory); + ++mouse_cursorx; + SwitchAtCursor(); + } + if (x < mouse_cursorx) { + --mouse_cursorx; + MoveCursorTo(mouse_cursorx, mouse_cursory); + SwitchAtCursor(); + } +} + +void BlockGame::MouseUp() { + mouse_cursorx = -1; + mouse_cursory = -1; +} //Play the next level void nextLevel(BlockGame& g, unsigned int ticks) { BlockGameStartInfo s; diff --git a/source/code/BlockGame.hpp b/source/code/BlockGame.hpp index e6690a2..1693dd9 100644 --- a/source/code/BlockGame.hpp +++ b/source/code/BlockGame.hpp @@ -131,6 +131,8 @@ protected: int chain = 0; int cursorx = 0; //stores cursor position int cursory = 0; // -||- + int mouse_cursorx = -1; //Stores the mouse hold cursor. -1 if nothing is selected. + int mouse_cursory = -1; double speed = 0.0; double baseSpeed = 0.0; //factor for speed. Lower value = faster gameplay int score = 0; @@ -189,6 +191,10 @@ public: int GetCursorX() const; int GetCursorY() const; void MoveCursorTo(int x, int y); + void GetMouseCursor(bool& pressed, int& x, int&y) const; + void MouseDown(int x, int y); //Send then the mouse is pressed + void MouseMove(int x); //Send then the mouse moves + void MouseUp(); //Send then the mouse goes up bool GetIsWinner() const; bool isSinglePuzzle() const; int getLevel() const; diff --git a/source/code/main.cpp b/source/code/main.cpp index 20b80f1..2beb2d7 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp @@ -569,6 +569,24 @@ public: int GetTopY() const { return topy; } + + /** + * Retrives the coordinate to a given brick based on mouse input. + * @param match true if the coordinate are within borders + * @param mousex mouse coordinate input + * @param mousey mouse coordiante input + * @param x brick x. Unchanged if outside border + * @param y brick y. Unchanged if outside border + */ + void GetBrickCoordinateFromMouse(bool& match, int mousex, int mousey, int& x, int& y) const { + if (mousex < topx || mousex > topx+50*6 || mousey < topy || mousey > topy+50*12) { + match = false; + return; + } + match = true; + x = (mousex-topx) / 50; + y = (50*12+topy-mousey-pixels) / 50; + } void AddText(int x, int y, const std::string& text, int time) const override { theTextManager.addText(topx-10+x*bsize, topy+12*bsize-y*bsize, text, time); @@ -1584,14 +1602,14 @@ int main(int argc, char* argv[]) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { sago::SagoFatalErrorF("Unable to init SDL: %s", SDL_GetError()); } - if (SDL_Init(SDL_INIT_GAMECONTROLLER ) != 0) { + if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER ) != 0) { cerr << "Warning: Game controller failed to initialize. Reason: " << SDL_GetError() << endl; } InitGameControllers(); TTF_Init(); atexit(SDL_Quit); //quits SDL when the game stops for some reason (like you hit exit or Esc) - SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); + //SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE); theTextManager = TextManager(); @@ -2045,67 +2063,63 @@ int runGame(int gametype, int level) { if (isPlayerPushEvent(1, event)) { theGame.PushLine(); } - } //while event PollEvent - read keys - - SDL_GetMouseState(&mousex,&mousey); - - /******************************************************************** - **************** Here comes mouse play ****************************** - ********************************************************************/ - - if ((mouseplay1)&&( ( (!editorMode)&&(!theGame.GetAIenabled()) ) ||(editorModeTest))) //player 1 - if ((mousex > 50)&&(mousey>100)&&(mousex<50+300)&&(mousey<100+600)) { - int yLine, xLine; - yLine = ((100+600)-(mousey-100+theGame.GetPixels()))/50; - xLine = (mousex-50+25)/50; - yLine-=2; - xLine-=1; - if ((yLine>10)&&(theGame.GetTowerHeight()<12)) { - yLine=10; - } - if (((theGame.GetPixels()==50)||(theGame.GetPixels()==0)) && (yLine>11)) { - yLine=11; - } - if (yLine<0) { - yLine=0; - } - if (xLine<0) { - xLine=0; - } - if (xLine>4) { - xLine=4; - } - theGame.MoveCursorTo(xLine,yLine); + + if (event.type == SDL_MOUSEBUTTONDOWN) { + cout << "Mouse button down event" << endl; + if (event.button.button == SDL_BUTTON_LEFT) { + cout << "Pressed at " << event.button.x << "," << event.button.y << endl; + bool pressed = false; + int x = 0; + int y = 0; + theGame.GetBrickCoordinateFromMouse(pressed, event.button.x, event.button.y, x, y); + if (pressed) { + theGame.MouseDown(x, y); + } + theGame2.GetBrickCoordinateFromMouse(pressed, event.button.x, event.button.y, x, y); + if (pressed) { + theGame2.MouseDown(x, y); + } + } } - - if ((mouseplay2)&&(!editorMode)&&(!theGame2.GetAIenabled())) //player 2 - if ((mousex > xsize-500)&&(mousey>100)&&(mousex10)&&(theGame2.GetTowerHeight()<12)) { - yLine=10; - } - if (((theGame2.GetPixels()==50)||(theGame2.GetPixels()==0)) && (yLine>11)) { - yLine=11; + if (event.type == SDL_MOUSEBUTTONUP) { + if (event.button.button == SDL_BUTTON_LEFT) { + cout << "Button released" << endl; + theGame.MouseUp(); + theGame2.MouseUp(); } - if (yLine<0) { - yLine=0; - } - if (xLine<0) { - xLine=0; + } + if (event.type == SDL_MOUSEMOTION) { + //cout << "Moved" << endl; + bool pressed = false; + int x = 0; + int y = 0; + theGame.GetMouseCursor(pressed, x, y); + if (pressed) { + int mx = 0; + int my = 0; + theGame.GetBrickCoordinateFromMouse(pressed, event.motion.x, event.motion.y, mx, my); + if (pressed) { + if (mx != x) { + theGame.MouseMove(mx); + } + } } - if (xLine>4) { - xLine=4; + theGame2.GetMouseCursor(pressed, x, y); + if (pressed) { + int mx = 0; + int my = 0; + theGame2.GetBrickCoordinateFromMouse(pressed, event.motion.x, event.motion.y, mx, my); + if (pressed) { + if (mx != x) { + theGame2.MouseMove(mx); + } + } } - theGame2.MoveCursorTo(xLine,yLine); + } + } //while event PollEvent - read keys - /******************************************************************** - **************** Here ends mouse play ******************************* - ********************************************************************/ + SDL_GetMouseState(&mousex,&mousey); // If the mouse button is released, make bMouseUp equal true if (!SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) { @@ -2124,24 +2138,6 @@ int runGame(int gametype, int level) { bMouseUp = false; DrawIMG(backgroundImage, screen, 0, 0); - - /******************************************************************** - **************** Here comes mouse play ****************************** - ********************************************************************/ - { - if (mouseplay1 && !theGame.GetAIenabled()) //player 1 - if ((mousex > 50)&&(mousey>100)&&(mousex<50+300)&&(mousey<100+600)) { - theGame.SwitchAtCursor(); - } - if (mouseplay2 && !theGame2.GetAIenabled()) //player 2 - if ((mousex > xsize-500)&&(mousey>100)&&(mousex theGame.GetTopX()+cordNextButton.x) &&(mousex < theGame.GetTopX()+cordNextButton.x+cordNextButton.xsize) &&(mousey > theGame.GetTopY()+cordNextButton.y)&&(mousey < theGame.GetTopY()+cordNextButton.y+cordNextButton.ysize)) { @@ -2166,25 +2162,6 @@ int runGame(int gametype, int level) { //Mouse button 2: if ((SDL_GetMouseState(nullptr,nullptr)&SDL_BUTTON(3))==SDL_BUTTON(3) && bMouseUp2) { bMouseUp2=false; //The button is pressed - /******************************************************************** - **************** Here comes mouse play ****************************** - ********************************************************************/ - - if (mouseplay1 && !theGame.GetAIenabled()) { - //player 1 - if ((mousex > 50)&&(mousey>100)&&(mousex<50+300)&&(mousey<100+600)) { - theGame.PushLine(); - } - } - if (mouseplay2 && !theGame2.GetAIenabled()) { - //player 2 - if ((mousex > xsize-500)&&(mousey>100)&&(mousex