aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-10 13:21:22 +0200
committerkdx <kikoodx@paranoici.org>2023-07-10 13:21:22 +0200
commitde846cd6b857a1e41b0eb9204f9bb4a9fe1b51f3 (patch)
tree492d5ea057a32c7c8447ec9ff4b7c252a868680d
parente8a9f0e2bbc766d4767a516b98167c97ff410948 (diff)
downloadtzr-de846cd6b857a1e41b0eb9204f9bb4a9fe1b51f3.tar.gz
joystick button input
-rwxr-xr-xcreate_TZR.h.sh1
-rw-r--r--headers/TZR_globals.h1
-rw-r--r--headers/TZR_joystick.h9
-rw-r--r--sources/TZR_CycleEvents.c2
-rw-r--r--sources/TZR_JoystickDown.c8
-rw-r--r--sources/TZR_JoystickGetState.c7
-rw-r--r--sources/TZR_JoystickPressed.c7
-rw-r--r--sources/TZR_JoystickReleased.c7
-rw-r--r--sources/TZR_JoystickUp.c8
-rw-r--r--sources/TZR_PollEvent.c6
-rw-r--r--sources/globals.c1
11 files changed, 55 insertions, 2 deletions
diff --git a/create_TZR.h.sh b/create_TZR.h.sh
index aacd282..1d1c88e 100755
--- a/create_TZR.h.sh
+++ b/create_TZR.h.sh
@@ -18,6 +18,7 @@ HEADER=TZR_events ProcessHeader
HEADER=TZR_render ProcessHeader
HEADER=TZR_keystate ProcessHeader
HEADER=TZR_mouse ProcessHeader
+HEADER=TZR_joystick ProcessHeader
HEADER=TZR ProcessHeader
cp _head.h "$BUILDDIR/out"
diff --git a/headers/TZR_globals.h b/headers/TZR_globals.h
index db03ce8..e12f31d 100644
--- a/headers/TZR_globals.h
+++ b/headers/TZR_globals.h
@@ -20,6 +20,7 @@ extern int ___tzr_mouse_x;
extern int ___tzr_mouse_y;
extern TZR_KeyState ___tzr_keystates[SDL_NUM_SCANCODES];
extern TZR_KeyState ___tzr_mousestates[256];
+extern TZR_KeyState ___tzr_joystates[256];
extern float ___tzr_scale;
extern int ___tzr_off_x;
extern int ___tzr_off_y;
diff --git a/headers/TZR_joystick.h b/headers/TZR_joystick.h
new file mode 100644
index 0000000..ce1a49a
--- /dev/null
+++ b/headers/TZR_joystick.h
@@ -0,0 +1,9 @@
+#pragma once
+#include "TZR_types.h"
+#include <SDL2/SDL_joystick.h>
+
+TZR_KeyState TZR_JoystickGetState(uint8_t button);
+bool TZR_JoystickDown(uint8_t button);
+bool TZR_JoystickUp(uint8_t button);
+bool TZR_JoystickReleased(uint8_t button);
+bool TZR_JoystickPressed(uint8_t button);
diff --git a/sources/TZR_CycleEvents.c b/sources/TZR_CycleEvents.c
index b30ffe5..0a6a9af 100644
--- a/sources/TZR_CycleEvents.c
+++ b/sources/TZR_CycleEvents.c
@@ -28,6 +28,8 @@ TZR_CycleEvents(void)
next_state(&___tzr_keystates[i]);
for (int i = 0; i < 256; i++)
next_state(&___tzr_mousestates[i]);
+ for (int i = 0; i < 256; i++)
+ next_state(&___tzr_joystates[i]);
TZR_Event e;
while (TZR_PollEvent(&e))
;
diff --git a/sources/TZR_JoystickDown.c b/sources/TZR_JoystickDown.c
new file mode 100644
index 0000000..d723483
--- /dev/null
+++ b/sources/TZR_JoystickDown.c
@@ -0,0 +1,8 @@
+#include "TZR_joystick.h"
+
+bool
+TZR_JoystickDown(uint8_t button)
+{
+ const TZR_KeyState state = TZR_JoystickGetState(button);
+ return (state == TZR_KEYSTATE_DOWN || state == TZR_KEYSTATE_PRESS);
+}
diff --git a/sources/TZR_JoystickGetState.c b/sources/TZR_JoystickGetState.c
new file mode 100644
index 0000000..8357fc6
--- /dev/null
+++ b/sources/TZR_JoystickGetState.c
@@ -0,0 +1,7 @@
+#include "TZR_globals.h"
+
+TZR_KeyState
+TZR_JoystickGetState(uint8_t button)
+{
+ return ___tzr_joystates[button];
+}
diff --git a/sources/TZR_JoystickPressed.c b/sources/TZR_JoystickPressed.c
new file mode 100644
index 0000000..c1898bd
--- /dev/null
+++ b/sources/TZR_JoystickPressed.c
@@ -0,0 +1,7 @@
+#include "TZR_joystick.h"
+
+bool
+TZR_JoystickPressed(uint8_t button)
+{
+ return (TZR_JoystickGetState(button) == TZR_KEYSTATE_PRESS);
+}
diff --git a/sources/TZR_JoystickReleased.c b/sources/TZR_JoystickReleased.c
new file mode 100644
index 0000000..56c596b
--- /dev/null
+++ b/sources/TZR_JoystickReleased.c
@@ -0,0 +1,7 @@
+#include "TZR_joystick.h"
+
+bool
+TZR_JoystickReleased(uint8_t button)
+{
+ return (TZR_JoystickGetState(button) == TZR_KEYSTATE_RELEASE);
+}
diff --git a/sources/TZR_JoystickUp.c b/sources/TZR_JoystickUp.c
new file mode 100644
index 0000000..d8ab67b
--- /dev/null
+++ b/sources/TZR_JoystickUp.c
@@ -0,0 +1,8 @@
+#include "TZR_joystick.h"
+
+bool
+TZR_JoystickUp(uint8_t button)
+{
+ const TZR_KeyState state = TZR_JoystickGetState(button);
+ return (state == TZR_KEYSTATE_UP || state == TZR_KEYSTATE_RELEASE);
+}
diff --git a/sources/TZR_PollEvent.c b/sources/TZR_PollEvent.c
index 571149e..651a084 100644
--- a/sources/TZR_PollEvent.c
+++ b/sources/TZR_PollEvent.c
@@ -23,12 +23,12 @@ TZR_PollEvent(TZR_Event *e)
e->type = TZR_EV_KEYDOWN;
e->button = se.key.keysym.scancode;
___tzr_keystates[e->button] = TZR_KEYSTATE_PRESS;
- break;
+ return 1;
case SDL_KEYUP:
e->type = TZR_EV_KEYUP;
e->button = se.key.keysym.scancode;
___tzr_keystates[e->button] = TZR_KEYSTATE_RELEASE;
- break;
+ return 1;
case SDL_MOUSEBUTTONDOWN:
___tzr_mouse_x = se.button.x;
___tzr_mouse_y = se.button.y;
@@ -89,8 +89,10 @@ TZR_PollEvent(TZR_Event *e)
case SDL_JOYAXISMOTION:
break;
case SDL_JOYBUTTONDOWN:
+ ___tzr_keystates[se.jbutton.button] = TZR_KEYSTATE_PRESS;
break;
case SDL_JOYBUTTONUP:
+ ___tzr_keystates[se.jbutton.button] = TZR_KEYSTATE_RELEASE;
break;
default:
break;
diff --git a/sources/globals.c b/sources/globals.c
index 92d1348..2d69846 100644
--- a/sources/globals.c
+++ b/sources/globals.c
@@ -20,6 +20,7 @@ int ___tzr_mouse_x = 0;
int ___tzr_mouse_y = 0;
TZR_KeyState ___tzr_keystates[SDL_NUM_SCANCODES] = {0};
TZR_KeyState ___tzr_mousestates[256] = {0}; //doc says than mouse button is u8
+TZR_KeyState ___tzr_joystates[256] = {0}; //doc says than joy button is u8
float ___tzr_scale = 1.0;
int ___tzr_off_x = 1.0;
int ___tzr_off_y = 1.0;