diff --git a/run.py b/build.py similarity index 100% rename from run.py rename to build.py diff --git a/source/fumotris.h b/source/fumotris.h index fd37aa2..17ae408 100644 --- a/source/fumotris.h +++ b/source/fumotris.h @@ -28,7 +28,7 @@ typedef int_fast32_t i32f; typedef int64_t i64; typedef int_fast64_t i64f; -const u8 I[16] = { +/*const u8 I[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, @@ -68,4 +68,4 @@ const u8 L[9] = { 0, 0, 1, 1, 1, 1, 0, 0, 0 -}; \ No newline at end of file +};*/ \ No newline at end of file diff --git a/source/io/ctrl.c b/source/io/ctrl.c index 8467bad..7e25593 100644 --- a/source/io/ctrl.c +++ b/source/io/ctrl.c @@ -1,10 +1,12 @@ #include "ctrl.h" -bool NewCtrl(struct Controller *ctrl, size_t init_axes) +#define INIT_SIZE 16 + +bool CreateCtrl(struct Controller *ctrl) { - struct ctrl_bkt *code_bkts = calloc(init_axes, sizeof(struct ctrl_bkt)); - struct ctrl_bkt *bind_bkts = calloc(init_axes, sizeof(struct ctrl_bkt)); - struct InputAxis *axes = calloc(init_axes, sizeof(struct InputAxis)); + struct ctrl_bkt *code_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt)); + struct ctrl_bkt *bind_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt)); + struct InputAxis *axes = calloc(INIT_SIZE, sizeof(struct InputAxis)); if (code_bkts == nullptr or bind_bkts == nullptr or axes == nullptr) return false; @@ -13,15 +15,19 @@ bool NewCtrl(struct Controller *ctrl, size_t init_axes) .buf.len = 0, .pending_buf.len = 0, - .axis_vec.axes = axes, + .axis_vec = (struct ctrl_axis_vec) { + .axes = axes, + .size = INIT_SIZE, + .len = 0, + }, .codes = (struct ctrl_dict) { .bkts = code_bkts, - .capacity = init_axes, + .capacity = INIT_SIZE, .filled = 0, }, .binds = (struct ctrl_dict) { .bkts = bind_bkts, - .capacity = init_axes, + .capacity = INIT_SIZE, .filled = 0, }, }; @@ -35,22 +41,20 @@ void FreeCtrl(struct Controller *ctrl) free(ctrl->axis_vec.axes); } -struct ctrl_bkt *get_bkt(struct ctrl_dict *dict, size_t i) -{ +struct ctrl_bkt *get_bkt(struct ctrl_dict *dict, size_t i) { return &dict->bkts[i]; } -size_t wrap_index(size_t i, size_t max) -{ +size_t wrap_index(size_t i, size_t max) { return i % (SIZE_MAX - max + 1); } struct ctrl_bkt *find_or_set(struct ctrl_dict *dict, union InputID id) { - const size_t index = id.hash % dict->capacity; + size_t i = id.hash % dict->capacity; - size_t i = index; - while (i != wrap_index(index - 1, dict->capacity)) { + size_t last = wrap_index(i - 1, dict->capacity); + while (i != last) { struct ctrl_bkt *bkt = get_bkt(dict, i); if (bkt->axis == nullptr) { @@ -60,9 +64,8 @@ struct ctrl_bkt *find_or_set(struct ctrl_dict *dict, union InputID id) return bkt; } - if (bkt->id.hash == id.hash) { + if (bkt->id.hash == id.hash) return bkt; - } i = (i + 1) % dict->capacity; } @@ -72,10 +75,10 @@ struct ctrl_bkt *find_or_set(struct ctrl_dict *dict, union InputID id) struct ctrl_bkt *find(struct ctrl_dict *dict, union InputID id) { - const size_t index = id.hash % dict->capacity; + size_t i = id.hash % dict->capacity; - size_t i = index; - while (i != wrap_index(index - 1, dict->capacity)) { + size_t last = wrap_index(i - 1, dict->capacity); + while (i != last) { struct ctrl_bkt *bkt = get_bkt(dict, i); if (bkt->id.hash == id.hash) @@ -96,8 +99,7 @@ struct InputAxis *find_axis(struct ctrl_dict *dict, union InputID id) return bkt->axis; } -union InputID to_id(u16f value, u16f type) -{ +union InputID to_id(u16f value, u16f type) { return (union InputID) { .value = value, .type = type }; } @@ -167,10 +169,10 @@ bool CtrlPoll(struct Controller *ctrl) return true; } -int main() +/*int main() { struct Controller ctrl; - if (!NewCtrl(&ctrl, 3)) + if (!CreateCtrl(&ctrl)) return 1; CtrlMap(&ctrl, 123, BUTTON, 111); @@ -199,4 +201,4 @@ int main() printf("success"); return 0; -} \ No newline at end of file +}*/ \ No newline at end of file diff --git a/source/io/ctrl.h b/source/io/ctrl.h index 375a70f..ce82582 100644 --- a/source/io/ctrl.h +++ b/source/io/ctrl.h @@ -56,7 +56,7 @@ struct Controller { struct ctrl_dict binds; }; -bool NewCtrl(struct Controller *ctrl, size_t init_axes); +bool CreateCtrl(struct Controller *ctrl); void FreeCtrl(struct Controller *ctrl); @@ -87,8 +87,8 @@ struct ControlBind { u8 type; }; -const size_t code_count = 12; -const struct ControlBind ctrl_binds[12] = { +#define CODE_COUNT 12 +/*const struct ControlBind ctrl_binds[12] = { { LEFT, 0x25, BUTTON }, { RIGHT, 0x27, BUTTON }, { SOFT_DROP, 0x28, BUTTON }, @@ -101,4 +101,4 @@ const struct ControlBind ctrl_binds[12] = { { VSCROLL, 0, AXIS }, { HSCROLL, 1, AXIS }, { MOUSE, 0, JOYSTICK } -}; \ No newline at end of file +};*/ \ No newline at end of file diff --git a/source/io/input.c b/source/io/input.c index e75d2e3..73cc854 100644 --- a/source/io/input.c +++ b/source/io/input.c @@ -1,5 +1,4 @@ #include "input.h" -#include #include "platform.h" @@ -30,41 +29,46 @@ void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *rec) *buf_get(buf, buf->len++) = *rec; } -struct input_args { - struct InputBuffer *buf; - pthread_mutex_t *mutex; -}; - -void *input_thread_loop(struct input_args *args) +void *input_thread_loop(struct InputThreadHandle *hand) { - struct InputBuffer *buf = args->buf; - pthread_mutex_t *mutex = args->mutex; - free(args); - struct InputBuffer tmp_buf = { .len = 0, .start = 0 }; - while (true) { - if (!PlatformReadInput(&tmp_buf)) - return false; - - pthread_mutex_lock(mutex); - { - InputBufferTransfer(&tmp_buf, buf); + while (!hand->is_terminating) { + if (!PlatformReadInput(&tmp_buf)) { + hand->err = true; + return; } - pthread_mutex_unlock(mutex); - } - return nullptr; + hand->err = pthread_mutex_lock(&hand->mutex); + if (hand->err) + return; + + InputBufferTransfer(&tmp_buf, hand->buf); + + hand->err = pthread_mutex_unlock(&hand->mutex); + if (hand->err) + return; + } } -bool CreateInputThread(struct InputBuffer *buf, pthread_mutex_t *mutex) +bool BeginInputThread(struct InputThreadHandle *hand, struct InputBuffer *buf) { - struct input_args *args = malloc(sizeof(struct input_args)); - *args = (struct input_args) { - .buf = buf, - .mutex = mutex, - }; + hand->mutex = PTHREAD_MUTEX_INITIALIZER; + return pthread_create(&hand->thread, nullptr, input_thread_loop, hand) == 0; +} - pthread_t thread; - return pthread_create(&thread, nullptr, input_thread_loop, args) == 0; +bool EndInputThread(struct InputThreadHandle *hand) +{ + hand->is_terminating = true; + + if (!PlatformStopInput()) + return false; + + if (!pthread_mutex_destroy(&hand->mutex)) + return false; + + if (!pthread_join(hand->thread, nullptr)) + return false; + + return true; } \ No newline at end of file diff --git a/source/io/input.h b/source/io/input.h index 61b80ca..48d40d2 100644 --- a/source/io/input.h +++ b/source/io/input.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "fumotris.h" #include "gametime.h" @@ -19,7 +20,11 @@ enum InputType { union InputID { struct { - union { u16 code; u16 bind; u16 value }; + union { + u16 code; + u16 bind; + u16 value; + }; u16 type; }; u32 hash; @@ -66,8 +71,20 @@ struct InputBuffer { u8f start; }; +struct InputThreadHandle { + struct InputBuffer *buf; + + pthread_t thread; + pthread_mutex_t mutex; + + int err; + bool is_terminating; +}; + void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest); void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *src); -bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex); \ No newline at end of file +bool BeginInputThread(struct InputThreadHandle *hand, struct InputBuffer *buf); + +bool EndInputThread(struct InputThreadHandle *hand); \ No newline at end of file diff --git a/source/io/platforms/platform.h b/source/io/platforms/platform.h index 23750e7..ec995b9 100644 --- a/source/io/platforms/platform.h +++ b/source/io/platforms/platform.h @@ -12,19 +12,12 @@ #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 PlatformStopInput(); + 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 a736fd6..e683185 100644 --- a/source/io/platforms/win.c +++ b/source/io/platforms/win.c @@ -5,21 +5,31 @@ #include "gametime.h" #include "input.h" -static struct windows { +struct windows { + union { + HANDLE input_hands[2]; + struct { + HANDLE input_hand; + HANDLE early_exit_hand; + }; + }; HANDLE timer; - HANDLE input_handle; } win; bool init_handles() { - win.input_handle = GetStdHandle(STD_INPUT_HANDLE); - if (win.input_handle == INVALID_HANDLE_VALUE) + win.input_hand = GetStdHandle(STD_INPUT_HANDLE); + if (win.input_hand == INVALID_HANDLE_VALUE) return false; win.timer = CreateWaitableTimerW(NULL, TRUE, NULL); if (win.timer == NULL) return false; + win.early_exit_hand = CreateEventW(NULL, FALSE, FALSE, NULL); + if (win.early_exit_hand == NULL) + return false; + return true; } @@ -31,7 +41,7 @@ bool init_console() | ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT; - return SetConsoleMode(win.input_handle, mode) != 0; + return SetConsoleMode(win.input_hand, mode) != 0; } bool PlatformInit() @@ -47,11 +57,11 @@ bool PlatformInit() bool PlatformGetRefreshRate(u16f *out) { - DEVMODE mode; - mode.dmSize = sizeof(DEVMODE); + DEVMODEW mode; + mode.dmSize = sizeof(DEVMODEW); mode.dmDriverExtra = 0; - if(!EnumDisplaySettingsA(NULL, ENUM_CURRENT_SETTINGS, &mode)) + if(!EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &mode)) return false; *out = mode.dmDisplayFrequency; @@ -110,11 +120,16 @@ bool copy_rec(struct InputRecord *rec, INPUT_RECORD *win_rec) bool PlatformReadInput(struct InputBuffer *buf) { + DWORD wait_status = WaitForMultipleObjects(2, win.input_hands, FALSE, 0); + + if (wait_status != 0) + return wait_status == 1; + DWORD max_records = IO_BUF_SIZE - buf->len; INPUT_RECORD win_buf[max_records]; - DWORD filled; - if (!ReadConsoleInputW(win.input_handle, win_buf, max_records, &filled)) + + if (!ReadConsoleInputW(win.input_hand, win_buf, max_records, &filled)) return false; struct InputRecord rec = { .time = TimeNow() }; @@ -129,12 +144,17 @@ bool PlatformReadInput(struct InputBuffer *buf) return true; } +bool PlatformStopInput() +{ + return SetEvent(win.early_exit_hand) == 0; +} + bool PlatformWait(struct Time relative) { LARGE_INTEGER duration; duration.QuadPart = -10000000 * relative.sec - relative.nsec / 100; - if (!SetWaitableTimer(win.timer, &duration, 0, NULL,NULL, FALSE)) + if (!SetWaitableTimer(win.timer, &duration, 0, NULL, NULL, FALSE)) return false; DWORD result = WaitForSingleObject(win.timer, INFINITE); diff --git a/source/main.c b/source/main.c index 7208218..ed8ea7a 100644 --- a/source/main.c +++ b/source/main.c @@ -14,17 +14,26 @@ #include "event.h" #include "platform.h" +void ErrorExit(char *message) +{ + printf(message); + exit(1); +} + int main() { - struct Instance game; - if (!Start(&game)) - exit(1); + if (!PlatformInit()) + ErrorExit("Platform failed to initialize"); - if(!PlatformInit()) - exit(1); + struct Controller ctrl; + if (!CreateCtrl(&ctrl)) + ErrorExit("Out of memory"); - CreateInputThread(&game.ctrl.buf, ); - Loop(&game); + struct InputThreadHandle input; + if (!BeginInputThread(&input, &ctrl.buf)) + ErrorExit("Input handle failed to initialize"); + + printf("woah"); return 0; } \ No newline at end of file diff --git a/test.exe b/test.exe index 3e4986b..78e8ad2 100644 Binary files a/test.exe and b/test.exe differ