foxygit / fake-08-xbox360 Log in
commit c8bbaba5ba026a7702f59994a9a5d4990561e459
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Thu Apr 15 22:35:10 2021 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Thu Apr 15 22:35:10 2021 -0600

    WIP getting mouse actually working on desktop
---
 platform/SDL2Common/source/sdl2basehost.cpp | 10 ++++++++++
 platform/SDL2Desktop/Makefile               |  2 +-
 platform/SDL2Desktop/source/SDL2Host.cpp    | 24 +++++++++++++++++++++++-
 source/Input.cpp                            | 18 ++++++++++++++++++
 source/Input.h                              |  9 +++++++++
 source/host.h                               |  4 ++++
 source/hostVmShared.h                       |  4 ++++
 source/picoluaapi.cpp                       | 15 +++++++++++++++
 source/vm.cpp                               |  6 ++++++
 9 files changed, 90 insertions(+), 2 deletions(-)

diff --git a/platform/SDL2Common/source/sdl2basehost.cpp b/platform/SDL2Common/source/sdl2basehost.cpp
index b9dc549..0c62f74 100644
--- a/platform/SDL2Common/source/sdl2basehost.cpp
+++ b/platform/SDL2Common/source/sdl2basehost.cpp
@@ -127,6 +127,7 @@ void _changeStretch(StretchOption newStretch){
         _screenHeight = _maxNoStretchHeight;
     }

+
     DestR.x = _windowWidth / 2 - _screenWidth / 2;
     DestR.y = _windowHeight / 2 - _screenHeight / 2;
     DestR.w = _screenWidth;
@@ -216,6 +217,11 @@ void Host::oneTimeSetup(Color* paletteColors, Audio* audio){
     loadSettingsIni();

     _changeStretch(stretch);
+
+    scaleX = _screenWidth / (float)PicoScreenWidth;
+    scaleY = _screenHeight / (float)PicoScreenHeight;
+    mouseOffsetX = DestR.x;
+    mouseOffsetY = DestR.y;
 }

 void Host::oneTimeCleanup(){
@@ -253,6 +259,10 @@ void Host::changeStretch(){
         _changeStretch(newStretch);

         stretch = newStretch;
+        scaleX = _screenWidth / (float)PicoScreenWidth;
+        scaleY = _screenHeight / (float)PicoScreenHeight;
+        mouseOffsetX = DestR.x;
+        mouseOffsetY = DestR.y;
     }
 }

diff --git a/platform/SDL2Desktop/Makefile b/platform/SDL2Desktop/Makefile
index 134474d..5036806 100644
--- a/platform/SDL2Desktop/Makefile
+++ b/platform/SDL2Desktop/Makefile
@@ -16,7 +16,7 @@ INCLUDES	:=	${INCLUDES}
 #---------------------------------------------------------------------------------
 CC = $(CXX)

-CFLAGS	:=	-g -Wall -O2 -ffunction-sections \
+CFLAGS	:=	-g -Wall -ffunction-sections \
 			$(DEFINES)

 CFLAGS	+=	$(INCLUDE) -DVER_STR=\"$(APP_VERSION)\"
diff --git a/platform/SDL2Desktop/source/SDL2Host.cpp b/platform/SDL2Desktop/source/SDL2Host.cpp
index abd7364..4c931fc 100644
--- a/platform/SDL2Desktop/source/SDL2Host.cpp
+++ b/platform/SDL2Desktop/source/SDL2Host.cpp
@@ -92,6 +92,25 @@ InputState_t Host::scanInput(){
         }
     }

+    int mouseX = 0;
+	int mouseY = 0;
+    uint32_t sdlMouseBtnState = SDL_GetMouseState(&mouseX, &mouseY);
+    //adjust for scale
+    mouseX -= mouseOffsetX;
+    mouseY -= mouseOffsetY;
+    mouseX /= scaleX;
+    mouseY /= scaleY;
+    uint8_t picoMouseState = 0;
+    if (sdlMouseBtnState & SDL_BUTTON(SDL_BUTTON_LEFT)) {
+        picoMouseState &= 1;
+    }
+    if (sdlMouseBtnState & SDL_BUTTON(SDL_BUTTON_MIDDLE)) {
+        picoMouseState &= 4;
+    }
+    if (sdlMouseBtnState & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
+        picoMouseState &= 2;
+    }
+
     const Uint8* keystate = SDL_GetKeyboardState(NULL);

     //continuous-response keys
@@ -119,7 +138,10 @@ InputState_t Host::scanInput(){

     return InputState_t {
         currKDown,
-        currKHeld
+        currKHeld,
+        (int16_t)mouseX,
+        (int16_t)mouseY,
+        picoMouseState
     };
 }

diff --git a/source/Input.cpp b/source/Input.cpp
index 0c035af..a4d4d95 100644
--- a/source/Input.cpp
+++ b/source/Input.cpp
@@ -50,6 +50,12 @@ void Input::SetState(uint8_t kdown, uint8_t kheld){
     }
 }

+void Input::SetMouse(int16_t mouseX, int16_t mouseY, uint8_t mouseBtnState){
+    _mouseX = mouseX;
+    _mouseY = mouseY;
+    _mouseBtnState = _mouseBtnState;
+}
+
 uint8_t Input::btn(){
     return _memory->hwState.buttonStates[0];
 }
@@ -78,3 +84,15 @@ bool Input::btnp(uint8_t i, uint8_t p){
     //no multiplayer support for now
     return p == 0 ? btnp(i) : 0;
 }
+
+int16_t Input::getMouseX() {
+    return _mouseX;
+}
+
+int16_t Input::getMouseY() {
+    return _mouseY;
+}
+
+uint8_t Input::getMouseBtnState() {
+    return _mouseBtnState;
+}
diff --git a/source/Input.h b/source/Input.h
index 84789ae..2e49d47 100644
--- a/source/Input.h
+++ b/source/Input.h
@@ -8,10 +8,15 @@ class Input{
 	uint8_t _currentKDown;

 	uint16_t _framesHeld[8];
+
+    int16_t _mouseX;
+    int16_t _mouseY;
+    uint8_t _mouseBtnState;

     public:
     Input(PicoRam* memory);
     void SetState(uint8_t kdown, uint8_t kheld);
+    void SetMouse(int16_t mouseX, int16_t mouseY, uint8_t mouseBtnState);

 	uint8_t btn();
     uint8_t btnp();
@@ -21,4 +26,8 @@ class Input{

 	bool btn(uint8_t i, uint8_t p);
     bool btnp(uint8_t i, uint8_t p);
+
+    int16_t getMouseX();
+    int16_t getMouseY();
+    uint8_t getMouseBtnState();
 };
\ No newline at end of file
diff --git a/source/host.h b/source/host.h
index 83c58f6..c984550 100644
--- a/source/host.h
+++ b/source/host.h
@@ -21,6 +21,10 @@ class Host {
     bool rDown = false;
     bool stretchKeyPressed = false;
     StretchOption stretch = PixelPerfectStretch;
+    float scaleX = 1.0;
+    float scaleY = 1.0;
+    int mouseOffsetX = 0;
+    int mouseOffsetY = 0;
     int quit = 0;


diff --git a/source/hostVmShared.h b/source/hostVmShared.h
index b6a1749..186f721 100644
--- a/source/hostVmShared.h
+++ b/source/hostVmShared.h
@@ -31,4 +31,8 @@ struct Bgr24Col {
 struct InputState_t {
 	uint8_t KDown;
 	uint8_t KHeld;
+
+	int16_t mouseX;
+	int16_t mouseY;
+	uint8_t mouseBtnState;
 };
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 9fb11a0..d1f7169 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -797,6 +797,21 @@ int stat(lua_State *L) {
             lua_pushnumber(L, _vmForLuaApi->getTargetFps());
             return 1;
         break;
+        //mouse x
+        case 32:
+            lua_pushnumber(L, _inputForLuaApi->getMouseX());
+            return 1;
+        break;
+        //mouse y
+        case 33:
+            lua_pushnumber(L, _inputForLuaApi->getMouseY());
+            return 1;
+        break;
+        //mouse btn state
+        case 34:
+            lua_pushnumber(L, _inputForLuaApi->getMouseBtnState());
+            return 1;
+        break;
         //Current Year
         case 90:
             lua_pushnumber(L, _vmForLuaApi->getYear());
diff --git a/source/vm.cpp b/source/vm.cpp
index f693985..d1689d3 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -827,6 +827,12 @@ void Vm::update_buttons() {
     //get button states from hardware
     auto inputState = _host->scanInput();
     _input->SetState(inputState.KDown, inputState.KHeld);
+    if (_memory->drawState.devkitMode) {
+        _input->SetMouse(inputState.mouseX, inputState.mouseY, inputState.mouseBtnState);
+    }
+    else {
+        _input->SetMouse(0, 0, 0);
+    }
 }

 void Vm::vm_flip() {