foxygit / fake-08-xbox360 Log in
commit 3cb3ae6305ccae3572fff7c60bdbabeaba90377b
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sat Apr 18 14:05:53 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sat Apr 18 14:05:53 2020 -0600

    Basics  of graphics working from p8 cart
---
 source/console.cpp       |  39 +++++++++++++++
 source/console.h         |   2 +
 source/graphics.cpp      |   2 +-
 source/main.cpp          |   6 ++-
 source/picoglobalapi.cpp |  28 ++++++-----
 source/picoglobalapi.h   |   1 -
 source/picoluaapi.cpp    | 126 +++++++++++++++++++++++++++++++++++++++++++++++
 source/picoluaapi.h      |  27 ++++++++++
 8 files changed, 214 insertions(+), 17 deletions(-)

diff --git a/source/console.cpp b/source/console.cpp
index e79ba53..d5aeca5 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -5,6 +5,7 @@
 #include "fontdata.h"
 #include "cart.h"
 #include "picoglobalapi.h"
+#include "picoluaapi.h"
 #include "logger.h"

 extern "C" {
@@ -25,6 +26,7 @@ Console::Console(){

     //this can probably go away when I'm loading actual carts and just have to expose api to lua
     Logger::Write("Initializing global api\n");
+    initPicoApi(_graphics);
     initGlobalApi(_graphics);
 }

@@ -42,9 +44,45 @@ void Console::LoadCart(std::string filename){
     // load Lua base libraries (print / math / etc)
     luaL_openlibs(_luaState);

+    lua_register(_luaState, "cls", cls);
+    lua_register(_luaState, "pset", pset);
+    lua_register(_luaState, "pget", pget);
+    lua_register(_luaState, "color", color);
+    lua_register(_luaState, "line", line);
+    lua_register(_luaState, "circ", circ);
+    lua_register(_luaState, "circfill", circfill);
+    lua_register(_luaState, "rect", rect);
+    lua_register(_luaState, "rectfill", rectfill);
+    lua_register(_luaState, "print", print);
+    lua_register(_luaState, "spr", spr);
+
     luaL_dostring(_luaState, _loadedCart->LuaString.c_str());
 }

+void Console::UpdateAndDraw(int frameCount){
+    // Push the fib function on the top of the lua stack
+    lua_getglobal(_luaState, "_update");
+
+    // Push any arguments (the number 13 in this case) on the stack
+    //lua_pushnumber(_luaState, 13);
+
+    // call the function with 0 argument, returning 0 resulta.  Note that the function actually
+    // returns 2 results -- we just don't want them
+    lua_call(_luaState, 0, 0);
+
+    // Get the result from the lua stack
+    //int result = (int)lua_tointeger(_luaState, -1);
+
+    // Clean up.  If we don't do this last step, we'll leak stack memory.
+    lua_pop(_luaState, 0);
+
+
+    lua_getglobal(_luaState, "_draw");
+    lua_call(_luaState, 0, 0);
+    lua_pop(_luaState, 0);
+
+}
+
 void Console::FlipBuffer(uint8_t* fb){
     _graphics->flipBuffer(fb);
 }
@@ -52,3 +90,4 @@ void Console::FlipBuffer(uint8_t* fb){
 void Console::TurnOff() {
     lua_close(_luaState);
 }
+
diff --git a/source/console.h b/source/console.h
index b82f70c..4f810a7 100644
--- a/source/console.h
+++ b/source/console.h
@@ -18,6 +18,8 @@ class Console {

     void LoadCart(std::string filename);

+    void UpdateAndDraw(int frameCount);
+
     void FlipBuffer(uint8_t* fb);

     void TurnOff();
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 6373ebe..bf7c86f 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -249,7 +249,7 @@ void Graphics::rectfill(short x1, short y1, short x2, short y2, uint8_t col) {
 	}
 }

-//tac08
+//based on tac08 impl
 short Graphics::print(std::string str, short x, short y, uint16_t c) {
 	_gfxState_text_x = x;

diff --git a/source/main.cpp b/source/main.cpp
index 78c5135..eb3b9fe 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -59,7 +59,7 @@ int main(int argc, char* argv[])
 		gfxFlushBuffers();
 		gfxSwapBuffers();

-		_update();
+		//_update();

 		uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
 		//clear whole top framebuffer
@@ -70,7 +70,9 @@ int main(int argc, char* argv[])
 		//memset(fb_b, bgcolor, 240*320*3);

 		//cart draw
-		_draw();
+		//_draw();
+
+		console.UpdateAndDraw(frames);

 		//send pico 8 screen to framebuffer
 		console.FlipBuffer(fb);
diff --git a/source/picoglobalapi.cpp b/source/picoglobalapi.cpp
index b232069..cf9f722 100644
--- a/source/picoglobalapi.cpp
+++ b/source/picoglobalapi.cpp
@@ -1,44 +1,46 @@

 #include <string>

+#include "picoglobalapi.h"
 #include "graphics.h"

-Graphics* _graphicsForApi;
+
+Graphics* _graphicsForGlobalCApi;

 void initGlobalApi(Graphics* graphics){
-    _graphicsForApi = graphics;
+    _graphicsForGlobalCApi = graphics;
 }

 void cls(){
-    _graphicsForApi->cls();
+    _graphicsForGlobalCApi->cls();
 }
 void pset(short x, short y, uint8_t col){
-    _graphicsForApi->pset(x, y, col);
+    _graphicsForGlobalCApi->pset(x, y, col);
 }
 uint8_t pget(short x, short y){
-    return _graphicsForApi->pget(x, y);
+    return _graphicsForGlobalCApi->pget(x, y);
 }
 void color(uint8_t c){
-    _graphicsForApi->color(c);
+    _graphicsForGlobalCApi->color(c);
 }
 void line (short x1, short y1, short x2, short y2, uint8_t col){
-    _graphicsForApi->line(x1, y1, x2, y2, col);
+    _graphicsForGlobalCApi->line(x1, y1, x2, y2, col);
 }
 void circ(short ox, short oy, short r, uint8_t col){
-    _graphicsForApi->circ(ox, oy, r, col);
+    _graphicsForGlobalCApi->circ(ox, oy, r, col);
 }
 void circfill(short ox, short oy, short r, uint8_t col){
-    _graphicsForApi->circfill(ox, oy, r, col);
+    _graphicsForGlobalCApi->circfill(ox, oy, r, col);
 }
 void rect(short x1, short y1, short x2, short y2, uint8_t col){
-    _graphicsForApi->rect(x1, y1, x2, y2, col);
+    _graphicsForGlobalCApi->rect(x1, y1, x2, y2, col);
 }
 void rectfill(short x1, short y1, short x2, short y2, uint8_t col){
-    _graphicsForApi->rectfill(x1, y1, x2, y2, col);
+    _graphicsForGlobalCApi->rectfill(x1, y1, x2, y2, col);
 }
 short print(std::string str, short x, short y, uint16_t c){
-    return _graphicsForApi->print(str, x, y, c);
+    return _graphicsForGlobalCApi->print(str, x, y, c);
 }
 void spr(int n, int x, int y, int w, int h, bool flip_x, bool flip_y) {
-    _graphicsForApi->spr(n, x, y, w, h, flip_x, flip_y);
+    _graphicsForGlobalCApi->spr(n, x, y, w, h, flip_x, flip_y);
 }
diff --git a/source/picoglobalapi.h b/source/picoglobalapi.h
index 1a192db..e9282c1 100644
--- a/source/picoglobalapi.h
+++ b/source/picoglobalapi.h
@@ -18,4 +18,3 @@ void rect(short x1, short y1, short x2, short y2, uint8_t col);
 void rectfill(short x1, short y1, short x2, short y2, uint8_t col);
 short print(std::string str, short x, short y, uint16_t c);
 void spr(int n, int x, int y, int w, int h, bool flip_x, bool flip_y);
-
diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
new file mode 100644
index 0000000..23d31fa
--- /dev/null
+++ b/source/picoluaapi.cpp
@@ -0,0 +1,126 @@
+
+#include <string>
+
+#include "picoluaapi.h"
+#include "graphics.h"
+
+extern "C" {
+  #include <lua.h>
+  #include <lualib.h>
+  #include <lauxlib.h>
+}
+
+Graphics* _graphicsForLuaApi;
+
+void initPicoApi(Graphics* graphics){
+    _graphicsForLuaApi = graphics;
+}
+
+/*functions to expose to lua*/
+int cls(lua_State *L){
+    _graphicsForLuaApi->cls();
+
+    return 0;
+}
+int pset(lua_State *L){
+    double x = lua_tonumber(L,1);
+    double y = lua_tonumber(L,2);
+    double c = lua_tonumber(L,3);
+
+    _graphicsForLuaApi->pset((short)x, (short)y, (uint8_t)c);
+
+    return 0;
+}
+int pget(lua_State *L){
+    double x = lua_tonumber(L,1);
+    double y = lua_tonumber(L,2);
+
+    uint8_t color = _graphicsForLuaApi->pget((short)x, (short)y);
+
+    lua_pushinteger(L, color);
+
+    return 1;
+}
+int color(lua_State *L){
+    double c = lua_tonumber(L,1);
+
+    _graphicsForLuaApi->color((uint8_t)c);
+
+    return 0;
+}
+int line (lua_State *L){
+    double x1 = lua_tonumber(L,1);
+    double y1 = lua_tonumber(L,2);
+    double x2 = lua_tonumber(L,3);
+    double y2 = lua_tonumber(L,4);
+    double c = lua_tonumber(L,5);
+
+    _graphicsForLuaApi->line((short)x1, (short)y1, (short)x2, (short)y2, (uint8_t)c);
+
+    return 0;
+}
+int circ(lua_State *L){
+    double ox = lua_tonumber(L,1);
+    double oy = lua_tonumber(L,2);
+    double r = lua_tonumber(L,3);
+    double c = lua_tonumber(L,4);
+
+    _graphicsForLuaApi->circ((short)ox, (short)oy, (short)r, (uint8_t)c);
+
+    return 0;
+}
+int circfill(lua_State *L){
+    double ox = lua_tonumber(L,1);
+    double oy = lua_tonumber(L,2);
+    double r = lua_tonumber(L,3);
+    double c = lua_tonumber(L,4);
+
+    _graphicsForLuaApi->circfill((short)ox, (short)oy, (short)r, (uint8_t)c);
+
+    return 0;
+}
+int rect(lua_State *L){
+    double x1 = lua_tonumber(L,1);
+    double y1 = lua_tonumber(L,2);
+    double x2 = lua_tonumber(L,3);
+    double y2 = lua_tonumber(L,4);
+    double c = lua_tonumber(L,5);
+
+    _graphicsForLuaApi->rect((short)x1, (short)y1, (short)x2, (short)y2, (uint8_t)c);
+
+    return 0;
+}
+int rectfill(lua_State *L){
+    double x1 = lua_tonumber(L,1);
+    double y1 = lua_tonumber(L,2);
+    double x2 = lua_tonumber(L,3);
+    double y2 = lua_tonumber(L,4);
+    double c = lua_tonumber(L,5);
+
+    _graphicsForLuaApi->rectfill((short)x1, (short)y1, (short)x2, (short)y2, (uint8_t)c);
+
+    return 0;
+}
+int print(lua_State *L){
+    const char * str = lua_tostring(L, 1);
+    double x = lua_tonumber(L,2);
+    double y = lua_tonumber(L,3);
+    double c = lua_tonumber(L,3);
+
+    _graphicsForLuaApi->print(str, x, y, c);
+
+    return 0;
+}
+int spr(lua_State *L) {
+    double n = lua_tonumber(L,1);
+    double x = lua_tonumber(L,2);
+    double y = lua_tonumber(L,3);
+    double w = lua_tonumber(L,4);
+    double h = lua_tonumber(L,5);
+    bool flip_x = lua_toboolean(L,6);
+    bool flip_y = lua_toboolean(L,7);
+
+    _graphicsForLuaApi->spr((short)n, (short)x, (short)y, (short)w, (short)h, flip_x, flip_y);
+
+    return 0;
+}
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
new file mode 100644
index 0000000..e605893
--- /dev/null
+++ b/source/picoluaapi.h
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <string>
+
+extern "C" {
+  #include <lua.h>
+  #include <lualib.h>
+  #include <lauxlib.h>
+}
+
+#include "graphics.h"
+
+//this can probably go away when I'm loading actual carts and just have to expose api to lua
+void initPicoApi(Graphics* graphics);
+
+int cls(lua_State *L);
+int pset(lua_State *L);
+int pget(lua_State *L);
+int color(lua_State *L);
+int line (lua_State *L);
+int circ(lua_State *L);
+int circfill(lua_State *L);
+int rect(lua_State *L);
+int rectfill(lua_State *L);
+int print(lua_State *L);
+int spr(lua_State *L);
+