foxygit / fake-08-xbox360 Log in
commit 1c263b49ff66ac24e93b24b6765dbc6e382bf01a
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sat Apr 18 16:38:07 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sat Apr 18 16:38:07 2020 -0600

    Get basics of input working
---
 source/Input.cpp         | 14 +++++++++
 source/Input.h           | 29 ++++++++++++++++++
 source/console.cpp       | 18 +++++++----
 source/console.h         |  4 ++-
 source/fakecart.cpp      | 80 ------------------------------------------------
 source/fakecart.h        |  4 ---
 source/main.cpp          | 54 ++++++++++++++++++++++++++++++--
 source/picoglobalapi.cpp | 46 ----------------------------
 source/picoglobalapi.h   | 20 ------------
 source/picoluaapi.cpp    | 28 ++++++++++++++++-
 source/picoluaapi.h      |  7 ++++-
 testcart.p8              | 14 ++++++++-
 12 files changed, 155 insertions(+), 163 deletions(-)

diff --git a/source/Input.cpp b/source/Input.cpp
new file mode 100644
index 0000000..700cee2
--- /dev/null
+++ b/source/Input.cpp
@@ -0,0 +1,14 @@
+#include "input.h"
+
+void Input::SetState(uint8_t kdown, uint8_t kheld){
+    _currentKDown = kdown;
+    _currentKHeld = kheld;
+}
+
+bool Input::btn(uint8_t i){
+    return BITMASK(i) & _currentKHeld;
+}
+
+bool Input::btnp(uint8_t i){
+    return BITMASK(i) & _currentKDown;
+}
diff --git a/source/Input.h b/source/Input.h
new file mode 100644
index 0000000..a40db1b
--- /dev/null
+++ b/source/Input.h
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <stdint.h>
+
+/// Creates a bitmask from a bit number.
+#define BITMASK(n) (1U<<(n))
+
+enum
+{
+	P8_KEY_LEFT   = BITMASK(0),
+	P8_KEY_RIGHT  = BITMASK(1),
+	P8_KEY_UP     = BITMASK(2),
+	P8_KEY_DOWN   = BITMASK(3),
+	P8_KEY_O      = BITMASK(4),
+	P8_KEY_X      = BITMASK(5),
+	P8_KEY_PAUSE  = BITMASK(6),
+	P8_KEY_7      = BITMASK(7),
+};
+
+class Input{
+	uint8_t _currentKDown;
+	uint8_t _currentKHeld;
+
+    public:
+    void SetState(uint8_t kdown, uint8_t kheld);
+
+    bool btn(uint8_t i);
+    bool btnp(uint8_t i);
+};
\ No newline at end of file
diff --git a/source/console.cpp b/source/console.cpp
index d5aeca5..3b01c45 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -4,9 +4,9 @@
 #include "graphics.h"
 #include "fontdata.h"
 #include "cart.h"
-#include "picoglobalapi.h"
 #include "picoluaapi.h"
 #include "logger.h"
+#include "Input.h"

 extern "C" {
   #include <lua.h>
@@ -20,14 +20,16 @@ Console::Console(){

     Logger::Write("Creating Graphics object\n");
     Graphics* graphics = new Graphics(fontdata);
-    Logger::Write("Created Graphics object\n");
-
     _graphics = graphics;

+    Logger::Write("Creating Input object\n");
+    Input* input = new Input();
+    _input = input;
+
     //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);
+    initPicoApi(_graphics, _input);
+    //initGlobalApi(_graphics);
 }

 void Console::LoadCart(std::string filename){
@@ -55,11 +57,15 @@ void Console::LoadCart(std::string filename){
     lua_register(_luaState, "rectfill", rectfill);
     lua_register(_luaState, "print", print);
     lua_register(_luaState, "spr", spr);
+    lua_register(_luaState, "btn", btn);
+    lua_register(_luaState, "btnp", btnp);

     luaL_dostring(_luaState, _loadedCart->LuaString.c_str());
 }

-void Console::UpdateAndDraw(int frameCount){
+void Console::UpdateAndDraw(int frameCount, uint8_t kdown, uint8_t kheld){
+    _input->SetState(kdown, kheld);
+
     // Push the fib function on the top of the lua stack
     lua_getglobal(_luaState, "_update");

diff --git a/source/console.h b/source/console.h
index 4f810a7..669fabc 100644
--- a/source/console.h
+++ b/source/console.h
@@ -1,6 +1,7 @@
 #pragma once

 #include "cart.h"
+#include "Input.h"

 extern "C" {
   #include <lua.h>
@@ -12,13 +13,14 @@ class Console {
     Cart* _loadedCart;
     Graphics* _graphics;
     lua_State* _luaState;
+    Input* _input;

     public:
     Console();

     void LoadCart(std::string filename);

-    void UpdateAndDraw(int frameCount);
+    void UpdateAndDraw(int frameCount, uint8_t kdown, uint8_t kheld);

     void FlipBuffer(uint8_t* fb);

diff --git a/source/fakecart.cpp b/source/fakecart.cpp
deleted file mode 100644
index ff84b2a..0000000
--- a/source/fakecart.cpp
+++ /dev/null
@@ -1,80 +0,0 @@
-
-#include "picoglobalapi.h"
-
-
-float r1x0 = 10;
-float r1y0 = 10;
-float r1x1 = 50;
-float r1y1 = 50;
-float r1c = 2;
-
-float r2x0 = 60;
-float r2y0 = 60;
-float r2x1 = 100;
-float r2y1 = 100;
-float r2c = 3;
-
-float r1dx = .5;
-float r1dy = 2;
-
-float r2dx = 2;
-float r2dy = 2;
-
-void _draw() {
-
-    cls();
-
-    rectfill(0, 0, 127, 127, 0);
-
-    print("abcdefghijklmnopqrstuvwxyz", 0, 0, 7);
-
-    rectfill(50, -1, 60, 10, 11);
-
-    rectfill((short)r1x0, (short)r1y0, (short)r1x1, (short)r1y1, r1c);
-
-    rect((short)r2x1, (short)r2y1, (short)r2x0, (short)r2y0, r2c);
-
-    pset(40, 10, 13);
-
-    line(120, 10, 120, 120, 14);
-
-    line(0, 0, 20, 20, 15);
-
-    line(0, 20, 20, 0, 15);
-
-    line(20, 30, 0, 20, 12);
-
-    line(0, 30, 20, 20, 12);
-
-    circ(90, 90, 30, 8);
-
-    circfill(100, 100, 15, 9);
-
-    print("test str", 64, 64, 7);
-
-    spr(7, 40, 40, 1, 1, false, false);
-}
-
-void _update() {
-    if (r1x0 < 0 || r1x1 > 128){
-        r1dx = -1 * r1dx;
-    }
-
-    if (r1y0 < 0 || r1y1 > 128){
-        r1dy = -1 * r1dy;
-    }
-
-    if (r2x0 < 0 || r2x1 > 128){
-        r2dx = -1 * r2dx;
-    }
-
-    if (r2y0 < 0 || r2y1 > 128){
-        r2dy = -1 * r2dy;
-    }
-
-    r1x0 = r1dx + r1x0;
-    r1x1 = r1dx + r1x1;
-    r1y0 = r1dy + r1y0;
-    r1y1 = r1dy + r1y1;
-
-}
\ No newline at end of file
diff --git a/source/fakecart.h b/source/fakecart.h
deleted file mode 100644
index cfe2acc..0000000
--- a/source/fakecart.h
+++ /dev/null
@@ -1,4 +0,0 @@
-#pragma once
-
-void _draw();
-void _update();
\ No newline at end of file
diff --git a/source/main.cpp b/source/main.cpp
index eb3b9fe..c4440fa 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -8,13 +8,51 @@
 #include <string>

 #include "graphics.h"
-#include "fakecart.h"
 #include "console.h"
 #include "logger.h"

+#include "input.h"
+
 //this has the macro _TEST defined in it
 #include "tests/test_base.h"

+uint8_t ConvertInputToP8(u32 input){
+	uint8_t result = 0;
+	if (input & KEY_LEFT){
+		result |= P8_KEY_LEFT;
+	}
+
+	if (input & KEY_RIGHT){
+		result |= P8_KEY_RIGHT;
+	}
+
+	if (input & KEY_UP){
+		result |= P8_KEY_UP;
+	}
+
+	if (input & KEY_DOWN){
+		result |= P8_KEY_DOWN;
+	}
+
+	if (input & KEY_B){
+		result |= P8_KEY_O;
+	}
+
+	if (input & KEY_A){
+		result |= P8_KEY_X;
+	}
+
+	if (input & KEY_START){
+		result |= P8_KEY_PAUSE;
+	}
+
+	if (input & KEY_SELECT){
+		result |= P8_KEY_7;
+	}
+
+	return result;
+}
+
 int main(int argc, char* argv[])
 {
 	int frames = 0;
@@ -43,6 +81,7 @@ int main(int argc, char* argv[])

 	// Main loop
 	Logger::Write("Starting main loop\n");
+
 	while (aptMainLoop())
 	{
 		gspWaitForVBlank();
@@ -52,8 +91,15 @@ int main(int argc, char* argv[])

 		//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
 		u32 kDown = hidKeysDown();
+		u32 kHeld = hidKeysHeld();
+
+		bool lpressed = kHeld & KEY_L;
+		bool rpressed = kHeld & KEY_R;

-		if (kDown & KEY_START) break; // break in order to return to hbmenu
+		if (lpressed && rpressed) break; // break in order to return to hbmenu
+
+		uint8_t p8kDown = ConvertInputToP8(kDown);
+		uint8_t p8kHeld = ConvertInputToP8(kHeld);

 		// Flush and swap framebuffers
 		gfxFlushBuffers();
@@ -72,7 +118,7 @@ int main(int argc, char* argv[])
 		//cart draw
 		//_draw();

-		console.UpdateAndDraw(frames);
+		console.UpdateAndDraw(frames, p8kDown, p8kHeld);

 		//send pico 8 screen to framebuffer
 		console.FlipBuffer(fb);
@@ -87,3 +133,5 @@ int main(int argc, char* argv[])
 	gfxExit();
 	return 0;
 }
+
+
diff --git a/source/picoglobalapi.cpp b/source/picoglobalapi.cpp
deleted file mode 100644
index cf9f722..0000000
--- a/source/picoglobalapi.cpp
+++ /dev/null
@@ -1,46 +0,0 @@
-
-#include <string>
-
-#include "picoglobalapi.h"
-#include "graphics.h"
-
-
-Graphics* _graphicsForGlobalCApi;
-
-void initGlobalApi(Graphics* graphics){
-    _graphicsForGlobalCApi = graphics;
-}
-
-void cls(){
-    _graphicsForGlobalCApi->cls();
-}
-void pset(short x, short y, uint8_t col){
-    _graphicsForGlobalCApi->pset(x, y, col);
-}
-uint8_t pget(short x, short y){
-    return _graphicsForGlobalCApi->pget(x, y);
-}
-void color(uint8_t c){
-    _graphicsForGlobalCApi->color(c);
-}
-void line (short x1, short y1, short x2, short y2, uint8_t col){
-    _graphicsForGlobalCApi->line(x1, y1, x2, y2, col);
-}
-void circ(short ox, short oy, short r, uint8_t col){
-    _graphicsForGlobalCApi->circ(ox, oy, r, col);
-}
-void circfill(short ox, short oy, short r, uint8_t col){
-    _graphicsForGlobalCApi->circfill(ox, oy, r, col);
-}
-void rect(short x1, short y1, short x2, short y2, uint8_t col){
-    _graphicsForGlobalCApi->rect(x1, y1, x2, y2, col);
-}
-void rectfill(short x1, short y1, short x2, short y2, uint8_t col){
-    _graphicsForGlobalCApi->rectfill(x1, y1, x2, y2, col);
-}
-short print(std::string str, short x, short y, uint16_t 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) {
-    _graphicsForGlobalCApi->spr(n, x, y, w, h, flip_x, flip_y);
-}
diff --git a/source/picoglobalapi.h b/source/picoglobalapi.h
deleted file mode 100644
index e9282c1..0000000
--- a/source/picoglobalapi.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#pragma once
-
-#include <string>
-
-#include "graphics.h"
-
-//this can probably go away when I'm loading actual carts and just have to expose api to lua
-void initGlobalApi(Graphics* graphics);
-
-void cls();
-void pset(short x, short y, uint8_t col);
-uint8_t pget(short x, short y);
-void color(uint8_t c);
-void line (short x1, short y1, short x2, short y2, uint8_t col);
-void circ(short ox, short oy, short r, uint8_t col);
-void circfill(short ox, short oy, short r, uint8_t col);
-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
index 23d31fa..749686a 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -3,6 +3,7 @@

 #include "picoluaapi.h"
 #include "graphics.h"
+#include "Input.h"

 extern "C" {
   #include <lua.h>
@@ -11,12 +12,15 @@ extern "C" {
 }

 Graphics* _graphicsForLuaApi;
+Input* _inputForLuaApi;

-void initPicoApi(Graphics* graphics){
+void initPicoApi(Graphics* graphics, Input* input){
     _graphicsForLuaApi = graphics;
+    _inputForLuaApi = input;
 }

 /*functions to expose to lua*/
+//Graphics
 int cls(lua_State *L){
     _graphicsForLuaApi->cls();

@@ -124,3 +128,25 @@ int spr(lua_State *L) {

     return 0;
 }
+
+//Input
+
+//input api
+int btn(lua_State *L){
+    double i = lua_tonumber(L,1);
+
+    bool pressed = _inputForLuaApi->btn((short)i);
+
+    lua_pushboolean(L, pressed);
+
+    return 1;
+}
+int btnp(lua_State *L){
+    double i = lua_tonumber(L,1);
+
+    bool pressed = _inputForLuaApi->btnp((short)i);
+
+    lua_pushboolean(L, pressed);
+
+    return 1;
+}
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index e605893..724a031 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -9,10 +9,12 @@ extern "C" {
 }

 #include "graphics.h"
+#include "Input.h"

 //this can probably go away when I'm loading actual carts and just have to expose api to lua
-void initPicoApi(Graphics* graphics);
+void initPicoApi(Graphics* graphics, Input* input);

+//graphics api
 int cls(lua_State *L);
 int pset(lua_State *L);
 int pget(lua_State *L);
@@ -25,3 +27,6 @@ int rectfill(lua_State *L);
 int print(lua_State *L);
 int spr(lua_State *L);

+//input api
+int btn(lua_State *L);
+int btnp(lua_State *L);
diff --git a/testcart.p8 b/testcart.p8
index 90e4a37..f2409bd 100644
--- a/testcart.p8
+++ b/testcart.p8
@@ -19,6 +19,11 @@ local r1dy = 2
 local r2dx = 2
 local r2dy = 2

+local sprx = 40;
+local spry = 40;
+
+local sprn = 7;
+
 function _draw()
 	cls()

@@ -50,10 +55,17 @@ function _draw()

     print("test str", 64, 64, 7)

-    spr(7, 40, 40, 1, 1, false, false)
+    spr(sprn, sprx, spry, 1, 1, false, false)
 end

 function _update()
+    if btn(0) then sprx = sprx - 2 end
+	if btn(1) then sprx = sprx + 2 end
+	if btn(2) then spry = spry - 2 end
+	if btn(3) then spry = spry + 2 end
+	--if btnp(4) then sprn +=1 end
+    --if btnp(5) then sprn -=1 end
+
     if (r1x0 < 0 or r1x1 > 127) then
         r1dx = -1 * r1dx;
     end