foxygit / fake-08-xbox360 Log in
commit 7230ded66ba9381645a51758cc731b897858d7af
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Mon May 4 11:18:58 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Mon May 4 11:18:58 2020 -0600

    Update input to repeat btnp correctly
---
 inputtest.p8     | 28 ++++++++++++++++++++++++++++
 source/Input.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 source/Input.h   |  9 +++++++++
 source/main.cpp  | 12 ++----------
 4 files changed, 83 insertions(+), 12 deletions(-)

diff --git a/inputtest.p8 b/inputtest.p8
new file mode 100644
index 0000000..7e04803
--- /dev/null
+++ b/inputtest.p8
@@ -0,0 +1,28 @@
+pico-8 cartridge // http://www.pico-8.com
+version 22
+__lua__
+function _update()
+
+end
+
+function _draw()
+	cls()
+	for i=0, 6, 1 do
+		if(btn(i)) then
+			print('btn ' .. i)
+		end
+	end
+
+	for i=0, 6, 1 do
+		if(btnp(i)) then
+			print('btnp ' .. i)
+		end
+	end
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/source/Input.cpp b/source/Input.cpp
index b8ab3a4..360ede0 100644
--- a/source/Input.cpp
+++ b/source/Input.cpp
@@ -3,13 +3,55 @@
 void Input::SetState(uint8_t kdown, uint8_t kheld){
     _currentKDown = kdown;
     _currentKHeld = kheld;
+
+    //TODO: this can be overridden by poking certain memory address
+    for (int i = 0; i < 8; i ++) {
+        bool down = BITMASK(i) & _currentKHeld;
+
+        _framesHeld[i] = down ? _framesHeld[i] + 1 : 0;
+
+        //update kdown to be true if held for 15 frames, then every 4th after that.
+        //from wiki:
+        //btnp() implements a keyboard-like repeat mechanism: if the player holds
+        //the button for 15 frames, it registers as on again for one frame, then
+        //again every four frames after that. The frame counter resets when the
+        //player releases the button.
+
+        bool repeatPressed =
+            (_framesHeld[i] == 15) ||
+            (_framesHeld[i] / 15 >= 1 && _framesHeld[i] % 4 == 0);
+
+        if (repeatPressed) {
+            _currentKDown = _currentKDown | BITMASK(i);
+        }
+    }
+}
+
+uint8_t Input::btn(){
+    return _currentKHeld;
+}
+
+//todo: repetition behavior to match pico 8
+uint8_t Input::btnp(){
+    return _currentKDown;
 }

 bool Input::btn(uint8_t i){
-    return BITMASK(i) & _currentKHeld;
+    return BITMASK(i) & btn();
 }

 //todo: repetition behavior to match pico 8
 bool Input::btnp(uint8_t i){
-    return BITMASK(i) & _currentKDown;
+    return BITMASK(i) & btnp();
+}
+
+bool Input::btn(uint8_t i, uint8_t p){
+    //no multiplayer support for now
+    return p == 0 ? btn(i) : 0;
+}
+
+//todo: repetition behavior to match pico 8
+bool Input::btnp(uint8_t i, uint8_t p){
+    //no multiplayer support for now
+    return p == 0 ? btn(i) : 0;
 }
diff --git a/source/Input.h b/source/Input.h
index a40db1b..3b4c906 100644
--- a/source/Input.h
+++ b/source/Input.h
@@ -20,10 +20,19 @@ enum
 class Input{
 	uint8_t _currentKDown;
 	uint8_t _currentKHeld;
+
+	uint16_t _framesHeld[8];

     public:
     void SetState(uint8_t kdown, uint8_t kheld);

+
+	uint8_t btn();
+    uint8_t btnp();
+
     bool btn(uint8_t i);
     bool btnp(uint8_t i);
+
+	bool btn(uint8_t i, uint8_t p);
+    bool btnp(uint8_t i, uint8_t p);
 };
\ No newline at end of file
diff --git a/source/main.cpp b/source/main.cpp
index 9d7771e..84cdacf 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -120,7 +120,7 @@ int main(int argc, char* argv[])
 	Logger::Write("Cart Loaded\n");

 	#else
-	console->LoadCart("testcart.p8");
+	console->LoadCart("inputtest.p8");
 	#endif

 	// Main loop
@@ -132,8 +132,6 @@ int main(int argc, char* argv[])
 	std::function<void()> clearFb = clear3dsFrameBuffer;
 	std::function<void()> postFlip = postFlip3dsFunction;

-	bool rWasDown = false;
-
 	while (aptMainLoop())
 	{
 		//Scan all the inputs. This should be done once for each frame
@@ -178,16 +176,10 @@ int main(int argc, char* argv[])

 		if (lpressed && rpressed) break; // break in order to return to hbmenu

-		if (rWasDown && !rpressed) {
+		if (kDown & KEY_R) {
 			ChangeStretch();
 		}

-		if (rpressed) {
-			rWasDown = true;
-		} else {
-			rWasDown = false;
-		}
-
 		uint8_t p8kDown = ConvertInputToP8(kDown);
 		uint8_t p8kHeld = ConvertInputToP8(kHeld);