foxygit / fake-08-xbox360 Log in
commit 9e1631a688cc1a788626e6c276dfa29deb27a7fa
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sat Feb 13 10:12:21 2021 -0700
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Sat Feb 13 10:12:21 2021 -0700

    WIP adding pause menu
---
 source/p8GlobalLuaFunctions.h |  54 ++++++++++++++++++++++
 source/picoluaapi.cpp         |  12 +++++
 source/picoluaapi.h           |   2 +
 source/vm.cpp                 | 105 +++++++++++++++++++++++++++++++-----------
 source/vm.h                   |   6 +++
 5 files changed, 152 insertions(+), 27 deletions(-)

diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index c7bec47..cddfe2e 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -113,6 +113,60 @@ function menuitem()
 --noop placeholder for now
 end

+
+__f08_menu_items = {
+    {"continue", __togglepausemenu},
+    {"reset cart", __resetcart}
+}
+
+__f08_menu_selected = 1
+
+function __f08_menu_update()
+    if btnp(3) and __f08_menu_selected < #__f08_menu_items then
+        __f08_menu_selected = __f08_menu_selected + 1
+    end
+
+    if btnp(2) and __f08_menu_selected > 1 then
+        __f08_menu_selected = __f08_menu_selected - 1
+    end
+
+    if btnp(4) or btnp(5) then
+        toexec = __f08_menu_items[__f08_menu_selected]
+        if toexec and toexec[2] then
+            toexec[2]()
+        end
+    end
+
+end
+
+function __f08_menu_draw()
+    local menuwidth = 82
+    local menuheight = 8 + (#__f08_menu_items)*10
+    local menux = (128 - menuwidth) / 2
+    local menuy = (128 - menuheight) / 2
+
+    rectfill(menux, menuy, menux + menuwidth, menuy + menuheight, 0)
+    rect(menux+1, menuy+1, menux + menuwidth-1, menuy + menuheight-1, 7)
+    local itemx = menux + 8
+    local itemy = menuy + 8
+    local itemidx = 1
+
+    for item in all(__f08_menu_items) do
+        print(item[1], itemx, itemy, 7)
+
+        if itemidx == __f08_menu_selected then
+            line(itemx-8, itemy, itemx-8, itemy+5, 7)
+            line(itemx-7, itemy+1, itemx-7, itemy+4, 7)
+            pset(itemx-6, itemy+3, 7)
+        end
+
+        itemy = itemy + 10
+        itemidx = itemidx + 1
+    end
+end
+
+
+
 --https://twitter.com/lexaloffle/status/1314463271324315649
 --save host cpu by making most of the api local
 local
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 9e36b71..31986e4 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -921,3 +921,15 @@ int loadbioscart(lua_State *L) {

     return 0;
 }
+
+int togglepausemenu(lua_State *L) {
+    _vmForLuaApi->togglePauseMenu();
+
+    return 0;
+}
+
+int resetcart(lua_State *L) {
+    _vmForLuaApi->QueueCartChange(_vmForLuaApi->CurrentCartFilename());
+
+    return 0;
+}
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index a6702d7..3d8e8fc 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -95,6 +95,8 @@ int listcarts(lua_State *L);
 int loadcart(lua_State *L);
 int getbioserror(lua_State *L);
 int loadbioscart(lua_State *L);
+int togglepausemenu(lua_State *L);
+int resetcart(lua_State *L);

 //system functions

diff --git a/source/vm.cpp b/source/vm.cpp
index 739143b..e1ca79d 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -146,18 +146,6 @@ bool Vm::loadCart(Cart* cart) {
     luaL_openlibs(_luaState);
     lua_pushglobaltable(_luaState);

-    //load in global lua fuctions for pico 8
-    auto convertedGlobalLuaFunctions = convert_emojis(p8GlobalLuaFunctions);
-    int loadedGlobals = luaL_dostring(_luaState, convertedGlobalLuaFunctions.c_str());
-
-    if (loadedGlobals != LUA_OK) {
-        _cartLoadError = "ERROR loading pico 8 lua globals";
-        Logger::Write("ERROR loading pico 8 lua globals\n");
-        Logger::Write("Error: %s\n", lua_tostring(_luaState, -1));
-        lua_pop(_luaState, 1);
-
-        return false;
-    }

     //graphics
     lua_register(_luaState, "cls", cls);
@@ -237,6 +225,21 @@ bool Vm::loadCart(Cart* cart) {
     lua_register(_luaState, "__loadcart", loadcart);
     lua_register(_luaState, "__getbioserror", getbioserror);
     lua_register(_luaState, "__loadbioscart", loadbioscart);
+    lua_register(_luaState, "__togglepausemenu", togglepausemenu);
+    lua_register(_luaState, "__resetcart", resetcart);
+
+     //load in global lua fuctions for pico 8
+    auto convertedGlobalLuaFunctions = convert_emojis(p8GlobalLuaFunctions);
+    int loadedGlobals = luaL_dostring(_luaState, convertedGlobalLuaFunctions.c_str());
+
+    if (loadedGlobals != LUA_OK) {
+        _cartLoadError = "ERROR loading pico 8 lua globals";
+        Logger::Write("ERROR loading pico 8 lua globals\n");
+        Logger::Write("Error: %s\n", lua_tostring(_luaState, -1));
+        lua_pop(_luaState, 1);
+
+        return false;
+    }


     int loadedCart = luaL_loadstring(_luaState, cart->LuaString.c_str());
@@ -330,6 +333,10 @@ void Vm::LoadCart(std::string filename){
     }
 }

+void Vm::togglePauseMenu(){
+    _pauseMenu = !_pauseMenu;
+}
+

 void Vm::UpdateAndDraw() {
     update_buttons();
@@ -338,35 +345,70 @@ void Vm::UpdateAndDraw() {

     //todo: pause menu here, but for now just load bios
     if (_input->btnp(6)) {
-        QueueCartChange("__FAKE08-BIOS.p8");
+        togglePauseMenu();
     }

     if (_cartChangeQueued) {
         LoadCart(_nextCartKey);
     }

-    if (_hasUpdate){
-        // Push the _update function on the top of the lua stack
-        if (_targetFps == 60) {
-            lua_getglobal(_luaState, "_update60");
-        } else {
-            lua_getglobal(_luaState, "_update");
+    if (_pauseMenu){
+        //3 item menu (default) 82x34
+        //23, 47, 23 + 82, 47 + 34
+        //pause menu handled in lua firmware now
+        /*
+        uint8_t preColor = _memory->drawState.color;
+        uint8_t fillp0 = _memory->drawState.fillPattern[0];
+        uint8_t fillp1 = _memory->drawState.fillPattern[1];
+        _memory->drawState.fillPattern[0] = 0;
+        _memory->drawState.fillPattern[1] = 0;
+        _graphics->rectfill(23, 47, 23 + 82, 47 + 34, 0);
+        _graphics->rect(24, 48, 24 + 80, 48 + 32, 7);
+        _graphics->print("paused", 30, 55, 7);
+
+        if (_input->btnp(4)) {
+            togglePauseMenu();
+            QueueCartChange("__FAKE08-BIOS.p8");
         }

-        //we already checked that its a function, so we should be able to call it
+        _memory->drawState.fillPattern[0] = fillp0;
+        _memory->drawState.fillPattern[1] = fillp1;
+        _memory->drawState.color = preColor;
+        */
+
+        //todo: fix crash here
+        lua_getglobal(_luaState, "__f08_menu_update");
         lua_call(_luaState, 0, 0);
+        lua_pop(_luaState, 0);

-        //pop the update fuction off the stack now that we're done with it
+        lua_getglobal(_luaState, "__f08_menu_draw");
+        lua_call(_luaState, 0, 0);
         lua_pop(_luaState, 0);
     }
+    else{
+        if (_hasUpdate){
+            // Push the _update function on the top of the lua stack
+            if (_targetFps == 60) {
+                lua_getglobal(_luaState, "_update60");
+            } else {
+                lua_getglobal(_luaState, "_update");
+            }

-    if (_hasDraw) {
-        lua_getglobal(_luaState, "_draw");
+            //we already checked that its a function, so we should be able to call it
+            lua_call(_luaState, 0, 0);

-        lua_call(_luaState, 0, 0);
-
-        //pop the update fuction off the stack now that we're done with it
-        lua_pop(_luaState, 0);
+            //pop the update fuction off the stack now that we're done with it
+            lua_pop(_luaState, 0);
+        }
+
+        if (_hasDraw) {
+            lua_getglobal(_luaState, "_draw");
+
+            lua_call(_luaState, 0, 0);
+
+            //pop the update fuction off the stack now that we're done with it
+            lua_pop(_luaState, 0);
+        }
     }

 }
@@ -411,12 +453,21 @@ void Vm::CloseCart() {
 void Vm::QueueCartChange(std::string filename){
     _nextCartKey = filename;
     _cartChangeQueued = true;
+    _pauseMenu = false;
 }

 int Vm::GetTargetFps() {
     return _targetFps;
 }

+std::string Vm::CurrentCartFilename(){
+    if (_loadedCart){
+        return _loadedCart->Filename;
+    }
+
+    return "";
+}
+
 int Vm::GetFrameCount() {
     return _picoFrameCount;
 }
diff --git a/source/vm.h b/source/vm.h
index 5346836..9c96dee 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -38,6 +38,7 @@ class Vm {
     bool _hasDraw;

     bool _cartChangeQueued;
+    bool _pauseMenu;
     string _nextCartKey;

     string _cartLoadError;
@@ -48,6 +49,7 @@ class Vm {

     bool loadCart(Cart* cart);
     void vm_reload(int destaddr, int sourceaddr, int len, Cart* cart);
+

     public:
     Vm(
@@ -88,6 +90,10 @@ class Vm {

     PicoRam* getPicoRam();

+    string CurrentCartFilename();
+
+    void togglePauseMenu();
+
     uint8_t vm_peek(int addr);
     int16_t vm_peek2(int addr);
     int32_t vm_peek4(int addr); //note: this should return a 32 bit fixed point number