From 1064e95edce7c5645a6504942b4a0c6c240bb489 Mon Sep 17 00:00:00 2001 From: Julia <145168563+julia-aph@users.noreply.github.com> Date: Tue, 23 Apr 2024 15:33:32 -0500 Subject: [PATCH] hhjkkj jkhkj --- source/io/ctrl.c | 23 ++++---- source/io/ctrl.h | 10 ++-- source/io/input.c | 51 +++++++++++++----- source/io/input.h | 29 +++++++--- source/io/platforms/platform.h | 2 +- source/io/platforms/win.c | 96 +++++++++++++++++++--------------- 6 files changed, 130 insertions(+), 81 deletions(-) diff --git a/source/io/ctrl.c b/source/io/ctrl.c index 0b8d337..8b4993e 100644 --- a/source/io/ctrl.c +++ b/source/io/ctrl.c @@ -144,11 +144,7 @@ void dispatch_update(struct InputAxis *axis, struct InputRecord *rec) axis->data = rec->data; } -bool is_alphanumeric(char c) { - return c >=32 and c <= 126; -} - -bool read_input_buf(struct Controller *ctrl) +void read_input_buf(struct Controller *ctrl) { for (size_t i = 0; i < ctrl->pending_buf.len; i++) { struct InputAxis *axis = ctrl->pending_buf.axes[i]; @@ -159,10 +155,9 @@ bool read_input_buf(struct Controller *ctrl) ctrl->pending_buf.len = 0; for (size_t i = 0; i < ctrl->buf.len; i++) { - struct InputRecord *rec = &ctrl->buf.recs[i]; + struct InputRecord *rec = &ctrl->buf.buf[i]; - union InputID rec_id = to_id(rec->bind, rec->type); - struct InputAxis *axis = find_axis(&ctrl->binds, rec_id); + struct InputAxis *axis = find_axis(&ctrl->binds, rec->id); if (axis == nullptr) continue; @@ -172,7 +167,11 @@ bool read_input_buf(struct Controller *ctrl) } ctrl->buf.len = 0; - return true; +} + +void read_str_buf(struct Controller *ctrl) +{ + ctrl->string } bool CtrlPoll(struct Controller *ctrl, struct InputThreadHandle *hand) @@ -181,6 +180,7 @@ bool CtrlPoll(struct Controller *ctrl, struct InputThreadHandle *hand) return false; read_input_buf(ctrl); + read_str_buf(ctrl); if (pthread_cond_signal(&hand->buf_read) != 0) return false; @@ -191,11 +191,6 @@ bool CtrlPoll(struct Controller *ctrl, struct InputThreadHandle *hand) return true; } -bool CtrlInputString(struct Controller *ctrl, size_t n, char *buf) -{ - -} - /*int main() { struct Controller ctrl; diff --git a/source/io/ctrl.h b/source/io/ctrl.h index 539748d..13b016e 100644 --- a/source/io/ctrl.h +++ b/source/io/ctrl.h @@ -21,9 +21,12 @@ struct InputAxis { }; union InputID id; - u8 is_down; - u8 is_held; - u8 is_up; + + struct { + u8f is_down : 1; + u8f is_held : 1; + u8f is_up : 1; + }; }; struct ctrl_bkt { @@ -45,6 +48,7 @@ struct ctrl_axis_vec { struct Controller { struct InputBuffer buf; + struct InputString string; struct { struct InputAxis *axes[IO_BUF_SIZE]; diff --git a/source/io/input.c b/source/io/input.c index 93e91c6..8765955 100644 --- a/source/io/input.c +++ b/source/io/input.c @@ -1,21 +1,26 @@ #include "input.h" +#include #include "platform.h" -struct InputRecord *buf_get(struct InputBuffer *buf, size_t i) { - return buf->recs + (buf->start + i) % IO_BUF_SIZE; +inline size_t min_size(size_t a, size_t b) { + return a < b ? a : b; } -size_t min_size(size_t a, size_t b) { - return a < b ? a : b; +inline size_t in_buf_empty(struct InputBuffer *in) { + return IO_BUF_SIZE - in->len; +} + +struct InputRecord *in_buf_get(struct InputBuffer *in, size_t i) { + return in->buf + (in->start + i) % IO_BUF_SIZE; } void InputBufferTransfer(struct InputBuffer *dest, struct InputBuffer *src) { - size_t copy_amt = min_size(IO_BUF_SIZE - dest->len, src->len); - + size_t copy_amt = min_size(in_buf_empty(dest), src->len); + for (size_t i = 0; i < copy_amt; i++) - *buf_get(dest, dest->len + i) = *buf_get(src, i); + *in_buf_get(dest, dest->len + i) = *in_buf_get(src, i); if (copy_amt < src->len) src->start += copy_amt; @@ -26,16 +31,35 @@ void InputBufferTransfer(struct InputBuffer *dest, struct InputBuffer *src) void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *rec) { - *buf_get(buf, buf->len++) = *rec; + *in_buf_get(buf, buf->len++) = *rec; +} + +inline size_t str_buf_empty(struct InputString *str) { + return STR_BUF_SIZE - str->len; +} + +inline char *str_buf_get(struct InputString *str, size_t i) { + return (str->start + i) % STR_BUF_SIZE; +} + +void InputStringTransfer(struct InputString *dest, struct InputString *src) +{ + size_t copy_amt = min_size(str_buf_empty(dest), src->len); + + for (size_t i = 0; i < copy_amt; i++) { + *str_buf_get(dest, dest->len + i) = *str_buf_get(src, i); + } } void *input_thread_loop(void *arg) { struct InputThreadHandle *hand = arg; - struct InputBuffer tmp_buf = { .len = 0, .start = 0 }; + + struct InputBuffer tmp_in = { .len = 0, .start = 0 }; + struct InputString tmp_str = { .len = 0, .start = 0 }; while (!hand->is_terminating) { - if (!PlatformReadInput(&tmp_buf)) { + if (!PlatformReadInput(&tmp_in, &tmp_str)) { hand->err = true; return nullptr; } @@ -44,13 +68,14 @@ void *input_thread_loop(void *arg) if (hand->err) return nullptr; - while (tmp_buf.len == IO_BUF_SIZE) { + while (tmp_in.len == IO_BUF_SIZE) { hand->err = pthread_cond_wait(&hand->buf_read, &hand->mutex); if (hand->err) return nullptr; } - InputBufferTransfer(hand->buf, &tmp_buf); + InputBufferTransfer(hand->in, &tmp_in); + InputStringTransfer(hand->str, &tmp_str); hand->err = pthread_mutex_unlock(&hand->mutex); if (hand->err) @@ -62,7 +87,7 @@ void *input_thread_loop(void *arg) bool BeginInputThread(struct InputThreadHandle *hand, struct InputBuffer *buf) { - hand->buf = buf; + hand->in = buf; hand->mutex = PTHREAD_MUTEX_INITIALIZER; hand->buf_read = PTHREAD_COND_INITIALIZER; hand->err = 0; diff --git a/source/io/input.h b/source/io/input.h index 78dda40..07b293a 100644 --- a/source/io/input.h +++ b/source/io/input.h @@ -10,6 +10,7 @@ #include "gametime.h" #define IO_BUF_SIZE 16 +#define STR_BUF_SIZE (IO_BUF_SIZE * 4) enum InputType { BUTTON, @@ -57,22 +58,36 @@ struct InputRecord { union InputData data; }; - u16f bind; + union InputID id; + + struct { + u8f is_down : 1; + u8f is_held : 1; + u8f is_up : 1; + }; +}; - u8 type; - u8 is_down; - u8 is_held; - u8 is_up; +struct input_circ_buf { + u8f len; + u8f start; }; struct InputBuffer { - struct InputRecord recs[IO_BUF_SIZE]; + struct InputRecord buf[IO_BUF_SIZE]; + u8f len; + u8f start; + +}; + +struct InputString { + char buf[STR_BUF_SIZE]; u8f len; u8f start; }; struct InputThreadHandle { - struct InputBuffer *buf; + struct InputBuffer *in; + struct InputString *str; pthread_t thread; pthread_mutex_t mutex; diff --git a/source/io/platforms/platform.h b/source/io/platforms/platform.h index c5fcb49..7a286fc 100644 --- a/source/io/platforms/platform.h +++ b/source/io/platforms/platform.h @@ -16,7 +16,7 @@ bool PlatformInit(); bool PlatformGetRefreshRate(u16f *out); -bool PlatformReadInput(struct InputBuffer *buf); +bool PlatformReadInput(struct InputBuffer *in, struct InputString *str); bool PlatformStopInput(); diff --git a/source/io/platforms/win.c b/source/io/platforms/win.c index 1e644bd..0f6a3cf 100644 --- a/source/io/platforms/win.c +++ b/source/io/platforms/win.c @@ -25,11 +25,11 @@ bool init_handles() bool init_console() { - DWORD mode = ENABLE_EXTENDED_FLAGS - | ENABLE_PROCESSED_INPUT - | ENABLE_PROCESSED_OUTPUT - | ENABLE_MOUSE_INPUT - | ENABLE_WINDOW_INPUT; + DWORD mode = ENABLE_EXTENDED_FLAGS + | ENABLE_PROCESSED_INPUT + | ENABLE_PROCESSED_OUTPUT + | ENABLE_MOUSE_INPUT + | ENABLE_WINDOW_INPUT; return SetConsoleMode(win.input_hand, mode) != 0; } @@ -58,72 +58,82 @@ bool PlatformGetRefreshRate(u16f *out) return true; } -void read_key_event(struct InputRecord *rec, KEY_EVENT_RECORD *win_key) -{ - rec->type = BUTTON; - rec->bind = win_key->wVirtualKeyCode; - - rec->is_down = win_key->bKeyDown; - rec->is_up = !win_key->bKeyDown; +inline size_t ucs2_to_utf8(u16f ucs2, size_t n, char *buf) { + return WideCharToMultiByte(CP_UTF8, 0, ucs2, 1, buf, n, NULL, NULL); } -bool read_mouse_event(struct InputRecord *rec, MOUSE_EVENT_RECORD *win_mouse) -{ - if (win_mouse->dwEventFlags == MOUSE_MOVED) { - rec->type = JOYSTICK; - rec->bind = 0; - rec->js.x = win_mouse->dwMousePosition.X; - rec->js.y = win_mouse->dwMousePosition.Y; - - return true; - } +void read_key( + struct InputBuffer *in, + struct InputString *str, + KEY_EVENT_RECORD *key +) { + struct InputRecord *rec = &in->buf[in->len++]; - if ((win_mouse->dwEventFlags & (MOUSE_WHEELED | MOUSE_HWHEELED)) != 0) { - rec->type = AXIS; - rec->bind = win_mouse->dwEventFlags == MOUSE_WHEELED; - rec->axis.value = win_mouse->dwButtonState; + rec->id.type = BUTTON; + rec->id.bind = key->wVirtualKeyCode; - return true; - } + rec->is_down = key->bKeyDown; + rec->is_up = !key->bKeyDown; - return false; + ucs2_to_utf8(key->uChar.UnicodeChar, IO_BUF_SIZE - str->len, str->buf); } -bool read_rec(struct InputRecord *rec, INPUT_RECORD *win_rec) +bool is_mouse_wheel(DWORD event_flags) { + return (event_flags & (MOUSE_WHEELED | MOUSE_HWHEELED)) != 0; +} + +void read_mouse(struct InputBuffer *in, MOUSE_EVENT_RECORD *mouse) { - switch (win_rec->EventType) { + if (mouse->dwEventFlags == MOUSE_MOVED) { + struct InputRecord *rec = &in->buf[in->len++]; + + rec->id.type = JOYSTICK; + rec->id.bind = 0; + rec->js.x = mouse->dwMousePosition.X; + rec->js.y = mouse->dwMousePosition.Y; + } else if (is_mouse_wheel(mouse->dwEventFlags)) { + struct InputRecord *rec = &in->buf[in->len++]; + + rec->id.type = AXIS; + rec->id.bind = (mouse->dwEventFlags == MOUSE_WHEELED); + rec->axis.value = mouse->dwButtonState; + } +} + +bool read_rec( + struct InputBuffer *in, + struct InputString *str, + INPUT_RECORD *rec +) { + switch (rec->EventType) { case KEY_EVENT: - read_key_event(rec, &win_rec->Event.KeyEvent); + read_key(in, str, &rec->Event.KeyEvent); return true; case MOUSE_EVENT: - return read_mouse_event(rec, &win_rec->Event.MouseEvent); + read_mouse(in, &rec->Event.MouseEvent); + return true; case WINDOW_BUFFER_SIZE_EVENT: - return false; + return true; // TODO: Handle window resizing } - rec->type = ESCAPE; return false; } -bool PlatformReadInput(struct InputBuffer *buf) +bool PlatformReadInput(struct InputBuffer *in, struct InputString *str) { - DWORD max_records = IO_BUF_SIZE - buf->len; + DWORD max_records = IO_BUF_SIZE - in->len; INPUT_RECORD win_buf[max_records]; DWORD filled; if (!ReadConsoleInputW(win.input_hand, win_buf, max_records, &filled)) return false; - struct InputRecord rec = { .time = TimeNow() }; - for (size_t i = 0; i < filled; i++) { - if (!read_rec(&rec, win_buf + i)) - continue; - - InputBufferAdd(buf, &rec); + if (!read_rec(in, str, win_buf + i)) + return false; } return true;