summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-12-31 01:18:15 +0100
committerkdx <kikoodx@paranoici.org>2023-12-31 01:18:15 +0100
commitda025eaa5350bfb71053409ac1cbf3f1ae2ea572 (patch)
tree63bbe2d33d4af7c1a4a3c770592231bf5fad62e0
parent65739fd6e94c291fb7d73fa6b133b7421dc7c776 (diff)
download007-da025eaa5350bfb71053409ac1cbf3f1ae2ea572.tar.gz
change player direction or smthg
-rw-r--r--inc/player.h4
-rw-r--r--src/main.c18
-rw-r--r--src/player.c17
-rw-r--r--vendors/_.h5
-rw-r--r--vendors/input.c309
-rw-r--r--vendors/input.h70
6 files changed, 418 insertions, 5 deletions
diff --git a/inc/player.h b/inc/player.h
new file mode 100644
index 0000000..de9c762
--- /dev/null
+++ b/inc/player.h
@@ -0,0 +1,4 @@
+#pragma once
+
+void player_update(void);
+void player_draw(void);
diff --git a/src/main.c b/src/main.c
index de7b5ff..d433ff1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,5 @@
#include <time.h>
+#include "player.h"
static int _main_loop(void *udata);
@@ -25,6 +26,16 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
.pixel_perfect=true,
.target_fps=60) == 0);
defer(TZR_Quit);
+ input_new_action("action");
+ input_bind_action_mb("action", SDL_BUTTON_LEFT);
+ input_bind_action_mb("action", SDL_BUTTON_RIGHT);
+ input_bind_action_mb("action", SDL_BUTTON_MIDDLE);
+ input_bind_action_sc("action", SDL_SCANCODE_SPACE);
+ input_bind_action_sc("action", SDL_SCANCODE_J);
+ input_bind_action_cb("action", SDL_CONTROLLER_BUTTON_A);
+ input_bind_action_cb("action", SDL_CONTROLLER_BUTTON_B);
+ input_bind_action_cb("action", SDL_CONTROLLER_BUTTON_X);
+ input_bind_action_cb("action", SDL_CONTROLLER_BUTTON_Y);
TZR_MainLoop(_main_loop, nullptr);
return 0;
}
@@ -32,8 +43,10 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
static int
_main_loop([[maybe_unused]] void *udata)
{
+ player_update();
+
assert(TZR_DrawBegin() == 0);
- if (rand() % 10 == 0) {
+ if (rand() % 1 == 0) {
int r; do r = rand(); while ((r & 7) == 0);
TZR_DrawSetColor(r & 1, (r & 2) != 0, (r & 4) != 0);
TZR_DrawClear();
@@ -60,8 +73,7 @@ _main_loop([[maybe_unused]] void *udata)
.sy=1.0+randf()*.1);
}
world_draw();
- //map_draw();
- //player_draw();
+ player_draw();
} else {
TZR_DrawSetColor(0, 0, 0);
TZR_DrawClear();
diff --git a/src/player.c b/src/player.c
new file mode 100644
index 0000000..a2d594d
--- /dev/null
+++ b/src/player.c
@@ -0,0 +1,17 @@
+#include "player.h"
+
+static int2 dir = I2(1, 0);
+
+void
+player_update(void)
+{
+ if (input_pressed("action")) {
+ dir = I2(dir.y, -dir.x);
+ int2_log(log, dir);
+ }
+}
+
+void
+player_draw(void)
+{
+}
diff --git a/vendors/_.h b/vendors/_.h
index 544ff29..d5662dc 100644
--- a/vendors/_.h
+++ b/vendors/_.h
@@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "input.h"
void wdeinit(void);
[[nodiscard]] void *alloc(size_t size);
@@ -97,8 +98,8 @@ int2 int2_inv(int2 a);
int2 int4_xy(int4 a);
int2 int4_zw(int4 a);
-#define int2_log(L, A) log_##L("%d %d", (A).x, (A).y)
-#define float2_log(L, A) log_##L("%f %f", (A).x, (A).y)
+#define int2_log(L, A) p##L("%d %d", (A).x, (A).y)
+#define float2_log(L, A) p##L("%f %f", (A).x, (A).y)
#include "TZR.h"
#include "config.h"
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;
+}
diff --git a/vendors/input.h b/vendors/input.h
new file mode 100644
index 0000000..f7f0239
--- /dev/null
+++ b/vendors/input.h
@@ -0,0 +1,70 @@
+#pragma once
+#include "TZR.h"
+#include <stdbool.h>
+
+#define INPUT_TAG_LEN 32
+
+typedef enum InputBindType {
+ IBT_SCANCODE,
+ IBT_CONBUTTON,
+ IBT_MOUSEBUTTON
+} InputBindType;
+
+typedef struct InputBind InputBind;
+struct InputBind {
+ InputBindType type;
+ union {
+ SDL_Scancode sc;
+ uint8_t cb;
+ uint8_t mb;
+ };
+ InputBind *next;
+};
+
+typedef struct InputAction InputAction;
+struct InputAction {
+ char tag[INPUT_TAG_LEN];
+ InputBind binds;
+ InputAction *next;
+};
+
+typedef enum InputPairType {
+ IPT_ACTION,
+ IPT_STICK
+} InputPairType;
+
+typedef struct InputPair InputPair;
+struct InputPair {
+ InputPairType type;
+ union {
+ struct {
+ char left[INPUT_TAG_LEN];
+ char right[INPUT_TAG_LEN];
+ };
+ float (*stick)(void);
+ };
+ InputPair *next;
+};
+
+typedef struct InputAxis InputAxis;
+struct InputAxis {
+ char tag[INPUT_TAG_LEN];
+ InputPair pairs;
+ InputAxis *next;
+};
+
+void input_deinit(void);
+void input_new_action(const char *tag);
+void input_new_axis(const char *tag);
+void input_bind_action_sc(const char *tag, SDL_Scancode sc);
+void input_bind_action_cb(const char *tag, uint8_t cb);
+void input_bind_action_mb(const char *tag, uint8_t mb);
+void input_bind_axis_gc(const char *tag, SDL_GameControllerAxis ax);
+void input_bind_axis_act(const char *tag, const char *l, const char *r);
+
+bool input_down(const char *tag);
+bool input_up(const char *tag);
+bool input_pressed(const char *tag);
+bool input_released(const char *tag);
+int input_axis(const char *tag);
+float input_axisf(const char *tag);