From acfb0067be69203ab75d9bbfa4eec571cbd2856d Mon Sep 17 00:00:00 2001 From: Julia <145168563+julia-aph@users.noreply.github.com> Date: Fri, 19 Apr 2024 01:38:45 -0500 Subject: [PATCH] errors n all that i hate muflrgf --- .vscode/settings.json | 5 +- source/fumotris.h | 53 ++++++++++---- source/io/ctrl.c | 20 ------ source/io/ctrl.h | 51 +++++++++++--- source/io/input.c | 45 ++++++------ source/io/input.h | 3 +- source/io/platforms/platform.h | 30 +++++++- source/io/platforms/win.c | 18 ++--- source/io/platforms/win.h | 11 +-- source/main.c | 123 +-------------------------------- 10 files changed, 149 insertions(+), 210 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index f3124c4..dddf55c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -51,6 +51,9 @@ "streambuf": "cpp", "typeinfo": "cpp", "execution": "cpp", - "stdbool.h": "c" + "stdbool.h": "c", + "gametime.h": "c", + "stdlib.h": "c", + "stdio.h": "c" } } \ No newline at end of file diff --git a/source/fumotris.h b/source/fumotris.h index f9338b1..fd37aa2 100644 --- a/source/fumotris.h +++ b/source/fumotris.h @@ -28,17 +28,44 @@ typedef int_fast32_t i32f; typedef int64_t i64; typedef int_fast64_t i64f; -enum CtrlCode { - LEFT, - RIGHT, - SOFT_DROP, - HARD_DROP, - ROTATE_CCW, - ROTATE_CW, - ROTATE_180, - SWAP, - ESC, - VSCROLL, - HSCROLL, - MOUSE +const u8 I[16] = { + 0, 0, 0, 0, + 0, 0, 0, 0, + 1, 1, 1, 1, + 0, 0, 0, 0 +}; + +const u8 O[4] = { + 1, 1, + 1, 1 +}; + +const u8 T[9] = { + 0, 1, 0, + 1, 1, 1, + 0, 0, 0 +}; + +const u8 S[9] = { + 0, 1, 1, + 1, 1, 0, + 0, 0, 0 +}; + +const u8 Z[9] = { + 1, 1, 0, + 0, 1, 1, + 0, 0, 0 +}; + +const u8 J[9] = { + 1, 0, 0, + 1, 1, 1, + 0, 0, 0 +}; + +const u8 L[9] = { + 0, 0, 1, + 1, 1, 1, + 0, 0, 0 }; \ No newline at end of file diff --git a/source/io/ctrl.c b/source/io/ctrl.c index 142eaca..8467bad 100644 --- a/source/io/ctrl.c +++ b/source/io/ctrl.c @@ -1,25 +1,5 @@ #include "ctrl.h" -struct ctrl_bkt { - union InputID id; - struct InputAxis *axis; -}; - -struct ctrl_dict { - struct ctrl_bkt *bkts; - - u16f capacity; - u16f filled; -}; - -struct axis_vec { - struct InputAxis *axes; - u16f size; - u16f len; -}; - -size_t a = sizeof(struct ctrl_dict); - bool NewCtrl(struct Controller *ctrl, size_t init_axes) { struct ctrl_bkt *code_bkts = calloc(init_axes, sizeof(struct ctrl_bkt)); diff --git a/source/io/ctrl.h b/source/io/ctrl.h index 1661bfc..375a70f 100644 --- a/source/io/ctrl.h +++ b/source/io/ctrl.h @@ -27,21 +27,19 @@ struct InputAxis { u8 is_up; }; -static struct ctrl_bkt { - union InputID id; +struct ctrl_bkt { struct InputAxis *axis; + union InputID id; }; -static struct ctrl_dict { +struct ctrl_dict { struct ctrl_bkt *bkts; - u16f capacity; u16f filled; }; -static struct axis_vec { +struct ctrl_axis_vec { struct InputAxis *axes; - u16f size; u16f len; }; @@ -53,7 +51,7 @@ struct Controller { u16f len; } pending_buf; - struct axis_vec axis_vec; + struct ctrl_axis_vec axis_vec; struct ctrl_dict codes; struct ctrl_dict binds; }; @@ -66,4 +64,41 @@ bool CtrlMap(struct Controller *ctrl, u16f code, u16f type, u16f bind); struct InputAxis *CtrlGet(struct Controller *ctrl, u16f code, u16f type); -bool CtrlPoll(struct Controller *ctrl); \ No newline at end of file +bool CtrlPoll(struct Controller *ctrl); + +enum ControlCode { + LEFT, + RIGHT, + SOFT_DROP, + HARD_DROP, + ROTATE_CCW, + ROTATE_CW, + ROTATE_180, + SWAP, + ESC, + VSCROLL, + HSCROLL, + MOUSE +}; + +struct ControlBind { + enum ControlCode code; + u16 bind; + u8 type; +}; + +const size_t code_count = 12; +const struct ControlBind ctrl_binds[12] = { + { LEFT, 0x25, BUTTON }, + { RIGHT, 0x27, BUTTON }, + { SOFT_DROP, 0x28, BUTTON }, + { HARD_DROP, 0x20, BUTTON }, + { ROTATE_CCW, 'Z', BUTTON }, + { ROTATE_CW, 'X', BUTTON }, + { ROTATE_180, 'A', BUTTON }, + { SWAP, 'C', BUTTON }, + { ESC, 0x1B, BUTTON }, + { VSCROLL, 0, AXIS }, + { HSCROLL, 1, AXIS }, + { MOUSE, 0, JOYSTICK } +}; \ No newline at end of file diff --git a/source/io/input.c b/source/io/input.c index a5dd882..e75d2e3 100644 --- a/source/io/input.c +++ b/source/io/input.c @@ -3,30 +3,31 @@ #include "platform.h" -struct InputRecord *in_buf_get(struct InputBuffer *buf, size_t i) -{ - return buf->recs + (buf->start + 1) % IO_BUF_SIZE; +struct InputRecord *buf_get(struct InputBuffer *buf, size_t i) { + return buf->recs + (buf->start + i) % IO_BUF_SIZE; } -void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest) -{ - size_t n = IO_BUF_SIZE - (tmp->len > dest->len ? tmp->len : dest->len); - - for (size_t i = 0; i < n; i++) { - *in_buf_get(dest, dest->len + i) = *in_buf_get(tmp, i); - } - - if (n < tmp->len) - tmp->start += n; - - tmp->len -= n; - dest->len += n; +size_t max_size(size_t a, size_t b) { + return a > b ? a : b; } -void InputBufferCopy(struct InputBuffer *buf, struct InputRecord *src) +void InputBufferTransfer(struct InputBuffer *dest, struct InputBuffer *src) { - buf->recs[(buf->start + buf->len) % IO_BUF_SIZE] = *src; - buf->len += 1; + size_t copy_amt = IO_BUF_SIZE - max_size(dest->len, src->len); + + for (size_t i = 0; i < copy_amt; i++) + *buf_get(dest, dest->len + i) = *buf_get(src, i); + + if (copy_amt < src->len) + src->start += copy_amt; + + src->len -= copy_amt; + dest->len += copy_amt; +} + +void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *rec) +{ + *buf_get(buf, buf->len++) = *rec; } struct input_args { @@ -34,7 +35,7 @@ struct input_args { pthread_mutex_t *mutex; }; -void *block_input(struct input_args *args) +void *input_thread_loop(struct input_args *args) { struct InputBuffer *buf = args->buf; pthread_mutex_t *mutex = args->mutex; @@ -56,7 +57,7 @@ void *block_input(struct input_args *args) return nullptr; } -bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex) +bool CreateInputThread(struct InputBuffer *buf, pthread_mutex_t *mutex) { struct input_args *args = malloc(sizeof(struct input_args)); *args = (struct input_args) { @@ -65,5 +66,5 @@ bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex) }; pthread_t thread; - return pthread_create(&thread, nullptr, block_input, args) == 0; + return pthread_create(&thread, nullptr, input_thread_loop, args) == 0; } \ No newline at end of file diff --git a/source/io/input.h b/source/io/input.h index cc6c417..61b80ca 100644 --- a/source/io/input.h +++ b/source/io/input.h @@ -35,6 +35,7 @@ struct Joystick { i32 x; i32 y; }; + union InputData { struct Button input_but; struct Axis input_axis; @@ -67,6 +68,6 @@ struct InputBuffer { void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest); -void InputBufferCopy(struct InputBuffer *buf, struct InputRecord *src); +void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *src); bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex); \ No newline at end of file diff --git a/source/io/platforms/platform.h b/source/io/platforms/platform.h index f93ef27..23750e7 100644 --- a/source/io/platforms/platform.h +++ b/source/io/platforms/platform.h @@ -1,4 +1,30 @@ #pragma once +#include +#include +#include +#include +#include + +#include "fumotris.h" +#include "gametime.h" + #ifdef _WIN32 -#include "win.h" -#endif \ No newline at end of file + #include "win.h" +#endif + +enum PlatformError { + PLTF_E_INITFAIL, +}; + +struct Error { + int e; + +}; + +bool PlatformInit(); + +bool PlatformGetRefreshRate(u16f *out); + +bool PlatformReadInput(struct InputBuffer *buf); + +bool PlatformWait(struct Time relative); \ No newline at end of file diff --git a/source/io/platforms/win.c b/source/io/platforms/win.c index 7d3bd5f..a736fd6 100644 --- a/source/io/platforms/win.c +++ b/source/io/platforms/win.c @@ -1,13 +1,9 @@ -#include -#include -#include -#include -#include +#include "win.h" + #include -#include "fumotris.h" -#include "ctrl.h" #include "gametime.h" +#include "input.h" static struct windows { HANDLE timer; @@ -121,22 +117,22 @@ bool PlatformReadInput(struct InputBuffer *buf) if (!ReadConsoleInputW(win.input_handle, win_buf, max_records, &filled)) return false; - struct InputRecord rec = { .timestamp = TimeNow() }; + struct InputRecord rec = { .time = TimeNow() }; for (size_t i = 0; i < filled; i++) { if (!copy_rec(&rec, win_buf + i)) continue; - InputBufferCopy(buf, &rec); + InputBufferAdd(buf, &rec); } return true; } -bool PlatformWait(struct timespec relative) +bool PlatformWait(struct Time relative) { LARGE_INTEGER duration; - duration.QuadPart = -10000000 * relative.tv_sec - relative.tv_nsec / 100; + duration.QuadPart = -10000000 * relative.sec - relative.nsec / 100; if (!SetWaitableTimer(win.timer, &duration, 0, NULL,NULL, FALSE)) return false; diff --git a/source/io/platforms/win.h b/source/io/platforms/win.h index 85639dc..f8e9eb7 100644 --- a/source/io/platforms/win.h +++ b/source/io/platforms/win.h @@ -4,14 +4,5 @@ #include #include #include -#include -#include "fumotris.h" - -bool PlatformInit(); - -bool PlatformGetRefreshRate(u16f *out); - -bool PlatformReadInput(struct InputBuffer *buf); - -bool PlatformWait(struct timespec relative); \ No newline at end of file +#include "fumotris.h" \ No newline at end of file diff --git a/source/main.c b/source/main.c index 0a0308d..7208218 100644 --- a/source/main.c +++ b/source/main.c @@ -14,127 +14,6 @@ #include "event.h" #include "platform.h" -struct Instance { - struct Controller ctrl; - struct Terminal term; - - struct Delegate on_start; - struct Delegate on_update; - struct Delegate on_draw; -}; - -const u8 I[16] = { - 0, 0, 0, 0, - 0, 0, 0, 0, - 1, 1, 1, 1, - 0, 0, 0, 0 -}; - -const u8 O[4] = { - 1, 1, - 1, 1 -}; - -const u8 T[9] = { - 0, 1, 0, - 1, 1, 1, - 0, 0, 0 -}; - -const u8 S[9] = { - 0, 1, 1, - 1, 1, 0, - 0, 0, 0 -}; - -const u8 Z[9] = { - 1, 1, 0, - 0, 1, 1, - 0, 0, 0 -}; - -const u8 J[9] = { - 1, 0, 0, - 1, 1, 1, - 0, 0, 0 -}; - -const u8 L[9] = { - 0, 0, 1, - 1, 1, 1, - 0, 0, 0 -}; - -struct CtrlBind { - enum CtrlCode code; - u16 bind; - u8 type; -}; - -const size_t code_count = 12; -const struct CtrlBind ctrl_binds[12] = { - { LEFT, 0x25, BUTTON }, - { RIGHT, 0x27, BUTTON }, - { SOFT_DROP, 0x28, BUTTON }, - { HARD_DROP, 0x20, BUTTON }, - { ROTATE_CCW, 'Z', BUTTON }, - { ROTATE_CW, 'X', BUTTON }, - { ROTATE_180, 'A', BUTTON }, - { SWAP, 'C', BUTTON }, - { ESC, 0x1B, BUTTON }, - { VSCROLL, 0, AXIS }, - { HSCROLL, 1, AXIS }, - { MOUSE, 0, JOYSTICK } -}; - -void *Update(void *args) -{ - struct Instance *game = args; - - while (true) { - // Input - CtrlPoll(&game->ctrl); - if (CtrlGet(&game->ctrl, LEFT, BUTTON)->is_down) - printf("left down this frame\n"); - - // Game logic - //Invoke(&game->on_update, game); - - // Draw - //TermOut(&game->term); - //puts(game->term.buf); - } -} - -void Loop(struct Instance *game) -{ - pthread_t update_thread; - pthread_create(&update_thread, nullptr, Update, (void *)game); -} - -bool Start(struct Instance *game) -{ - if (!NewCtrl(&game->ctrl)) - return false; - - for (size_t i = 0; i < code_count; i++) { - const struct CtrlBind *bind = &ctrl_binds[i]; - CtrlMap(&game->ctrl, bind->code, bind->bind, bind->type); - } - - if (!NewTerm(&game->term, 20, 20)) - return false; - - if (!NewDelegate(&game->on_start, 16)) - return false; - if (!NewDelegate(&game->on_update, 16)) - return false; - if (!NewDelegate(&game->on_draw, 16)) - return false; - - return true; -} - int main() { struct Instance game; @@ -144,7 +23,7 @@ int main() if(!PlatformInit()) exit(1); - InputStart(&game.ctrl.buf, ); + CreateInputThread(&game.ctrl.buf, ); Loop(&game); return 0;