foxygit / fake-08-xbox360 Log in
commit 51354ead105a4ba13cc4724d6d00088e13622570
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Tue May 26 17:04:09 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Tue May 26 17:04:09 2020 -0600

    Load bios cart and start prep for cart swapping
---
 platform/3ds/source/3dsHost.cpp |  2 +-
 source/main.cpp                 |  8 ++---
 source/picoluaapi.cpp           | 18 +++++++++++
 source/picoluaapi.h             |  7 +++++
 source/vm.cpp                   | 67 ++++++++++++++++++++++++++++++++---------
 source/vm.h                     |  6 +++-
 6 files changed, 87 insertions(+), 21 deletions(-)

diff --git a/platform/3ds/source/3dsHost.cpp b/platform/3ds/source/3dsHost.cpp
index b4222f4..ecd3717 100644
--- a/platform/3ds/source/3dsHost.cpp
+++ b/platform/3ds/source/3dsHost.cpp
@@ -26,7 +26,7 @@ const int PicoScreenWidth = 128;
 const int PicoScreenHeight = 128;


-StretchOption stretch;
+StretchOption stretch = StretchAndOverflow;
 u64 last_time;
 u64 now_time;
 u64 frame_time;
diff --git a/source/main.cpp b/source/main.cpp
index 6154751..a13f69f 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -22,9 +22,9 @@ int main(int argc, char* argv[])
 	Vm *vm = new Vm();
 	Logger::Write("initialized Vm and host\n");

-	Logger::Write("Loading cart\n");
-	vm->LoadCart("cart.p8");
-	Logger::Write("Cart Loaded\n");
+	Logger::Write("Loading Bios cart\n");
+	vm->LoadBiosCart();
+	Logger::Write("Bios Cart Loaded\n");

 	// Main loop
 	Logger::Write("Starting main loop\n");
@@ -63,7 +63,7 @@ int main(int argc, char* argv[])


 	Logger::Write("Turning off vm and exiting logger\n");
-	vm->TurnOff();
+	vm->CloseCart();
 	delete vm;

 	Logger::Exit();
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 88841ed..d818a4b 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -621,3 +621,21 @@ int dget(lua_State *L) {
 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);
+    }
+
+    return 0;
+}
+
+int loadbioscart(lua_State *L) {
+    _vmForLuaApi->LoadBiosCart();
+
+    return 0;
+}
+*/
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index f0c5f29..652c60c 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -80,6 +80,13 @@ int cartdata(lua_State *L);
 int dget(lua_State *L);
 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

 //api.tonum(val)
diff --git a/source/vm.cpp b/source/vm.cpp
index 5538804..d92d34d 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -42,18 +42,14 @@ Vm::Vm(){
 }

 Vm::~Vm(){
+    CloseCart();
+
     delete _graphics;
     delete _input;
     delete _audio;
-
-    if (_loadedCart){
-        delete _loadedCart;
-    }
 }

-void Vm::LoadCart(std::string filename){
-    Logger::Write("Calling Cart Constructor\n");
-    Cart *cart = new Cart(filename);
+bool Vm::loadCart(Cart* cart) {
     _picoFrameCount = 0;

     _graphics->setSpriteSheet(cart->SpriteSheetString);
@@ -62,9 +58,6 @@ void Vm::LoadCart(std::string filename){

     _audio->setSfx(cart->SfxString);
     _audio->setMusic(cart->MusicString);
-
-    _loadedCart = cart;
-

     // initialize Lua interpreter
     _luaState = luaL_newstate();
@@ -80,7 +73,7 @@ void Vm::LoadCart(std::string filename){
         Logger::Write("Error: %s\n", lua_tostring(_luaState, -1));
         lua_pop(_luaState, 1);

-        return;
+        return false;
     }

     //graphics
@@ -94,6 +87,7 @@ void Vm::LoadCart(std::string filename){
     lua_register(_luaState, "rect", rect);
     lua_register(_luaState, "rectfill", rectfill);
     lua_register(_luaState, "print", print);
+    lua_register(_luaState, "cursor", cursor);
     lua_register(_luaState, "spr", spr);
     lua_register(_luaState, "sspr", sspr);
     lua_register(_luaState, "fget", fget);
@@ -138,14 +132,20 @@ void Vm::LoadCart(std::string filename){
     lua_register(_luaState, "dget", dget);
     lua_register(_luaState, "dset", dset);

-    int loadedCart = luaL_dostring(_luaState, _loadedCart->LuaString.c_str());
+    //file system
+    /*
+    lua_register(_luaState, "__loadcart", loadcart);
+    lua_register(_luaState, "__loadbioscart", loadbioscart);
+    */
+
+    int loadedCart = luaL_dostring(_luaState, cart->LuaString.c_str());

     if (loadedCart != LUA_OK) {
         Logger::Write("ERROR loading cart\n");
         Logger::Write("Error: %s\n", lua_tostring(_luaState, -1));
         lua_pop(_luaState, 1);

-        return;
+        return false;
     }


@@ -180,6 +180,35 @@ void Vm::LoadCart(std::string filename){
         _hasDraw = true;
     }
     lua_pop(_luaState, 0);
+
+    _loadedCart = cart;
+
+    return true;
+}
+
+void Vm::LoadBiosCart(){
+    CloseCart();
+
+    Cart *cart = new Cart("fake08-nocart.p8");
+
+    bool success = loadCart(cart);
+
+    if (!success) {
+        CloseCart();
+    }
+}
+
+void Vm::LoadCart(std::string filename){
+    CloseCart();
+
+    Logger::Write("Calling Cart Constructor\n");
+    Cart *cart = new Cart(filename);
+
+    bool success = loadCart(cart);
+
+    if (!success) {
+        CloseCart();
+    }
 }


@@ -234,8 +263,16 @@ void Vm::FillAudioBuffer(void *audioBuffer, size_t offset, size_t size){
    _audio->FillAudioBuffer(audioBuffer, offset, size);
 }

-void Vm::TurnOff() {
-    lua_close(_luaState);
+void Vm::CloseCart() {
+    if (_loadedCart){
+        delete _loadedCart;
+        _loadedCart = nullptr;
+    }
+
+    if (_luaState) {
+        lua_close(_luaState);
+        _luaState = nullptr;
+    }
 }

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

+    bool loadCart(Cart* cart);
+
     public:
     Vm();
     ~Vm();

+    void LoadBiosCart();
+
     void LoadCart(std::string filename);

     void UpdateAndDraw(
@@ -39,7 +43,7 @@ class Vm {

     void FillAudioBuffer(void *audioBuffer, size_t offset, size_t size);

-    void TurnOff();
+    void CloseCart();

     uint8_t GetTargetFps();