foxygit / fake-08-xbox360 Log in
commit 0be1ae4e6eafce6f5c60a3eb886dcbfca09fddd8
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Thu Sep 7 18:04:34 2023 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Thu Sep 7 18:04:34 2023 -0600

    WIP - just saving progress for a branch switch
---
 platform/libretro/libretro.cpp | 141 ++++++++++++++++++++---------------------
 source/picoluaapi.cpp          |   5 +-
 source/vm.cpp                  |  26 +++++++-
 source/vm.h                    |   3 +-
 4 files changed, 97 insertions(+), 78 deletions(-)

diff --git a/platform/libretro/libretro.cpp b/platform/libretro/libretro.cpp
index b3a119e..8d9ab47 100644
--- a/platform/libretro/libretro.cpp
+++ b/platform/libretro/libretro.cpp
@@ -223,94 +223,91 @@ int flip = 0;

 EXPORT void retro_run()
 {
-    //TODO: improve this so slower hardware can play 30fps games at full speed
-    if (_vm->getTargetFps() == 60 || frame % 2 == 0)
-    {
-        input_poll_cb();
-
-        uint8_t currKDown = 0;
-        uint8_t currKHeld = 0;
-        for (int i = 0; i < 7; i++) {
-            bool down = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, buttons[i]);
-            if (down) {
-                currKHeld |= BITMASK(i);
-                //if the key is a new push this frame, mark it as down as well
-                if (!(kHeld & BITMASK(i))) {
-                    currKDown |= BITMASK(i);
-                }
+    input_poll_cb();
+
+    uint8_t currKDown = 0;
+    uint8_t currKHeld = 0;
+    for (int i = 0; i < 7; i++) {
+        bool down = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, buttons[i]);
+        if (down) {
+            currKHeld |= BITMASK(i);
+            //if the key is a new push this frame, mark it as down as well
+            if (!(kHeld & BITMASK(i))) {
+                currKDown |= BITMASK(i);
             }
         }
+    }

-        mouseBtnState = 0;
+    mouseBtnState = 0;

-        if (_memory->drawState.devkitMode) {
-            bool havePointer = false;
-            bool haveAnalog = false;
-            bool haveMouse = false;
-            bool gotTouch = false;
-
-            uint64_t flags = 0;
-            enviro_cb(RETRO_ENVIRONMENT_GET_INPUT_DEVICE_CAPABILITIES, &flags);
+    if (_memory->drawState.devkitMode) {
+        bool havePointer = false;
+        bool haveAnalog = false;
+        bool haveMouse = false;
+        bool gotTouch = false;
+
+        uint64_t flags = 0;
+        enviro_cb(RETRO_ENVIRONMENT_GET_INPUT_DEVICE_CAPABILITIES, &flags);


-            haveMouse = flags & (1 << RETRO_DEVICE_MOUSE);
-            havePointer = flags & (1 << RETRO_DEVICE_POINTER);
-            haveAnalog = flags & (1 << RETRO_DEVICE_ANALOG);
+        haveMouse = flags & (1 << RETRO_DEVICE_MOUSE);
+        havePointer = flags & (1 << RETRO_DEVICE_POINTER);
+        haveAnalog = flags & (1 << RETRO_DEVICE_ANALOG);

-            if (haveMouse) {
-                int16_t pointX = input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
-                int16_t pointY = input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
-                mouseBtnState = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED);
+        if (haveMouse) {
+            int16_t pointX = input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
+            int16_t pointY = input_state_cb(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
+            mouseBtnState = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED);

-                //this needs to be adjusted to screen scale I think?
-                picoMouseX += pointX;
-                picoMouseY += pointY;
-            }
-            else if (havePointer) {
-                int16_t pointX = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_X);
-                int16_t pointY = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_Y);
-                int16_t pressed = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED);
-
-                if (pressed) {
-                    picoMouseX = pointX * 64 / 32768 + 64;
-                    picoMouseY = pointY * 64 / 32768 + 64;
-                    mouseBtnState = 1;
-                    gotTouch = true;
-                }
+            //this needs to be adjusted to screen scale I think?
+            picoMouseX += pointX;
+            picoMouseY += pointY;
+        }
+        else if (havePointer) {
+            int16_t pointX = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_X);
+            int16_t pointY = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_Y);
+            int16_t pressed = input_state_cb(0, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED);
+
+            if (pressed) {
+                picoMouseX = pointX * 64 / 32768 + 64;
+                picoMouseY = pointY * 64 / 32768 + 64;
+                mouseBtnState = 1;
+                gotTouch = true;
             }
+        }

-            if (haveAnalog && !gotTouch) {
-                // Read the analog X/Y
-                int16_t analogX = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
-                int16_t analogY = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
-                // Pre-calculate where the cursor will be
-                int16_t tempX = picoMouseX + (((PicoScreenWidth / 32767.0f ) * analogX)/32);
-                int16_t tempY = picoMouseY + (((PicoScreenHeight / 32767.0f ) * analogY)/32);
-                // Make sure the cursor stays within the screen
-                if ( ((tempX - 0) | (PicoScreenWidth - tempX)) >= 0) {
-                    picoMouseX = tempX;
-                }
-                if ( ((tempY - 0) | (PicoScreenHeight - tempY)) >= 0) {
-                    picoMouseY = tempY;
-                }
-                // Grab the state of the X button
-                mouseBtnState = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X);
+        if (haveAnalog && !gotTouch) {
+            // Read the analog X/Y
+            int16_t analogX = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
+            int16_t analogY = input_state_cb(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
+            // Pre-calculate where the cursor will be
+            int16_t tempX = picoMouseX + (((PicoScreenWidth / 32767.0f ) * analogX)/32);
+            int16_t tempY = picoMouseY + (((PicoScreenHeight / 32767.0f ) * analogY)/32);
+            // Make sure the cursor stays within the screen
+            if ( ((tempX - 0) | (PicoScreenWidth - tempX)) >= 0) {
+                picoMouseX = tempX;
             }
+            if ( ((tempY - 0) | (PicoScreenHeight - tempY)) >= 0) {
+                picoMouseY = tempY;
+            }
+            // Grab the state of the X button
+            mouseBtnState = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X);
         }
-
-        setInputState(currKDown, currKHeld, picoMouseX, picoMouseY, mouseBtnState);
-
-        _vm->UpdateAndDraw();
-        kHeld = currKHeld;
-        kDown = currKDown;
+    }
+
+    setInputState(currKDown, currKHeld, picoMouseX, picoMouseY, mouseBtnState);

-        if (frame % 2 == 0) {
-            _audio->FillAudioBuffer(&audioBuffer, 0, SAMPLESPERFRAME);
-            audio_batch_cb(audioBuffer, SAMPLESPERFRAME);
-        }
+    _vm->Step();
+    //_vm->UpdateAndDraw();
+    kHeld = currKHeld;
+    kDown = currKDown;

+    if (frame % 2 == 0) {
+        _audio->FillAudioBuffer(&audioBuffer, 0, SAMPLESPERFRAME);
+        audio_batch_cb(audioBuffer, SAMPLESPERFRAME);
     }

+
     uint8_t* picoFb = _vm->GetPicoInteralFb();
     uint8_t* screenPaletteMap = _vm->GetScreenPaletteMap();

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 6c3db0e..c8a205d 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -819,7 +819,8 @@ int stat(lua_State *L) {
         break;
         //target framerate
         case 8:
-            lua_pushnumber(L, _vmForLuaApi->getTargetFps());
+            //lua_pushnumber(L, _vmForLuaApi->getTargetFps());
+            lua_pushnumber(L, _vmForLuaApi->getFps());
             return 1;
         break;
         //16-19 audio sfx currently playing
@@ -1287,7 +1288,7 @@ int reset(lua_State *L) {
 }

 int setFps(lua_State *L){
-    _vmForLuaApi->setTargetFps(lua_tointeger(L, 1));
+    //_vmForLuaApi->setTargetFps(lua_tointeger(L, 1));

     return 0;
 }
diff --git a/source/vm.cpp b/source/vm.cpp
index c42fd19..e52cd55 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -564,6 +564,25 @@ void Vm::deserializeCartDataToMemory(std::string cartDataStr) {
     }
 }

+bool Vm::Step(){
+    bool ret = false;
+    lua_getglobal(_luaState, "__z8_tick");
+    int status = lua_pcall(_luaState, 0, 1, 0);
+    if (status != LUA_OK)
+    {
+        char const *message = lua_tostring(_luaState, -1);
+        _cartLoadError = "Error in main loop: " + std::string(message);
+    }
+    else
+    {
+        ret = (int)lua_tonumber(_luaState, -1) >= 0;
+    }
+    lua_pop(_luaState, 1);
+
+
+    return ret;
+}
+
 // void Vm::UpdateAndDraw() {
 //     update_buttons();

@@ -718,10 +737,10 @@ void Vm::GameLoop() {
     while (_host->shouldRunMainLoop())
     {
         //shouldn't need to set this every frame
-        _host->setTargetFps(_targetFps);
+        //_host->setTargetFps(_targetFps);

         //is this better at the end of the loop?
-        _host->waitForTargetFps();
+        //_host->waitForTargetFps();

         if (_host->shouldQuit()) break; // break in order to return to hbmenu
         //this should probably be handled just in the host class
@@ -730,7 +749,8 @@ void Vm::GameLoop() {
         //update buttons needs to be callable from the cart, and also flip
         //it should update call the pico part of scanInput and set the values in memory
         //then we don't need to pass them in here
-        UpdateAndDraw();
+        //UpdateAndDraw();
+        Step();

         uint8_t* picoFb = GetPicoInteralFb();
         uint8_t* screenPaletteMap = GetScreenPaletteMap();
diff --git a/source/vm.h b/source/vm.h
index 75bfd2d..a940c9d 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -32,7 +32,7 @@ class Vm {

     bool _cleanupDeps;

-    //int _targetFps;
+    int _targetFps;

     int _picoFrameCount;
     //bool _hasUpdate;
@@ -75,6 +75,7 @@ class Vm {
     void LoadCart(const unsigned char* cartData, size_t size, bool loadBiosOnFail = true);

     // void UpdateAndDraw();
+    bool Step();

     uint8_t* GetPicoInteralFb();
     uint8_t* GetScreenPaletteMap();