foxygit / fake-08-xbox360 Log in
commit 9f0edeb98bb8feac496e3c626176ab168514cafb
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sat Apr 25 16:29:32 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sat Apr 25 16:29:32 2020 -0600

    Add table helpers to lua new lua file that gets loaded before running
---
 source/console.cpp            |  4 +++
 source/p8GlobalLuaFunctions.h | 58 +++++++++++++++++++++++++++++++++++++++++++
 testcart.p8                   | 34 ++++++++++++++++++-------
 3 files changed, 87 insertions(+), 9 deletions(-)

diff --git a/source/console.cpp b/source/console.cpp
index 56514e4..d9dcfaf 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -8,6 +8,7 @@
 #include "picoluaapi.h"
 #include "logger.h"
 #include "Input.h"
+#include "p8GlobalLuaFunctions.h"

 extern "C" {
   #include <lua.h>
@@ -62,6 +63,9 @@ void Console::LoadCart(std::string filename){
     // load Lua base libraries (print / math / etc)
     luaL_openlibs(_luaState);

+    //load in global lua fuctions for pico 8
+    luaL_dostring(_luaState, p8GlobalLuaFunctions);
+
     //graphics
     lua_register(_luaState, "cls", cls);
     lua_register(_luaState, "pset", pset);
diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
new file mode 100644
index 0000000..2058cdb
--- /dev/null
+++ b/source/p8GlobalLuaFunctions.h
@@ -0,0 +1,58 @@
+#pragma once
+
+#include <string>
+
+
+//from PicoLove api.lua
+
+const char * p8GlobalLuaFunctions = R"#(
+-- The functions below are normally attached to the program code, but are here for simplicity
+function all(a)
+	if a==nil or #a==0 then
+		return function() end
+	end
+	local i, li=1
+	return function()
+		if (a[i]==li) then i=i+1 end
+		while(a[i]==nil and i<=#a) do i=i+1 end
+		li=a[i]
+		return a[i]
+	end
+end
+
+function foreach(a, f)
+	for v in all(a) do
+		f(v)
+	end
+end
+
+function count(a)
+	local count=0
+	for i=1, #a do
+		if a[i]~=nil then count=count+1 end
+	end
+	return count
+end
+
+function add(a, v)
+	if a==nil then return end
+	a[#a+1]=v
+end
+
+function del(a, dv)
+	if a==nil then return end
+	for i=1, #a do
+		if a[i]==dv then
+			tremove(a, i)
+			return
+		end
+	end
+end
+
+yield = coroutine.yield
+cocreate = coroutine.create
+coresume = coroutine.resume
+costatus = coroutine.status
+)#";
+
+
diff --git a/testcart.p8 b/testcart.p8
index e54df47..118c7c1 100644
--- a/testcart.p8
+++ b/testcart.p8
@@ -74,18 +74,34 @@ function _draw()

  print(sget(59,5), 64, 88, 7)
  print(sget(60,6), 64, 96, 7)
+ print(time(), 64, 104)

+ pal(11, 10)
+ palt(8, 1)
  spr(sprn, sprx, spry, 1, 1, false, false)
+ palt()

  sspr(64, 0, 8, 8, 72, 48, 12, 20, false, false)
+ pal()

  clip(0, 8, 128, 112)
  circfill(10, 118, 8, 8)
  clip()
  circfill(40, 118, 8, 7)
+
+ spr(9, 64, 120)
+ spr(9, 74, 120, 1, 1, true, false)
+ spr(9, 84, 120, 1, 1, true, true)
+ spr(9, 94, 120, 1, 1, false, true)
+
+ pal(6, 14, 1)
+
+ t = {1, 3, 5}
+ add(t, 7)
+ foreach(t, print)
 end

-function _update()
+function _update60()
     if btn(0) then sprx = sprx - 2 end
 	if btn(1) then sprx = sprx + 2 end
 	if btn(2) then spry = spry - 2 end
@@ -121,14 +137,14 @@ function _update()
     r1y1 = r1dy + r1y1
 end
 __gfx__
-f0000000000000000000000007700770000770000000000000000000000660005000000500000000000000000000000000000000000000000000000000000000
-00000000000000067000000078877887077777700500000000000050800660085005500500000000000000000000000000000000000000000000000000000000
-0070070000000006600000007888888707bbbb700500000000000050600550065055550500000000000000000000000000000000000000000000000000000000
-000770000000000660000000078888707bbbbbb70500000000000050600550065550055500000000000000000000000000000000000000000000000000000000
-000770000000000660000000078888707bbbbbb70500000550000050600660065555555500000000000000000000000000000000000000000000000000000000
-0070070000000066670000000078870007bbbb700500005555000050666cc66650b55b0500000000000000000000000000000000000000000000000000000000
-00000000008000666600080000077000077777700550055555500050666cc6665006600500000000000000000000000000000000000000000000000000000000
-0000000080d00065d6000d0800000000000770000555555555555550000660005000000500000000000000000000000000000000000000000000000000000000
+f0000000000000000000000007700770000770000000000000000000000660005000000544911999000000000000000000000000000000000000000000000000
+00000000000000067000000078877887077777700500000000000050800660085005500544e11eee000000000000000000000000000000000000000000000000
+0070070000000006600000007888888707bbbb700500000000000050600550065055550544000000000000000000000000000000000000000000000000000000
+000770000000000660000000078888707bbbbbb705000000000000506005500655500555885552bb000000000000000000000000000000000000000000000000
+000770000000000660000000078888707bbbbbb705000005500000506006600655555555885552bb000000000000000000000000000000000000000000000000
+0070070000000066670000000078870007bbbb700500005555000050666cc66650b55b0544000000000000000000000000000000000000000000000000000000
+00000000008000666600080000077000077777700550055555500050666cc6665006600544fddfff000000000000000000000000000000000000000000000000
+0000000080d00065d6000d08000000000007700005555555555555500006600050000005443dd333000000000000000000000000000000000000000000000000
 0b00080060d0006556000d0600000000000000000550055665500550000000000000000000000000000000000000000000000000000000000000000000000000
 0b00080060ddd065560ddd0600000000000000000500005555000050000000000000000000000000000000000000000000000000000000000000000000000000
 0b000800600ddd6556ddd00600000000000000000500008558000050000000000000000000000000000000000000000000000000000000000000000000000000