summaryrefslogtreecommitdiff
path: root/vendors/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'vendors/input.c')
-rw-r--r--vendors/input.c309
1 files changed, 309 insertions, 0 deletions
diff --git a/vendors/input.c b/vendors/input.c
new file mode 100644
index 0000000..c396180
--- /dev/null
+++ b/vendors/input.c
@@ -0,0 +1,309 @@
+#include "input.h"
+
+#ifndef foreach
+#define foreach(E, L) for (__auto_type E = (L); E != NULL; E = E->next)
+#endif
+#ifndef plog
+#define plog(...) \
+ printf("\x1b[94mLOG %s:%s:%d\x1b[0m \t", \
+ __FILE_NAME__, __FUNCTION__, __LINE__), \
+ printf(__VA_ARGS__), putchar('\n')
+#endif
+#ifndef panic
+#define panic(...) plog(__VA_ARGS__), exit(EXIT_FAILURE)
+#endif
+
+static InputAction _actions = {0};
+static InputAxis _axis = {0};
+
+void
+input_deinit(void)
+{
+ InputAction *action = _actions.next;
+ while (action != NULL) {
+ InputBind *bind = action->binds.next;
+ while (bind != NULL) {
+ const __auto_type next = bind->next;
+ free(bind);
+ bind = next;
+ }
+ const __auto_type next = action->next;
+ free(action);
+ action = next;
+ }
+ InputAxis *axis = _axis.next;
+ while (axis != NULL) {
+ InputPair *pair = axis->pairs.next;
+ while (pair != NULL) {
+ const __auto_type next = pair->next;
+ free(pair);
+ pair = next;
+ }
+ const __auto_type next = axis->next;
+ free(axis);
+ axis = next;
+ }
+}
+
+void
+input_new_action(const char *tag)
+{
+ InputAction *action = malloc(sizeof(InputAction));
+ if (action == NULL)
+ panic("malloc failed");
+ memset(action, 0, sizeof(*action));
+
+ strncpy(action->tag, tag, INPUT_TAG_LEN - 1);
+ action->next = _actions.next;
+ _actions.next = action;
+}
+
+void
+input_new_axis(const char *tag)
+{
+ InputAxis *axis = malloc(sizeof(InputAxis));
+ if (axis == NULL)
+ panic("malloc failed");
+ memset(axis, 0, sizeof(*axis));
+
+ strncpy(axis->tag, tag, INPUT_TAG_LEN - 1);
+ axis->next = _axis.next;
+ _axis.next = axis;
+}
+
+InputAction *
+_input_get_action(const char *tag)
+{
+ foreach (e, _actions.next)
+ if (strcmp(e->tag, tag) == 0)
+ return e;
+ plog("action '%s' not found", tag);
+ return NULL;
+}
+
+InputBind *
+_input_new_bind(const char *tag, InputBindType ibt)
+{
+ InputAction *action = _input_get_action(tag);
+ if (action == NULL) {
+ plog("_input_get_action failed");
+ return NULL;
+ }
+
+ InputBind *bind = malloc(sizeof(InputBind));
+ if (bind == NULL)
+ panic("walloc failed");
+ memset(bind, 0, sizeof(*bind));
+
+ bind->type = ibt;
+ bind->next = action->binds.next;
+ action->binds.next = bind;
+ return bind;
+}
+
+void
+input_bind_action_sc(const char *tag, SDL_Scancode sc)
+{
+ InputBind *bind = _input_new_bind(tag, IBT_SCANCODE);
+ if (bind == NULL)
+ panic("_input_new_bind failed");
+ bind->sc = sc;
+}
+
+void
+input_bind_action_cb(const char *tag, uint8_t cb)
+{
+ InputBind *bind = _input_new_bind(tag, IBT_CONBUTTON);
+ if (bind == NULL)
+ panic("_input_new_bind failed");
+ bind->cb = cb;
+}
+
+void
+input_bind_action_mb(const char *tag, uint8_t mb)
+{
+ InputBind *bind = _input_new_bind(tag, IBT_MOUSEBUTTON);
+ if (bind == NULL)
+ panic("_input_new_bind failed");
+ bind->mb = mb;
+}
+
+bool
+input_down(const char *tag)
+{
+ InputAction *action = _input_get_action(tag);
+ if (action == NULL)
+ panic("_input_get_action failed");
+
+ foreach (e, action->binds.next) switch (e->type) {
+ case IBT_SCANCODE: if (TZR_IsKeyDown(e->sc)) return true; break;
+ case IBT_CONBUTTON: if (TZR_JoystickDown(e->cb)) return true; break;
+ case IBT_MOUSEBUTTON: if (TZR_MouseDown(e->mb)) return true; break;
+ default: __builtin_unreachable();
+ }
+ return false;
+}
+
+bool
+input_up(const char *tag)
+{
+ InputAction *action = _input_get_action(tag);
+ if (action == NULL)
+ panic("_input_get_action failed");
+
+ foreach (e, action->binds.next) switch (e->type) {
+ case IBT_SCANCODE: if (TZR_IsKeyDown(e->sc)) return false; break;
+ case IBT_CONBUTTON: if (TZR_JoystickDown(e->cb)) return false; break;
+ case IBT_MOUSEBUTTON: if (TZR_MouseDown(e->mb)) return false; break;
+ default: __builtin_unreachable();
+ }
+ return true;
+}
+
+bool
+input_pressed(const char *tag)
+{
+ InputAction *action = _input_get_action(tag);
+ if (action == NULL)
+ panic("_input_get_action failed");
+
+ foreach (e, action->binds.next) switch (e->type) {
+ case IBT_SCANCODE: if (TZR_IsKeyPressed(e->sc)) return true; break;
+ case IBT_CONBUTTON: if (TZR_JoystickPressed(e->cb)) return true; break;
+ case IBT_MOUSEBUTTON: if (TZR_MousePressed(e->mb)) return true; break;
+ default: __builtin_unreachable();
+ }
+ return false;
+}
+
+bool
+input_released(const char *tag)
+{
+ InputAction *action = _input_get_action(tag);
+ if (action == NULL)
+ panic("_input_get_action failed");
+
+ foreach (e, action->binds.next) switch (e->type) {
+ case IBT_SCANCODE: if (TZR_IsKeyReleased(e->sc)) return true; break;
+ case IBT_CONBUTTON: if (TZR_JoystickReleased(e->cb)) return true; break;
+ case IBT_MOUSEBUTTON: if (TZR_MouseReleased(e->mb)) return true; break;
+ default: __builtin_unreachable();
+ }
+ return false;
+}
+
+InputAxis *
+_input_get_axis(const char *tag)
+{
+ foreach (e, _axis.next)
+ if (strcmp(e->tag, tag) == 0)
+ return e;
+ plog("axis '%s' not found", tag);
+ return NULL;
+}
+
+InputPair *
+_input_new_pair(const char *tag, InputPairType ipt)
+{
+ InputAxis *axis = _input_get_axis(tag);
+ if (axis == NULL) {
+ plog("_input_get_axis failed");
+ return NULL;
+ }
+
+ InputPair *pair = malloc(sizeof(InputPair));
+ if (pair == NULL)
+ panic("malloc failed");
+ memset(pair, 0, sizeof(*pair));
+
+ pair->type = ipt;
+ pair->next = axis->pairs.next;
+ axis->pairs.next = pair;
+ return pair;
+}
+
+void
+input_bind_axis_gc(const char *tag, SDL_GameControllerAxis ax)
+{
+ InputPair *pair = _input_new_pair(tag, IPT_STICK);
+ if (pair == NULL)
+ panic("_input_new_pair failed");
+
+ switch (ax) {
+ case SDL_CONTROLLER_AXIS_LEFTX:
+ pair->stick = TZR_JoystickLeftX;
+ break;
+ case SDL_CONTROLLER_AXIS_LEFTY:
+ pair->stick = TZR_JoystickLeftY;
+ break;
+ case SDL_CONTROLLER_AXIS_RIGHTX:
+ pair->stick = TZR_JoystickRightX;
+ break;
+ case SDL_CONTROLLER_AXIS_RIGHTY:
+ pair->stick = TZR_JoystickRightY;
+ break;
+ default:
+ panic("unknown axis '%d'", ax);
+ }
+}
+
+void
+input_bind_axis_act(const char *tag, const char *a0, const char *a1)
+{
+ InputPair *pair = _input_new_pair(tag, IPT_ACTION);
+ if (pair == NULL)
+ panic("_input_new_pair failed");
+
+ strncpy(pair->left, a0, INPUT_TAG_LEN - 1);
+ strncpy(pair->right, a1, INPUT_TAG_LEN - 1);
+}
+
+int
+input_axis(const char *tag)
+{
+ InputAxis *axis = _input_get_axis(tag);
+ if (axis == NULL)
+ panic("_input_get_axis failed");
+
+ foreach (e, axis->pairs.next) switch (e->type) {
+ case IPT_ACTION: {
+ const __auto_type diff = input_down(e->right) - input_down(e->left);
+ if (diff)
+ return diff;
+ break;
+ }
+ case IPT_STICK: {
+ const __auto_type diff = e->stick();
+ if (diff)
+ return (diff > 0) - (diff < 0);
+ break;
+ }
+ default: __builtin_unreachable();
+ }
+ return 0;
+}
+
+float
+input_axisf(const char *tag)
+{
+ InputAxis *axis = _input_get_axis(tag);
+ if (axis == NULL)
+ panic("_input_get_axis failed");
+
+ foreach (e, axis->pairs.next) switch (e->type) {
+ case IPT_ACTION: {
+ const __auto_type diff = input_down(e->right) - input_down(e->left);
+ if (diff)
+ return diff;
+ break;
+ }
+ case IPT_STICK: {
+ const __auto_type diff = e->stick();
+ if (diff)
+ return diff;
+ break;
+ }
+ default: __builtin_unreachable();
+ }
+ return 0;
+}