ooga booga

kjlkljh
This commit is contained in:
Julia 2024-04-15 14:29:51 -05:00
parent 3767bfc036
commit f64b77ac21
4 changed files with 40 additions and 51 deletions

View file

@ -25,14 +25,12 @@ void *block_input(void *args_ptr)
struct InputBuffer tmp_buf = { .len = 0 }; struct InputBuffer tmp_buf = { .len = 0 };
while (true) { while (true) {
#ifdef _WIN32 if (!PlatformBlockInput(&tmp_buf))
if (!WindowsReadInputBuffer(&tmp_buf))
return false; return false;
#endif
pthread_mutex_lock(&in->access_mutex); 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); pthread_mutex_unlock(&in->access_mutex);
} }

View file

@ -8,45 +8,53 @@
#include "fumotris.h" #include "fumotris.h"
#include "ctrl.h" #include "ctrl.h"
#include "term.h"
struct Windows { static struct Windows {
HANDLE in_handle; HANDLE in_handle;
HANDLE timer; HANDLE timer;
DWORD in_len; DWORD in_len;
INPUT_RECORD in_buf[IO_BUF_SIZE]; INPUT_RECORD in_buf[IO_BUF_SIZE];
}; } win;
typedef struct Windows *platform; bool init_handles()
bool WinInitHandles(struct Windows *win)
{ {
win->in_handle = GetStdHandle(STD_INPUT_HANDLE); win.in_handle = GetStdHandle(STD_INPUT_HANDLE);
if (win->in_handle == INVALID_HANDLE_VALUE) if (win.in_handle == INVALID_HANDLE_VALUE)
return false; return false;
win->timer = CreateWaitableTimer( win.timer = CreateWaitableTimer(
NULL, // Timer attributes NULL, // Timer attributes
TRUE, // Manual reset TRUE, // Manual reset
NULL // Name NULL // Name
); );
if (win->timer == NULL) if (win.timer == NULL)
return false; return false;
return true; return true;
} }
bool WinInitConsole(struct Windows *win) bool init_console()
{ {
DWORD mode = ENABLE_EXTENDED_FLAGS DWORD mode = ENABLE_EXTENDED_FLAGS
| ENABLE_PROCESSED_INPUT | ENABLE_PROCESSED_INPUT
| ENABLE_PROCESSED_OUTPUT | ENABLE_PROCESSED_OUTPUT
| ENABLE_MOUSE_INPUT | ENABLE_MOUSE_INPUT
| ENABLE_WINDOW_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; DEVMODE mode;
mode.dmSize = sizeof(DEVMODE); mode.dmSize = sizeof(DEVMODE);
@ -63,20 +71,6 @@ bool WinGetRefreshRate(u16f *out)
return true; 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) void read_key_rec(struct InputRecord *rec, KEY_EVENT_RECORD win_key)
{ {
rec->type = BUTTON; rec->type = BUTTON;
@ -130,23 +124,23 @@ bool read_rec(struct InputRecord *rec, INPUT_RECORD win_rec)
return false; return false;
} }
bool WinBlockInput(platform win, struct InputBuffer *buf) bool PlatformBlockInput(struct InputBuffer *buf)
{ {
if (!ReadConsoleInput( if (!ReadConsoleInput(
win->in_handle, // Input handle win.in_handle, // Input handle
win->in_buf + buf->len, // Record buffer win.in_buf + buf->len, // Record buffer
IO_BUF_SIZE - buf->len, // Record buffer length IO_BUF_SIZE - buf->len, // Record buffer length
&win->in_len // Out number of records &win.in_len // Out number of records
)) ))
return false; return false;
struct timespec now; struct timespec now;
timespec_get(&now, TIME_UTC); 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]; struct InputRecord *rec = &buf->records[buf->len];
if (!read_rec(rec, win->in_buf[i])) if (!read_rec(rec, win.in_buf[i]))
continue; continue;
rec->timestamp = now; rec->timestamp = now;
@ -156,13 +150,13 @@ bool WinBlockInput(platform win, struct InputBuffer *buf)
return true; return true;
} }
bool WinWait(platform win, struct timespec relative) bool PlatformWait(struct timespec relative)
{ {
LARGE_INTEGER duration; LARGE_INTEGER duration;
duration.QuadPart = -10000000 * relative.tv_sec - relative.tv_nsec / 100; duration.QuadPart = -10000000 * relative.tv_sec - relative.tv_nsec / 100;
if (!SetWaitableTimer( if (!SetWaitableTimer(
win->timer, // Timer win.timer, // Timer
&duration, // Duration &duration, // Duration
0, // Period 0, // Period
NULL, // Completion coroutine NULL, // Completion coroutine
@ -171,7 +165,7 @@ bool WinWait(platform win, struct timespec relative)
)) ))
return false; return false;
DWORD result = WaitForSingleObject(win->timer, INFINITE); DWORD result = WaitForSingleObject(win.timer, INFINITE);
if (result != WAIT_OBJECT_0) if (result != WAIT_OBJECT_0)
return false; return false;

View file

@ -7,12 +7,11 @@
#include <time.h> #include <time.h>
#include "fumotris.h" #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); bool PlatformWait(struct timespec relative);

View file

@ -6,7 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "control.h" #include "ctrl.h"
#include "fumotris.h" #include "fumotris.h"
#include "term.h" #include "term.h"
#include "tetr.h" #include "tetr.h"
@ -96,7 +96,7 @@ void *Update(void *args)
while (true) { while (true) {
// Input // Input
CtrlPoll(&game->ctrl); 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"); printf("left down this frame\n");
// Game logic // Game logic
@ -143,10 +143,8 @@ int main()
if (!Start(&game)) if (!Start(&game))
exit(1); exit(1);
#ifdef _WIN32 if(!PlatformInit(&game.term))
if(!WindowsInit(&game.term))
exit(1); exit(1);
#endif
StartInput(&game.ctrl); StartInput(&game.ctrl);
Loop(&game); Loop(&game);