foxygit / fake-08-xbox360 Log in
commit 08567417aee74ddbcec79a42e25866a69015b245
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Fri Jul 1 10:38:35 2022 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Fri Jul 1 10:38:35 2022 -0600

    Progress improving suppress pause support
---
 platform/SDL2Desktop/source/SDL2Host.cpp | 52 +++++++++++---------------------
 source/Input.cpp                         | 15 ++++++---
 source/Input.h                           |  1 +
 source/vm.cpp                            | 11 ++++---
 4 files changed, 36 insertions(+), 43 deletions(-)

diff --git a/platform/SDL2Desktop/source/SDL2Host.cpp b/platform/SDL2Desktop/source/SDL2Host.cpp
index 684e195..3fc3b29 100644
--- a/platform/SDL2Desktop/source/SDL2Host.cpp
+++ b/platform/SDL2Desktop/source/SDL2Host.cpp
@@ -80,7 +80,7 @@ Host::Host()

 InputState_t Host::scanInput(){
     currKDown = 0;
-    currKHeld = 0;
+    uint8_t kUp = 0;
     stretchKeyPressed = false;

     currKBDown = false;
@@ -127,6 +127,21 @@ InputState_t Host::scanInput(){
                     case SDLK_r:     stretchKeyPressed = true; break;
                 }
                 break;
+
+            case SDL_KEYUP:
+                switch (event.key.keysym.sym)
+                {
+                    case SDLK_ESCAPE:case SDLK_RETURN:case SDLK_RETURN2:
+                                     kUp |= P8_KEY_PAUSE; break;
+                    case SDLK_LEFT:  kUp |= P8_KEY_LEFT; break;
+                    case SDLK_RIGHT: kUp |= P8_KEY_RIGHT; break;
+                    case SDLK_UP:    kUp |= P8_KEY_UP; break;
+                    case SDLK_DOWN:  kUp |= P8_KEY_DOWN; break;
+                    case SDLK_z:     kUp |= P8_KEY_X; break;
+                    case SDLK_x:     kUp |= P8_KEY_O; break;
+                    case SDLK_c:     kUp |= P8_KEY_X; break;
+                }
+                break;

             case SDL_QUIT:
                 quit = 1;
@@ -153,39 +168,8 @@ InputState_t Host::scanInput(){
         picoMouseState |= 2;
     }

-    const Uint8* keystate = SDL_GetKeyboardState(NULL);
-
-    //continuous-response keys
-    if(keystate[SDL_SCANCODE_ESCAPE]){
-        currKHeld |= P8_KEY_PAUSE;
-    }
-    if(keystate[SDL_SCANCODE_RETURN]){
-        currKHeld |= P8_KEY_PAUSE;
-    }
-    if(keystate[SDL_SCANCODE_RETURN2]){
-        currKHeld |= P8_KEY_PAUSE;
-    }
-    if(keystate[SDL_SCANCODE_LEFT]){
-        currKHeld |= P8_KEY_LEFT;
-    }
-    if(keystate[SDL_SCANCODE_RIGHT]){
-        currKHeld |= P8_KEY_RIGHT;;
-    }
-    if(keystate[SDL_SCANCODE_UP]){
-        currKHeld |= P8_KEY_UP;
-    }
-    if(keystate[SDL_SCANCODE_DOWN]){
-        currKHeld |= P8_KEY_DOWN;
-    }
-    if(keystate[SDL_SCANCODE_Z]){
-        currKHeld |= P8_KEY_X;
-    }
-    if(keystate[SDL_SCANCODE_X]){
-        currKHeld |= P8_KEY_O;
-    }
-    if(keystate[SDL_SCANCODE_C]){
-        currKHeld |= P8_KEY_X;
-    }
+    currKHeld |= currKDown;
+    currKHeld ^= kUp;

     return InputState_t {
         currKDown,
diff --git a/source/Input.cpp b/source/Input.cpp
index 58546c5..5ec5bfc 100644
--- a/source/Input.cpp
+++ b/source/Input.cpp
@@ -5,7 +5,8 @@
 #include "PicoRam.h"

 Input::Input(PicoRam* memory):
-    _currentKDown(0)
+    _currentKDown(0),
+    _currentKHeld(0)
 {
     _memory = memory;

@@ -14,7 +15,13 @@ Input::Input(PicoRam* memory):

 void Input::SetState(uint8_t kdown, uint8_t kheld){
     _currentKDown = kdown;
-    _memory->hwState.buttonStates[0] = kheld;
+    _currentKHeld = kheld;
+    //key 6 (PAUSE MENU) only fires for one frame, even if held
+    if ((_currentKHeld & BITMASK(6)) && !(_currentKDown & BITMASK(6))) {
+        _currentKHeld = _currentKHeld & ~(BITMASK(6));
+    }
+    //memory only stores buttons 0-5, not 6 or 7
+    _memory->hwState.buttonStates[0] = kheld & 0x3F;

     uint8_t repeatDelay = _memory->hwState.btnpRepeatDelay == 0
         ? 15
@@ -28,7 +35,7 @@ void Input::SetState(uint8_t kdown, uint8_t kheld){
         ? 4
         : _memory->hwState.btnpRepeatDelay;

-    for (int i = 0; i < 8; i ++) {
+    for (int i = 0; i < 7; i ++) {
         bool down = BITMASK(i) & kheld;

         _framesHeld[i] = down ? _framesHeld[i] + 1 : 0;
@@ -62,7 +69,7 @@ void Input::SetKeyboard(bool kbDown, std::string kbKey){
 }

 uint8_t Input::btn(){
-    return _memory->hwState.buttonStates[0];
+    return _currentKHeld;
 }

 //todo: repetition behavior to match pico 8
diff --git a/source/Input.h b/source/Input.h
index 29d158e..2fb0d26 100644
--- a/source/Input.h
+++ b/source/Input.h
@@ -6,6 +6,7 @@
 class Input{
     PicoRam* _memory;
 	uint8_t _currentKDown;
+    uint8_t _currentKHeld;

 	uint16_t _framesHeld[8];

diff --git a/source/vm.cpp b/source/vm.cpp
index 8a6c159..ba3ed8d 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -423,7 +423,8 @@ void Vm::LoadCart(std::string filename){
 }

 void Vm::togglePauseMenu(){
-    if (_memory->drawState.suppressPause) {
+    _input->SetState(0, 0);
+    if (_memory->drawState.suppressPause) {
         _memory->drawState.suppressPause = 0;
         return;
     }
@@ -512,10 +513,6 @@ void Vm::UpdateAndDraw() {

     _picoFrameCount++;

-    if (_input->btnp(6)) {
-        togglePauseMenu();
-    }
-
     if (_cartChangeQueued) {
         _prevCartKey = CurrentCartFilename();
         LoadCart(_nextCartKey);
@@ -568,6 +565,10 @@ void Vm::UpdateAndDraw() {
             }
         }
         lua_pop(_luaState, 0);
+
+        if (_input->btnp(6)) {
+            togglePauseMenu();
+        }
     }

 }