From ca32c2ce76a17289757eb42c6cde5a3c722a4af1 Mon Sep 17 00:00:00 2001 From: Julia <145168563+julia-aph@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:05:56 -0500 Subject: [PATCH] oh oh --- source/game/hash.c | 23 +++++++++++++ source/game/hash.h | 12 +++++++ source/io/ctrl.c | 59 +++++++++++--------------------- source/io/ctrl.h | 3 +- source/io/input.c | 37 +++++++++++++------- source/io/input.h | 11 ++++-- source/io/platforms/winhandler.c | 12 +++---- 7 files changed, 97 insertions(+), 60 deletions(-) create mode 100644 source/game/hash.c create mode 100644 source/game/hash.h diff --git a/source/game/hash.c b/source/game/hash.c new file mode 100644 index 0000000..5c5ee3b --- /dev/null +++ b/source/game/hash.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include + +#include "fumotris.h" + +typedef u32 hashtype; + +hashtype Hash(void *item, size_t size) +{ + u8 *data = (u8 *)item; + + u32 h = 98317; + for (size_t i = 0; i < size; i++) { + h ^= data[i]; + h *= 0x5bd1e995; + h ^= h >> 15; + } + + return h; +} \ No newline at end of file diff --git a/source/game/hash.h b/source/game/hash.h new file mode 100644 index 0000000..3db0175 --- /dev/null +++ b/source/game/hash.h @@ -0,0 +1,12 @@ +#pragma once +#include +#include +#include +#include +#include + +#include "fumotris.h" + +typedef u32 hashtype; + +hashtype Hash(void *item, size_t size); \ No newline at end of file diff --git a/source/io/ctrl.c b/source/io/ctrl.c index b5bf6b9..3bc7cce 100644 --- a/source/io/ctrl.c +++ b/source/io/ctrl.c @@ -8,6 +8,7 @@ #include #include "fumotris.h" +#include "hash.h" #define IO_BUF_SIZE 16 @@ -19,24 +20,23 @@ enum InputType { }; struct Button { - u32 value; - bool is_down; - bool is_held; - bool is_up; -}; - + u64 value; +} but; struct Axis { i64 value; -}; - +} axis; struct Joystick { i32 x; i32 y; -}; +} js; struct InputRecord { - u16 bind; + u16f bind; + u8 type; + u8 is_down; + u8 is_held; + u8 is_up; union { struct Button but; @@ -48,39 +48,12 @@ struct InputRecord { }; struct InputAxis { - u8 type; - - union { - struct Button but; - struct Axis axis; - struct Joystick js; - }; + struct input; struct timespec last_pressed; struct timespec last_released; }; -typedef u32 hashtype; -hashtype Hash(void *item, size_t size) -{ - u8 *data = (u8 *)item; - - u32 h = 98317; - for (size_t i = 0; i < size; i++) { - h ^= data[i]; - h *= 0x5bd1e995; - h ^= h >> 15; - } - - return h; -} - -hashtype hash_id(u16f value, u8f type) -{ - struct { u16 id; u8 type; } id = { value, type }; - return Hash(&id, sizeof(id)); -} - struct ctrl_dict { size_t capacity; size_t filled; @@ -99,9 +72,10 @@ struct Controller { struct ctrl_dict binds; struct InputAxis *axes; - struct { + struct InputBuffer { struct InputRecord records[IO_BUF_SIZE]; size_t len; + size_t start; } input_buf; struct { @@ -138,6 +112,7 @@ bool NewCtrl(struct Controller *ctrl, size_t code_cap, size_t bind_cap) .input_buf = { .len = 0, + .start = 0, }, .pending_buf = { .len = 0, @@ -170,6 +145,12 @@ size_t wrap_index(size_t i, size_t max) return i % (SIZE_MAX - max + 1); } +hashtype hash_id(u16f value, u8f type) +{ + struct { u16 id; u8 type; } id = { value, type }; + return Hash(&id, sizeof(id)); +} + bool find_or_set(struct ctrl_dict *dict, struct ctrl_bkt **out, u16f value, u8f type) { hashtype hash = hash_id(value, type); diff --git a/source/io/ctrl.h b/source/io/ctrl.h index b4380af..7baefe3 100644 --- a/source/io/ctrl.h +++ b/source/io/ctrl.h @@ -94,9 +94,10 @@ struct Controller { struct ctrl_dict binds; struct InputAxis *axes; - struct { + struct InputBuffer { struct InputRecord records[IO_BUF_SIZE]; size_t len; + size_t start; } input_buf; struct { diff --git a/source/io/input.c b/source/io/input.c index 3af6458..ab8e8e1 100644 --- a/source/io/input.c +++ b/source/io/input.c @@ -12,32 +12,45 @@ #include "win.h" #endif +struct Input { + struct Controller ctrl; + + pthread_t thread; + pthread_mutex_t access_mutex; +}; + +struct InputRecord *get_at(struct InputBuffer *buf, size_t i) +{ + return &buf->records[(buf->start + i) % IO_BUF_SIZE]; +} + void *block_input(void *args_ptr) { - struct RecordBuffer *rec_buf = args_ptr; + struct Input *in = args_ptr; + struct InputBuffer tmp_buf = { .len = 0 }; while (true) { - bool success; - #ifdef _WIN32 - success = WindowsBlockInput(rec_buf); + if (!WindowsReadInputBuffer(&tmp_buf)) + return false; #endif - - if (!success) { - printf("winfail"); - exit(1); + + pthread_mutex_lock(&in->access_mutex); + for (size_t i = 0; i < tmp_buf.len; i++) { + *get_at(&in->ctrl.input_buf, i) = *get_at(); } + pthread_mutex_unlock(&in->access_mutex); } return nullptr; } -void StartInput(struct Controller *ctrl) +bool StartInput(struct Input *in) { - pthread_create(&ctrl->thread, nullptr, block_input, &ctrl->buf); + return pthread_create(&in->thread, nullptr, block_input, in) == 0; } -void JoinInput(struct Controller *ctrl) +bool JoinInput(struct Input *in) { - pthread_join(ctrl->thread, nullptr); + return pthread_join(in->thread, nullptr) == 0; } \ No newline at end of file diff --git a/source/io/input.h b/source/io/input.h index 3b9ba11..3c5e433 100644 --- a/source/io/input.h +++ b/source/io/input.h @@ -8,6 +8,13 @@ #include "ctrl.h" #include "fumotris.h" -void StartInput(struct Controller *ctrl); +struct Input { + struct Controller ctrl; -void JoinInput(struct Controller *ctrl); \ No newline at end of file + pthread_t thread; + pthread_mutex_t access_mutex; +}; + +bool StartInput(struct Input *in); + +bool JoinInput(struct Input *in); \ No newline at end of file diff --git a/source/io/platforms/winhandler.c b/source/io/platforms/winhandler.c index d813b30..9865469 100644 --- a/source/io/platforms/winhandler.c +++ b/source/io/platforms/winhandler.c @@ -67,8 +67,8 @@ void set_key_record(struct InputRecord *rec, KEY_EVENT_RECORD win_key) rec->type = KEY; rec->bind = win_key.wVirtualKeyCode; - rec->button.is_down = win_key.bKeyDown; - rec->button.is_up = !win_key.bKeyDown; + rec->but.is_down = win_key.bKeyDown; + rec->but.is_up = !win_key.bKeyDown; if (win_key.wVirtualKeyCode == VK_ESCAPE) rec->type = ESCAPE; @@ -90,8 +90,8 @@ bool set_mouse_record(struct InputRecord *rec, MOUSE_EVENT_RECORD win_mouse) case MOUSE_MOVED: rec->type = JOYSTICK; rec->bind = 0; - rec->joystick.x = win_mouse.dwMousePosition.X; - rec->joystick.y = win_mouse.dwMousePosition.Y; + rec->js.x = win_mouse.dwMousePosition.X; + rec->js.y = win_mouse.dwMousePosition.Y; break; default: return false; @@ -116,9 +116,9 @@ bool dispatch_record(struct InputRecord *rec, INPUT_RECORD win_rec) return true; } -bool WinBlockInput(struct RecordBuffer *buf) +bool WinBlockInput(struct Controller *ctrl) { - size_t win_size = IO_BUF_SIZE - buf->count; + size_t win_size = IO_BUF_SIZE - ctrl->input_buf.len; INPUT_RECORD win_buf[win_size]; DWORD count;