foxygit / fake-08-xbox360 Log in
commit 19c1d00ebd8e339e05f1b696a9e7246169b0715a
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Tue May 26 21:38:57 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Tue May 26 21:38:57 2020 -0600

    Basics of cart changing working
---
 fake08-nocart.p8      | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++
 source/graphics.cpp   |  2 +-
 source/picoluaapi.cpp |  6 ++--
 source/picoluaapi.h   |  2 --
 source/vm.cpp         | 34 +++++++++++++++++--
 source/vm.h           |  5 +++
 6 files changed, 132 insertions(+), 10 deletions(-)

diff --git a/fake08-nocart.p8 b/fake08-nocart.p8
new file mode 100644
index 0000000..e91c294
--- /dev/null
+++ b/fake08-nocart.p8
@@ -0,0 +1,93 @@
+pico-8 cartridge // http://www.pico-8.com
+version 27
+__lua__
+--carts hardcoded for now
+local carts={"cart.p8", "lilking.p8", "celeste.p8"}
+local numcarts = 3
+
+local cidx=0
+local carttoload = ""
+local t=0
+local linebuffer=""
+local line
+local bgcolor=5
+local runcmd=false
+
+
+function _init()
+	cls(bgcolor)
+	spr(0, 1, 5, 6, 1)
+	color(6)
+	cursor(0, 6*3)
+	print("welcome to fake-08")
+	print("a homebrew pico-8 emulator")
+	print("currently in pre-alpha")
+	print("")
+	print("place p8 carts in sdmc:/p8carts/")
+	print("⬅️➡️ to navigate carts")
+	print("🅾️ (a) to load selected cart")
+	print("start to close current cart")
+	print("r to cycle screen sizes")
+	print("l + r to exit")
+	print("")
+	-- 18 pixels from cursor() call, then
+	-- 24 more from 4 print calls
+	line=84
+end
+
+function _update60()
+	t+=1
+
+	if btnp(1) then
+		cidx = min((cidx + 1), numcarts)
+	end
+	if btnp(0) then
+		cidx = max((cidx - 1), 0)
+	end
+
+	if btnp(2) then
+		--ls()
+		linebuffer = "ls"
+	end
+	if btnp(4) then
+		linebuffer = ""
+	end
+	if btnp(5) then
+		linebuffer = ""
+		runcmd = true
+	end
+
+	if cidx > 0 and cidx <= numcarts then
+		carttoload = carts[cidx]
+		linebuffer = "load " .. carttoload
+	else
+		carttoload = ""
+	end
+
+	if runcmd then
+		--make call to global
+		--load cart here
+
+		if __loadcart and carttoload then
+			__loadcart(carttoload)
+		end
+	end
+
+end
+
+function _draw()
+	rectfill(0, line, 128, line+5, bgcolor)
+	color(7)
+	print("> "..linebuffer, 0, line)
+	if t%30<15 then
+		rectfill((#linebuffer+2)*4, line, (#linebuffer+2)*4+3, line+4, 8)
+	end
+end
+
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+77777077777077007077777000000007777007777000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+77000077077077077077000000000077077077077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+77777077777077770077770077777077077077777000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+77000077077077077077000000000077077077077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+77000077077077007077777000000077770077770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/source/graphics.cpp b/source/graphics.cpp
index f8ad025..9630317 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -57,7 +57,7 @@ void Graphics::setSpriteFlags(std::string spriteFlagsstring){
 }

 void Graphics::setMapData(std::string mapDataString){
-	Logger::Write("Copying data to sprite flags\n");
+	Logger::Write("Copying data to map data\n");
 	copy_string_to_memory(mapData, mapDataString);
 }

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index d818a4b..f2477f9 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -622,20 +622,18 @@ int dset(lua_State *L) {
     return noop("dset");
 }

-/*
 int loadcart(lua_State *L) {
     if (lua_isstring(L, 1)){
         const char * str = "";
         str = lua_tolstring(L, 1, nullptr);
-        _vmForLuaApi->LoadCart(str);
+        _vmForLuaApi->QueueCartChange(str);
     }

     return 0;
 }

 int loadbioscart(lua_State *L) {
-    _vmForLuaApi->LoadBiosCart();
+    //_vmForLuaApi->LoadBiosCart();

     return 0;
 }
-*/
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 652c60c..79307be 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -82,10 +82,8 @@ int dset(lua_State *L);

 //file system/vm functions
 //int ls(lua_State *L);
-/*
 int loadcart(lua_State *L);
 int loadbioscart(lua_State *L);
-*/

 //system functions

diff --git a/source/vm.cpp b/source/vm.cpp
index d92d34d..81629b1 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -133,10 +133,8 @@ bool Vm::loadCart(Cart* cart) {
     lua_register(_luaState, "dset", dset);

     //file system
-    /*
     lua_register(_luaState, "__loadcart", loadcart);
     lua_register(_luaState, "__loadbioscart", loadbioscart);
-    */

     int loadedCart = luaL_dostring(_luaState, cart->LuaString.c_str());

@@ -199,10 +197,17 @@ void Vm::LoadBiosCart(){
 }

 void Vm::LoadCart(std::string filename){
+    Logger::Write("Loading cart %s\n", filename);
     CloseCart();

     Logger::Write("Calling Cart Constructor\n");
-    Cart *cart = new Cart(filename);
+    Cart *cart;
+    if (filename == "__FAKE08-BIOS.p8"){
+        cart = new Cart("fake08-nocart.p8");
+    }
+    else{
+        cart = new Cart(filename);
+    }

     bool success = loadCart(cart);

@@ -244,6 +249,17 @@ void Vm::UpdateAndDraw(
     }

     _picoFrameCount++;
+
+    //todo: pause menu here, but for now just load bios
+    if (kdown & P8_KEY_PAUSE) {
+        QueueCartChange("__FAKE08-BIOS.p8");
+    }
+
+    if (_cartChangeQueued) {
+        LoadCart(_nextCartKey);
+
+        _cartChangeQueued = false;
+    }
 }

 uint8_t* Vm::GetPicoInteralFb(){
@@ -264,6 +280,7 @@ void Vm::FillAudioBuffer(void *audioBuffer, size_t offset, size_t size){
 }

 void Vm::CloseCart() {
+    Logger::Write("deleting cart and closing lua state\n");
     if (_loadedCart){
         delete _loadedCart;
         _loadedCart = nullptr;
@@ -273,6 +290,17 @@ void Vm::CloseCart() {
         lua_close(_luaState);
         _luaState = nullptr;
     }
+
+    Logger::Write("resetting state\n");
+    _hasUpdate = false;
+    _hasDraw = false;
+    _targetFps = 30;
+    _picoFrameCount = 0;
+}
+
+void Vm::QueueCartChange(std::string filename){
+    _nextCartKey = filename;
+    _cartChangeQueued = true;
 }

 uint8_t Vm::GetTargetFps() {
diff --git a/source/vm.h b/source/vm.h
index 534229e..55476a0 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -23,6 +23,9 @@ class Vm {
     bool _hasUpdate;
     bool _hasDraw;

+    bool _cartChangeQueued;
+    std::string _nextCartKey;
+
     bool loadCart(Cart* cart);

     public:
@@ -45,6 +48,8 @@ class Vm {

     void CloseCart();

+    void QueueCartChange(std::string newcart);
+
     uint8_t GetTargetFps();

     int GetFrameCount();