diff --git a/source/io/input.c b/source/io/input.c index d47437e..0816495 100644 --- a/source/io/input.c +++ b/source/io/input.c @@ -25,14 +25,12 @@ void *block_input(void *args_ptr) struct InputBuffer tmp_buf = { .len = 0 }; while (true) { - #ifdef _WIN32 - if (!WindowsReadInputBuffer(&tmp_buf)) + if (!PlatformBlockInput(&tmp_buf)) return false; - #endif 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); } diff --git a/source/io/platforms/win.c b/source/io/platforms/win.c index 0a8ff30..f6262b0 100644 --- a/source/io/platforms/win.c +++ b/source/io/platforms/win.c @@ -8,45 +8,53 @@ #include "fumotris.h" #include "ctrl.h" -#include "term.h" -struct Windows { +static struct Windows { HANDLE in_handle; HANDLE timer; DWORD in_len; INPUT_RECORD in_buf[IO_BUF_SIZE]; -}; +} win; -typedef struct Windows *platform; - -bool WinInitHandles(struct Windows *win) +bool init_handles() { - win->in_handle = GetStdHandle(STD_INPUT_HANDLE); - if (win->in_handle == INVALID_HANDLE_VALUE) + win.in_handle = GetStdHandle(STD_INPUT_HANDLE); + if (win.in_handle == INVALID_HANDLE_VALUE) return false; - win->timer = CreateWaitableTimer( + win.timer = CreateWaitableTimer( NULL, // Timer attributes TRUE, // Manual reset NULL // Name ); - if (win->timer == NULL) + if (win.timer == NULL) return false; return true; } -bool WinInitConsole(struct Windows *win) +bool init_console() { DWORD mode = ENABLE_EXTENDED_FLAGS | ENABLE_PROCESSED_INPUT | ENABLE_PROCESSED_OUTPUT | ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT; - return SetConsoleMode(win->in_handle, mode) != 0; + return SetConsoleMode(win.in_handle, mode) != 0; } -bool WinGetRefreshRate(u16f *out) +bool PlatformInit() +{ + if (!init_handles()) + return false; + + if (!init_console()) + return false; + + return true; +} + +bool PlatformGetRefreshRate(u16f *out) { DEVMODE mode; mode.dmSize = sizeof(DEVMODE); @@ -63,20 +71,6 @@ bool WinGetRefreshRate(u16f *out) return true; } -bool WindowsInit(platform win, struct Terminal *term) -{ - if (!WinInitHandles(win)) - return false; - - if (!WinInitConsole(win)) - return false; - - if (!WinGetRefreshRate(&term->refresh_rate)) - return false; - - return true; -} - void read_key_rec(struct InputRecord *rec, KEY_EVENT_RECORD win_key) { rec->type = BUTTON; @@ -130,23 +124,23 @@ bool read_rec(struct InputRecord *rec, INPUT_RECORD win_rec) return false; } -bool WinBlockInput(platform win, struct InputBuffer *buf) +bool PlatformBlockInput(struct InputBuffer *buf) { if (!ReadConsoleInput( - win->in_handle, // Input handle - win->in_buf + buf->len, // Record buffer + win.in_handle, // Input handle + win.in_buf + buf->len, // Record buffer IO_BUF_SIZE - buf->len, // Record buffer length - &win->in_len // Out number of records + &win.in_len // Out number of records )) return false; struct timespec now; timespec_get(&now, TIME_UTC); - for (size_t i = 0; i < win->in_len; i++) { + for (size_t i = 0; i < win.in_len; i++) { struct InputRecord *rec = &buf->records[buf->len]; - if (!read_rec(rec, win->in_buf[i])) + if (!read_rec(rec, win.in_buf[i])) continue; rec->timestamp = now; @@ -156,13 +150,13 @@ bool WinBlockInput(platform win, struct InputBuffer *buf) return true; } -bool WinWait(platform win, struct timespec relative) +bool PlatformWait(struct timespec relative) { LARGE_INTEGER duration; duration.QuadPart = -10000000 * relative.tv_sec - relative.tv_nsec / 100; if (!SetWaitableTimer( - win->timer, // Timer + win.timer, // Timer &duration, // Duration 0, // Period NULL, // Completion coroutine @@ -171,7 +165,7 @@ bool WinWait(platform win, struct timespec relative) )) return false; - DWORD result = WaitForSingleObject(win->timer, INFINITE); + DWORD result = WaitForSingleObject(win.timer, INFINITE); if (result != WAIT_OBJECT_0) return false; diff --git a/source/io/platforms/win.h b/source/io/platforms/win.h index 8e0015b..b4a6148 100644 --- a/source/io/platforms/win.h +++ b/source/io/platforms/win.h @@ -7,12 +7,11 @@ #include #include "fumotris.h" -#include "term.h" -typedef struct Windows *platform; +bool PlatformInit(); -bool WindowsInit(platform win, struct Terminal *term); +bool PlatformGetRefreshRate(u16f *out); -bool WinBlockInput(platform win, struct InputBuffer *buf); +bool PlatformBlockInput(struct InputBuffer *buf); -bool WinWait(platform win, struct timespec relative); \ No newline at end of file +bool PlatformWait(struct timespec relative); \ No newline at end of file diff --git a/source/main.c b/source/main.c index 8113cd0..673583b 100644 --- a/source/main.c +++ b/source/main.c @@ -6,7 +6,7 @@ #include #include -#include "control.h" +#include "ctrl.h" #include "fumotris.h" #include "term.h" #include "tetr.h" @@ -96,7 +96,7 @@ void *Update(void *args) while (true) { // Input CtrlPoll(&game->ctrl); - if (CtrlGet(&game->ctrl, LEFT, BUTTON)->button.is_down) + if (CtrlGet(&game->ctrl, LEFT, BUTTON)->is_down) printf("left down this frame\n"); // Game logic @@ -143,10 +143,8 @@ int main() if (!Start(&game)) exit(1); - #ifdef _WIN32 - if(!WindowsInit(&game.term)) + if(!PlatformInit(&game.term)) exit(1); - #endif StartInput(&game.ctrl); Loop(&game);