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 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
};
};*/

View file

@ -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;
}
}*/

View file

@ -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 }
};
};*/

View file

@ -1,5 +1,4 @@
#include "input.h"
#include <pthread.h>
#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))
while (!hand->is_terminating) {
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;
pthread_mutex_lock(mutex);
{
InputBufferTransfer(&tmp_buf, buf);
}
pthread_mutex_unlock(mutex);
}
if (!pthread_mutex_destroy(&hand->mutex))
return false;
return nullptr;
}
bool CreateInputThread(struct InputBuffer *buf, pthread_mutex_t *mutex)
{
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;
if (!pthread_join(hand->thread, nullptr))
return false;
return true;
}

View file

@ -4,6 +4,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#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);
bool BeginInputThread(struct InputThreadHandle *hand, struct InputBuffer *buf);
bool EndInputThread(struct InputThreadHandle *hand);

View file

@ -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);

View file

@ -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);

View file

@ -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;
}

BIN
test.exe

Binary file not shown.