cleaning stuff, almost working

woah
This commit is contained in:
Julia 2024-04-19 15:23:11 -05:00
parent acfb0067be
commit a94a0949a7
10 changed files with 133 additions and 88 deletions

View file

View file

@ -28,7 +28,7 @@ typedef int_fast32_t i32f;
typedef int64_t i64; typedef int64_t i64;
typedef int_fast64_t i64f; typedef int_fast64_t i64f;
const u8 I[16] = { /*const u8 I[16] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
@ -68,4 +68,4 @@ const u8 L[9] = {
0, 0, 1, 0, 0, 1,
1, 1, 1, 1, 1, 1,
0, 0, 0 0, 0, 0
}; };*/

View file

@ -1,10 +1,12 @@
#include "ctrl.h" #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 *code_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt));
struct ctrl_bkt *bind_bkts = calloc(init_axes, sizeof(struct ctrl_bkt)); struct ctrl_bkt *bind_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt));
struct InputAxis *axes = calloc(init_axes, sizeof(struct InputAxis)); struct InputAxis *axes = calloc(INIT_SIZE, sizeof(struct InputAxis));
if (code_bkts == nullptr or bind_bkts == nullptr or axes == nullptr) if (code_bkts == nullptr or bind_bkts == nullptr or axes == nullptr)
return false; return false;
@ -13,15 +15,19 @@ bool NewCtrl(struct Controller *ctrl, size_t init_axes)
.buf.len = 0, .buf.len = 0,
.pending_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) { .codes = (struct ctrl_dict) {
.bkts = code_bkts, .bkts = code_bkts,
.capacity = init_axes, .capacity = INIT_SIZE,
.filled = 0, .filled = 0,
}, },
.binds = (struct ctrl_dict) { .binds = (struct ctrl_dict) {
.bkts = bind_bkts, .bkts = bind_bkts,
.capacity = init_axes, .capacity = INIT_SIZE,
.filled = 0, .filled = 0,
}, },
}; };
@ -35,22 +41,20 @@ void FreeCtrl(struct Controller *ctrl)
free(ctrl->axis_vec.axes); 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]; 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); return i % (SIZE_MAX - max + 1);
} }
struct ctrl_bkt *find_or_set(struct ctrl_dict *dict, union InputID id) 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; size_t last = wrap_index(i - 1, dict->capacity);
while (i != wrap_index(index - 1, dict->capacity)) { while (i != last) {
struct ctrl_bkt *bkt = get_bkt(dict, i); struct ctrl_bkt *bkt = get_bkt(dict, i);
if (bkt->axis == nullptr) { if (bkt->axis == nullptr) {
@ -60,9 +64,8 @@ struct ctrl_bkt *find_or_set(struct ctrl_dict *dict, union InputID id)
return bkt; return bkt;
} }
if (bkt->id.hash == id.hash) { if (bkt->id.hash == id.hash)
return bkt; return bkt;
}
i = (i + 1) % dict->capacity; 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) 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; size_t last = wrap_index(i - 1, dict->capacity);
while (i != wrap_index(index - 1, dict->capacity)) { while (i != last) {
struct ctrl_bkt *bkt = get_bkt(dict, i); struct ctrl_bkt *bkt = get_bkt(dict, i);
if (bkt->id.hash == id.hash) if (bkt->id.hash == id.hash)
@ -96,8 +99,7 @@ struct InputAxis *find_axis(struct ctrl_dict *dict, union InputID id)
return bkt->axis; 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 }; return (union InputID) { .value = value, .type = type };
} }
@ -167,10 +169,10 @@ bool CtrlPoll(struct Controller *ctrl)
return true; return true;
} }
int main() /*int main()
{ {
struct Controller ctrl; struct Controller ctrl;
if (!NewCtrl(&ctrl, 3)) if (!CreateCtrl(&ctrl))
return 1; return 1;
CtrlMap(&ctrl, 123, BUTTON, 111); CtrlMap(&ctrl, 123, BUTTON, 111);
@ -199,4 +201,4 @@ int main()
printf("success"); printf("success");
return 0; return 0;
} }*/

View file

@ -56,7 +56,7 @@ struct Controller {
struct ctrl_dict binds; struct ctrl_dict binds;
}; };
bool NewCtrl(struct Controller *ctrl, size_t init_axes); bool CreateCtrl(struct Controller *ctrl);
void FreeCtrl(struct Controller *ctrl); void FreeCtrl(struct Controller *ctrl);
@ -87,8 +87,8 @@ struct ControlBind {
u8 type; u8 type;
}; };
const size_t code_count = 12; #define CODE_COUNT 12
const struct ControlBind ctrl_binds[12] = { /*const struct ControlBind ctrl_binds[12] = {
{ LEFT, 0x25, BUTTON }, { LEFT, 0x25, BUTTON },
{ RIGHT, 0x27, BUTTON }, { RIGHT, 0x27, BUTTON },
{ SOFT_DROP, 0x28, BUTTON }, { SOFT_DROP, 0x28, BUTTON },
@ -101,4 +101,4 @@ const struct ControlBind ctrl_binds[12] = {
{ VSCROLL, 0, AXIS }, { VSCROLL, 0, AXIS },
{ HSCROLL, 1, AXIS }, { HSCROLL, 1, AXIS },
{ MOUSE, 0, JOYSTICK } { MOUSE, 0, JOYSTICK }
}; };*/

View file

@ -1,5 +1,4 @@
#include "input.h" #include "input.h"
#include <pthread.h>
#include "platform.h" #include "platform.h"
@ -30,41 +29,46 @@ void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *rec)
*buf_get(buf, buf->len++) = *rec; *buf_get(buf, buf->len++) = *rec;
} }
struct input_args { void *input_thread_loop(struct InputThreadHandle *hand)
struct InputBuffer *buf;
pthread_mutex_t *mutex;
};
void *input_thread_loop(struct input_args *args)
{ {
struct InputBuffer *buf = args->buf;
pthread_mutex_t *mutex = args->mutex;
free(args);
struct InputBuffer tmp_buf = { .len = 0, .start = 0 }; struct InputBuffer tmp_buf = { .len = 0, .start = 0 };
while (true) { while (!hand->is_terminating) {
if (!PlatformReadInput(&tmp_buf)) if (!PlatformReadInput(&tmp_buf)) {
hand->err = true;
return;
}
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 BeginInputThread(struct InputThreadHandle *hand, struct InputBuffer *buf)
{
hand->mutex = PTHREAD_MUTEX_INITIALIZER;
return pthread_create(&hand->thread, nullptr, input_thread_loop, hand) == 0;
}
bool EndInputThread(struct InputThreadHandle *hand)
{
hand->is_terminating = true;
if (!PlatformStopInput())
return false; return false;
pthread_mutex_lock(mutex); if (!pthread_mutex_destroy(&hand->mutex))
{ return false;
InputBufferTransfer(&tmp_buf, buf);
}
pthread_mutex_unlock(mutex);
}
return nullptr; if (!pthread_join(hand->thread, nullptr))
} return false;
bool CreateInputThread(struct InputBuffer *buf, pthread_mutex_t *mutex) return true;
{
struct input_args *args = malloc(sizeof(struct input_args));
*args = (struct input_args) {
.buf = buf,
.mutex = mutex,
};
pthread_t thread;
return pthread_create(&thread, nullptr, input_thread_loop, args) == 0;
} }

View file

@ -4,6 +4,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h>
#include "fumotris.h" #include "fumotris.h"
#include "gametime.h" #include "gametime.h"
@ -19,7 +20,11 @@ enum InputType {
union InputID { union InputID {
struct { struct {
union { u16 code; u16 bind; u16 value }; union {
u16 code;
u16 bind;
u16 value;
};
u16 type; u16 type;
}; };
u32 hash; u32 hash;
@ -66,8 +71,20 @@ struct InputBuffer {
u8f start; 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 InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest);
void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *src); void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *src);
bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex); bool BeginInputThread(struct InputThreadHandle *hand, struct InputBuffer *buf);
bool EndInputThread(struct InputThreadHandle *hand);

View file

@ -12,19 +12,12 @@
#include "win.h" #include "win.h"
#endif #endif
enum PlatformError {
PLTF_E_INITFAIL,
};
struct Error {
int e;
};
bool PlatformInit(); bool PlatformInit();
bool PlatformGetRefreshRate(u16f *out); bool PlatformGetRefreshRate(u16f *out);
bool PlatformReadInput(struct InputBuffer *buf); bool PlatformReadInput(struct InputBuffer *buf);
bool PlatformStopInput();
bool PlatformWait(struct Time relative); bool PlatformWait(struct Time relative);

View file

@ -5,21 +5,31 @@
#include "gametime.h" #include "gametime.h"
#include "input.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 timer;
HANDLE input_handle;
} win; } win;
bool init_handles() bool init_handles()
{ {
win.input_handle = GetStdHandle(STD_INPUT_HANDLE); win.input_hand = GetStdHandle(STD_INPUT_HANDLE);
if (win.input_handle == INVALID_HANDLE_VALUE) if (win.input_hand == INVALID_HANDLE_VALUE)
return false; return false;
win.timer = CreateWaitableTimerW(NULL, TRUE, NULL); win.timer = CreateWaitableTimerW(NULL, TRUE, NULL);
if (win.timer == NULL) if (win.timer == NULL)
return false; return false;
win.early_exit_hand = CreateEventW(NULL, FALSE, FALSE, NULL);
if (win.early_exit_hand == NULL)
return false;
return true; return true;
} }
@ -31,7 +41,7 @@ bool init_console()
| ENABLE_MOUSE_INPUT | ENABLE_MOUSE_INPUT
| ENABLE_WINDOW_INPUT; | ENABLE_WINDOW_INPUT;
return SetConsoleMode(win.input_handle, mode) != 0; return SetConsoleMode(win.input_hand, mode) != 0;
} }
bool PlatformInit() bool PlatformInit()
@ -47,11 +57,11 @@ bool PlatformInit()
bool PlatformGetRefreshRate(u16f *out) bool PlatformGetRefreshRate(u16f *out)
{ {
DEVMODE mode; DEVMODEW mode;
mode.dmSize = sizeof(DEVMODE); mode.dmSize = sizeof(DEVMODEW);
mode.dmDriverExtra = 0; mode.dmDriverExtra = 0;
if(!EnumDisplaySettingsA(NULL, ENUM_CURRENT_SETTINGS, &mode)) if(!EnumDisplaySettingsW(NULL, ENUM_CURRENT_SETTINGS, &mode))
return false; return false;
*out = mode.dmDisplayFrequency; *out = mode.dmDisplayFrequency;
@ -110,11 +120,16 @@ bool copy_rec(struct InputRecord *rec, INPUT_RECORD *win_rec)
bool PlatformReadInput(struct InputBuffer *buf) 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; DWORD max_records = IO_BUF_SIZE - buf->len;
INPUT_RECORD win_buf[max_records]; INPUT_RECORD win_buf[max_records];
DWORD filled; DWORD filled;
if (!ReadConsoleInputW(win.input_handle, win_buf, max_records, &filled))
if (!ReadConsoleInputW(win.input_hand, win_buf, max_records, &filled))
return false; return false;
struct InputRecord rec = { .time = TimeNow() }; struct InputRecord rec = { .time = TimeNow() };
@ -129,6 +144,11 @@ bool PlatformReadInput(struct InputBuffer *buf)
return true; return true;
} }
bool PlatformStopInput()
{
return SetEvent(win.early_exit_hand) == 0;
}
bool PlatformWait(struct Time relative) bool PlatformWait(struct Time relative)
{ {
LARGE_INTEGER duration; LARGE_INTEGER duration;

View file

@ -14,17 +14,26 @@
#include "event.h" #include "event.h"
#include "platform.h" #include "platform.h"
void ErrorExit(char *message)
{
printf(message);
exit(1);
}
int main() int main()
{ {
struct Instance game;
if (!Start(&game))
exit(1);
if (!PlatformInit()) if (!PlatformInit())
exit(1); ErrorExit("Platform failed to initialize");
CreateInputThread(&game.ctrl.buf, ); struct Controller ctrl;
Loop(&game); if (!CreateCtrl(&ctrl))
ErrorExit("Out of memory");
struct InputThreadHandle input;
if (!BeginInputThread(&input, &ctrl.buf))
ErrorExit("Input handle failed to initialize");
printf("woah");
return 0; return 0;
} }

BIN
test.exe

Binary file not shown.