input almost working,,

yay
This commit is contained in:
Julia 2024-04-01 17:39:21 -05:00
parent f14ee5eab2
commit 1517f2caea
18 changed files with 252 additions and 205 deletions

View file

@ -1 +1 @@
{"source\\main.c": "3d2369e9333b52142cb9f019e877e434", "source\\io\\controller.c": "4459cc302f4b70b072f39b43e87a0c08", "source\\io\\input.c": "d3336a096a3ac2d792292d84dd20fb8f", "source\\io\\platforms\\win.c": "4eb14343a9cb40a7f6a9d9e43329c46e", "source\\io\\platforms\\winio.c": "b3a5e9d8f2e8ab4ec38d14007e4fc3f4"} {"source\\main.c": "82d4037af47edc6a1ef16f061e8980c2", "source\\game\\gametime.c": "40eedcb0dd0e0ebd42a09860aeb24517", "source\\game\\tetr.c": "fee38bad904b2053644fddf0ba08cd5e", "source\\io\\ctrl.c": "a838d22152710a4be156b5c90c41863c", "source\\io\\input.c": "aca98b0ac5983206b3fad774dc4d6146", "source\\io\\term.c": "b8fa47ec18810973c2123e7ec3a3cec6", "source\\io\\platforms\\win.c": "5f8d684bbaae885edf7505a1d90d91e8", "source\\io\\platforms\\winhandler.c": "b0c21fa0370350172987a3ec39be1d20"}

Binary file not shown.

BIN
objects/ctrl.o Normal file

Binary file not shown.

BIN
objects/gametime.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
objects/term.o Normal file

Binary file not shown.

BIN
objects/tetr.o Normal file

Binary file not shown.

Binary file not shown.

BIN
objects/winhandler.o Normal file

Binary file not shown.

View file

@ -1,3 +1,4 @@
#include <assert.h>
#include <iso646.h> #include <iso646.h>
#include <pthread.h> #include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
@ -5,10 +6,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "fumotris.h" #include "fumotris.h"
#define IO_BUF_SIZE 8 #define IO_BUF_SIZE 16
enum InputType { enum InputType {
KEY, KEY,
@ -19,8 +21,8 @@ enum InputType {
}; };
struct InputRecord { struct InputRecord {
enum InputType type;
u16 id; u16 id;
u8 type;
union { union {
struct { struct {
@ -35,7 +37,7 @@ struct InputRecord {
} joystick; } joystick;
} data; } data;
double timestamp; struct timespec timestamp;
}; };
struct InputBuffer { struct InputBuffer {
@ -53,8 +55,6 @@ struct InputBuffer NewInputBuffer()
} }
struct Axis { struct Axis {
u8 type;
union { union {
struct { struct {
u32 value; u32 value;
@ -70,8 +70,8 @@ struct Axis {
} joystick; } joystick;
} data; } data;
double last_pressed; struct timespec last_pressed;
double last_released; struct timespec last_released;
}; };
enum KeyCode { enum KeyCode {
@ -112,208 +112,220 @@ hashtype Hash(void *item, size_t size)
struct ident { struct ident {
u16 id; u16 id;
enum InputType type; u8 type;
}; };
hashtype hash_ident(u16f id, enum InputType type) hashtype hash_ident(u16f value, u8f type)
{ {
struct ident obj = { id, type }; struct ident id = { value, type };
return Hash(&obj, sizeof(struct ident)); return Hash(&id, sizeof(struct ident));
} }
struct code_bkt { struct bkt {
hashtype hash; hashtype hash;
u16 code; u16 value;
struct Axis axis; u8 type;
};
struct bind_bkt {
hashtype hash;
u16 bind;
struct Axis *axis; struct Axis *axis;
}; };
struct dict {
size_t capacity;
size_t filled;
struct bkt *bkts;
};
struct Ctrl { struct Ctrl {
struct { struct dict codes;
size_t capacity; struct dict binds;
size_t filled;
struct code_bkt *bkts;
} codes;
struct {
size_t capacity;
size_t filled;
struct bind_bkt *bkts;
} binds;
pthread_t thread;
pthread_mutex_t mutex; pthread_mutex_t mutex;
}; };
typedef struct Ctrl Ctrl; typedef struct Ctrl Ctrl;
Ctrl NewCtrl(struct code_bkt *codes, size_t c, struct bind_bkt *binds, size_t b) Ctrl NewCtrl(struct bkt *codes, struct Axis *axes, size_t c, struct bkt *binds, size_t b)
{ {
memset(codes, 0, sizeof(struct code_bkt) * c); memset(codes, 0, sizeof(struct bkt) * c);
memset(binds, 0, sizeof(struct bind_bkt) * b); memset(axes, 0, sizeof(struct Axis) * c);
memset(binds, 0, sizeof(struct bkt) * b);
for (size_t i = 0; i < c; i++) {
codes[i].axis = &axes[i];
}
Ctrl ctrl; Ctrl ctrl;
ctrl.codes.capacity = c;
ctrl.codes.filled = 0;
ctrl.codes.bkts = codes; ctrl.codes.bkts = codes;
ctrl.binds.capacity = b;
ctrl.binds.filled = 0;
ctrl.binds.bkts = binds; ctrl.binds.bkts = binds;
ctrl.mutex = PTHREAD_MUTEX_INITIALIZER; ctrl.mutex = PTHREAD_MUTEX_INITIALIZER;
return ctrl; return ctrl;
} }
void CtrlPoll(struct InputBuffer *buf) struct bkt *get_bkt(struct dict *dict, size_t i)
{ {
for (size_t i = 0; i < buf->count; i++) { assert(i < dict->capacity);
struct InputRecord *rec = &buf->records[i]; return &dict->bkts[i];
}
switch (rec->type) { void set_bkt(struct bkt *bkt, hashtype hash, u16f value, u8f type)
case KEY: {
key_update(); bkt->hash = hash;
break; bkt->value = value;
case AXIS: bkt->type = type;
axis_update(); }
break;
case JOYSTICK: size_t wrap(size_t x, size_t wrap)
joystick_update(); {
break; return x % (SIZE_MAX - wrap + 1);
}
bool find_or_set(struct dict *dict, struct bkt **out, u16f value, u8f type)
{
hashtype hash = hash_ident(value, type);
const size_t index = hash % dict->capacity;
size_t i = index;
while (i != wrap(index - 1, dict->capacity)) {
struct bkt *bkt = get_bkt(dict, i);
if (bkt->hash == 0) {
set_bkt(bkt, hash, value, type);
dict->filled += 1;
*out = bkt;
return false;
} }
if (bkt->value == value and bkt->type == type) {
*out = bkt;
return true;
}
i = (i + 1) % dict->capacity;
} }
*out = nullptr;
return false;
} }
struct ctrl_bkt *get_code_bkt(Ctrl *ctrl, size_t i) struct bkt *find(struct dict *dict, u16f value, u8f type)
{ {
assert(i < ctrl->codes.capacity); hashtype hash = hash_ident(value, type);
return &ctrl->codes.bkts[i]; const size_t index = hash % dict->capacity;
}
struct ctrl_bkt *get_bind_bkt(Ctrl *ctrl, size_t i)
{
assert(i < ctrl->binds.capacity);
return &ctrl->binds.bkts[i];
}
struct code_bkt *find_code_or_set_empty(Ctrl *ctrl, u16 code, )
{
const size_t index = hash % ctrl->codes.capacity;
size_t i = index; size_t i = index;
while (i != wrap(index - 1, dict->capacity)) {
while (i != index - 1) { struct bkt *bkt = get_bkt(dict, i);
struct code_bkt *bkt = get_code_bkt(ctrl, i);
if (bkt->hash == search)
return bkt;
if (bkt->hash == 0) if (bkt->hash == 0)
return bkt; goto next;
if (bkt->value == value and bkt->type == type) {
return bkt;
}
next:
i = (i + 1) % dict->capacity;
};
i = (i + 1) % ctrl->codes.capacity;
}
return nullptr; return nullptr;
} }
struct ctrl_bkt *find_bind(Ctrl *ctrl, hashtype hash, hashtype search) struct Axis *find_axis(struct dict *dict, u16f value, u8f type)
{ {
const size_t index = hash % ctrl->binds.capacity; struct bkt *bkt = find(dict, value, type);
size_t i = index; if (bkt == nullptr)
while (i != index - 1) {
struct bind_bkt *bkt = get_bind_bkt(ctrl, i);
if (bkt->hash == 0)
return bkt;
i = (i + 1) % ctrl->binds.capacity;
}
return nullptr;
}
bool CtrlMap(Ctrl *ctrl, u16f bind, u16f code, enum InputType type)
{
if (ctrl->binds.filled == ctrl->binds.capacity)
return false;
struct code_bkt *code_bkt = find_code_or_set_empty(ctrl, code, type);
assert(code_bkt != nullptr);
hashtype bind_hash = hash_ident(bind, type);
struct bind_bkt *bind_bkt = find_bind(ctrl, bind_hash, bind_hash);
bind_bkt->hash = bind_hash;
bind_bkt->bind = bind;
bind_bkt->axis = &code_bkt->axis;
ctrl->binds.filled += 1;
return true;
}
struct Axis *find_bind_axis(Ctrl *ctrl, u16f bind, enum InputType type)
{
hashtype bind_hash = hash_ident(bind, KEY);
struct ctrl_bkt *bind_bkt = find_bind(ctrl, bind_hash, bind_hash);
if (bind_bkt == nullptr)
return nullptr; return nullptr;
return &get_bkt(ctrl, bind_bkt->index)->axis; return bkt->axis;
}
bool CtrlMap(Ctrl *ctrl, u16f code, u16f bind, u8f type)
{
assert(ctrl->codes.filled < ctrl->codes.capacity);
assert(ctrl->binds.filled < ctrl->binds.capacity);
struct bkt *code_bkt;
find_or_set(&ctrl->codes, &code_bkt, code, type);
assert(code_bkt != nullptr);
struct bkt *bind_bkt;
bool bind_existed = find_or_set(&ctrl->binds, &bind_bkt, bind, type);
assert(bind_bkt != nullptr);
if(bind_existed and bind_bkt->axis == code_bkt->axis)
return false;
bind_bkt->axis = code_bkt->axis;
return true;
} }
struct Axis *CtrlGet(Ctrl *ctrl, u16f code, u8f type) struct Axis *CtrlGet(Ctrl *ctrl, u16f code, u8f type)
{ {
= struct ctrl_bkt *code_bkt = find_code(ctrl, code, type); struct bkt *code_bkt = find(&ctrl->codes, code, type);
if (code_bkt == nullptr) if (code_bkt == nullptr)
return nullptr; return nullptr;
return &code_bkt->axis; return code_bkt->axis;
} }
bool CtrlUpdateKey(Ctrl *ctrl, struct InputRecord *record) void update_key(struct Axis *axis, struct InputRecord *record)
{ {
struct Axis *axis = find_bind_axis(ctrl, record->id, KEY); if (record->data.key.is_down)
if (axis == nullptr)
return false;
if (record->data.key.is_down) {
axis->last_pressed = record->timestamp; axis->last_pressed = record->timestamp;
} else { else
axis->last_released = record->timestamp; axis->last_released = record->timestamp;
}
axis->data.axis.value = record->data.key.is_down;
return true; axis->data.key.is_down = record->data.key.is_down;
} }
bool CtrlUpdateAxis(Ctrl *ctrl, struct InputRecord *record) void update_axis(struct Axis *axis, struct InputRecord *record)
{ {
struct Axis *axis = find_bind_axis(ctrl, record->id, AXIS);
if (axis == nullptr)
return false;
axis->data.axis.value = record->data.axis.value; axis->data.axis.value = record->data.axis.value;
axis->last_pressed = record->timestamp; axis->last_pressed = record->timestamp;
return true;
} }
bool CtrlUpdateJoystick(Ctrl *ctrl, struct InputRecord *record) void update_joystick(struct Axis *axis, struct InputRecord *record)
{ {
struct Axis *axis = find_bind_axis(ctrl, record->id, JOYSTICK);
if (axis == nullptr)
return false;
axis->data.joystick.x = record->data.joystick.x; axis->data.joystick.x = record->data.joystick.x;
axis->data.joystick.y = record->data.joystick.y; axis->data.joystick.y = record->data.joystick.y;
axis->last_pressed = record->timestamp; axis->last_pressed = record->timestamp;
return true;
} }
bool CtrlUpdateWindow(Ctrl *ctrl, struct InputRecord *record) bool CtrlPoll(Ctrl *ctrl, struct InputBuffer *buf)
{ {
struct Axis *axis = find_bind_axis(ctrl, record->id, WINDOW); pthread_mutex_lock(&buf->mutex);
if (axis == nullptr) pthread_mutex_lock(&ctrl->mutex);
return false;
axis->data.joystick.x = record->data.joystick.x; for (size_t i = 0; i < buf->count; i++) {
axis->data.joystick.y = record->data.joystick.y; struct InputRecord *rec = &buf->records[i];
axis->last_pressed = record->timestamp;
struct Axis *axis = find_axis(&ctrl->binds, rec->id, rec->type);
if (axis == nullptr)
continue;
switch (rec->type) {
case KEY:
update_key(axis, rec);
break;
case AXIS:
update_axis(axis, rec);
break;
case JOYSTICK:
case WINDOW:
update_joystick(axis, rec);
break;
default:
return false;
}
}
buf->count = 0;
pthread_mutex_unlock(&buf->mutex);
pthread_mutex_unlock(&ctrl->mutex);
return true; return true;
} }

View file

@ -6,10 +6,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "fumotris.h" #include "fumotris.h"
#define IO_BUF_SIZE 8 #define IO_BUF_SIZE 16
enum InputType { enum InputType {
KEY, KEY,
@ -20,8 +21,8 @@ enum InputType {
}; };
struct InputRecord { struct InputRecord {
enum InputType type;
u16 id; u16 id;
u8 type;
union { union {
struct { struct {
@ -36,7 +37,7 @@ struct InputRecord {
} joystick; } joystick;
} data; } data;
double timestamp; struct timespec timestamp;
}; };
struct InputBuffer { struct InputBuffer {
@ -63,8 +64,8 @@ struct Axis {
} joystick; } joystick;
} data; } data;
double last_pressed; struct timespec last_pressed;
double last_released; struct timespec last_released;
}; };
enum KeyCode { enum KeyCode {
@ -90,37 +91,42 @@ enum JoystickCode {
typedef u32 hashtype; typedef u32 hashtype;
struct ctrl_bkt { struct bkt {
hashtype bind_hash; hashtype hash;
u16 bind; u16 value;
size_t index; u8 type;
struct Axis *axis;
};
hashtype code_hash; struct dict {
u16 code; size_t capacity;
size_t filled;
struct Axis axis; struct bkt *bkts;
}; };
struct Ctrl { struct Ctrl {
size_t capacity; struct dict codes;
size_t filled; struct dict binds;
struct ctrl_bkt *bkts;
pthread_t thread;
pthread_mutex_t mutex; pthread_mutex_t mutex;
}; };
typedef struct Ctrl Ctrl; typedef struct Ctrl Ctrl;
Ctrl NewCtrl(struct ctrl_bkt *bkts_prealloc, size_t capacity); #define NEW_CTRL(CTRL, CODES, BINDS) \
struct bkt NEW_CTRL_CODES[CODES]; \
struct bkt NEW_CTRL_BINDS[BINDS]; \
struct Axis NEW_CTRL_AXES[CODES]; \
CTRL = NewCtrl( \
NEW_CTRL_CODES, \
NEW_CTRL_AXES, CODES, \
NEW_CTRL_BINDS, BINDS \
); \
bool CtrlMap(Ctrl *ctrl, u16f bind, u16f code, enum InputType type); Ctrl NewCtrl(struct bkt *codes, struct Axis *axes, size_t c, struct bkt *binds, size_t b);
struct Axis *CtrlGet(Ctrl *ctrl, u16f code, enum InputType type); bool CtrlMap(Ctrl *ctrl, u16f code, u16f bind, u8f type);
bool CtrlUpdateKey(Ctrl *ctrl, struct InputRecord *record); struct Axis *CtrlGet(Ctrl *ctrl, u16f code, u8f type);
bool CtrlUpdateAxis(Ctrl *ctrl, struct InputRecord *record);
bool CtrlUpdateJoystick(Ctrl *ctrl, struct InputRecord *record);
bool CtrlUpdateWindow(Ctrl *ctrl, struct InputRecord *record);
bool CtrlPoll(Ctrl *ctrl, struct InputBuffer *buf);

View file

@ -12,36 +12,52 @@
#include "win.h" #include "win.h"
#endif #endif
#define IO_BUF_SIZE 8
struct input_args { struct input_args {
Ctrl *ctrl; Ctrl *ctrl;
struct InputBuffer *in_buf; struct InputBuffer *buf;
pthread_mutex_t mutex;
}; };
void *block_input(void *args_ptr) void *block_input(void *args_ptr)
{ {
struct input_args *args = args_ptr; struct input_args *args = args_ptr;
Ctrl *ctrl = args->ctrl; Ctrl *ctrl = args->ctrl;
struct InputBuffer *in_buf = args->in_buf; struct InputBuffer *buf = args->buf;
printf("RECIEVED BUF: %u, %u\n", args->ctrl, args->buf);
input_loop: while (true) {
bool success; printf("\tIN LOOP\n");
bool success;
#ifdef _WIN32 #ifdef _WIN32
success = WindowsBlockInput(&in_buf); success = WindowsBlockInput(buf);
#endif #endif
if (!success) printf("%llu\n", buf->records[0].timestamp.tv_sec);
exit(1);
if (!success) {
printf("winfail");
exit(1);
}
}
goto input_loop;
return nullptr; return nullptr;
} }
void StartInput(Ctrl *ctrl) void StartInput(Ctrl *ctrl, struct InputBuffer *buf)
{ {
pthread_t input_thread; printf("START INPUT:%u\n", buf);
pthread_create(&input_thread, nullptr, block_input, ctrl); struct input_args args = {
//pthread_join(input_thread, nullptr); .ctrl = ctrl,
.buf = buf,
.mutex = PTHREAD_MUTEX_INITIALIZER
};
pthread_mutex_lock(&args.mutex);
pthread_create(&ctrl->thread, nullptr, block_input, &args);
pthread_mutex_destroy();
}
void JoinInput(Ctrl *ctrl)
{
pthread_join(ctrl->thread, nullptr);
} }

View file

@ -8,10 +8,6 @@
#include "ctrl.h" #include "ctrl.h"
#include "fumotris.h" #include "fumotris.h"
struct InputBuffer { void StartInput(Ctrl *ctrl, struct InputBuffer *buf);
struct InputRecord records[IO_BUF_SIZE];
size_t count;
pthread_mutex_t mutex;
};
void StartInput(Ctrl *ctrl); void JoinInput(Ctrl *ctrl);

View file

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <windows.h> #include <windows.h>
#include <time.h>
#include "fumotris.h" #include "fumotris.h"
#include "gametime.h" #include "gametime.h"
@ -103,14 +104,20 @@ bool dispatch_record(struct InputRecord *record, INPUT_RECORD win_record)
bool WinBlockInput(struct InputBuffer *buf) bool WinBlockInput(struct InputBuffer *buf)
{ {
printf("\twin blocked: %u\n", buf);
size_t win_size = IO_BUF_SIZE - buf->count; size_t win_size = IO_BUF_SIZE - buf->count;
printf("\twhar\n");
INPUT_RECORD win_buf[win_size]; INPUT_RECORD win_buf[win_size];
DWORD count; DWORD count;
printf("\treading in..\n");
if (!ReadConsoleInput(windows.input_handle, win_buf, win_size, &count)) if (!ReadConsoleInput(windows.input_handle, win_buf, win_size, &count))
return false; return false;
printf("\read done\n");
struct timespec now;
timespec_get(&now, TIME_UTC);
double now = GetTime();
pthread_mutex_lock(&buf->mutex); pthread_mutex_lock(&buf->mutex);
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
@ -133,11 +140,12 @@ bool WinWait(double seconds)
{ {
LARGE_INTEGER duration; LARGE_INTEGER duration;
duration.QuadPart = (u64)(-10000000.0 * seconds); duration.QuadPart = (u64)(-10000000.0 * seconds);
if (!SetWaitableTimer(windows.timer, &duration, 0, NULL, NULL, FALSE)) if (!SetWaitableTimer(windows.timer, &duration, 0, NULL, NULL, FALSE))
return false; return false;
printf("about to wait..\n");
DWORD result = WaitForSingleObject(windows.timer, INFINITE); DWORD result = WaitForSingleObject(windows.timer, INFINITE);
printf("waitforsingle\n");
if (result != WAIT_OBJECT_0) if (result != WAIT_OBJECT_0)
return false; return false;

View file

@ -71,7 +71,8 @@ size_t TermBufToChars(struct TermBuf *term, char *buf, size_t max_chars)
u8f last_bg = 0; u8f last_bg = 0;
u8f last_fg = 0; u8f last_fg = 0;
size_t filled = snprintf(buf, max_chars, "\x1b[H\x1b[0m"); //size_t filled = snprintf(buf, max_chars, "\x1b[H\x1b[0m");
size_t filled = snprintf(buf, max_chars, "\x1b[0m");
for(size_t y = 0; y < term->hgt; y++) { for(size_t y = 0; y < term->hgt; y++) {
for(size_t x = 0; x < term->wid; x++) { for(size_t x = 0; x < term->wid; x++) {

View file

@ -51,7 +51,7 @@ u8 I[16] = {
0, 0, 0, 0 0, 0, 0, 0
}; };
void Loop(Ctrl *ctrl) void Loop(Ctrl *ctrl, struct InputBuffer *in_buf)
{ {
struct TermBuf term = NewTermBuf(20, 20); struct TermBuf term = NewTermBuf(20, 20);
struct CharBlk4 term_blks[term.area]; struct CharBlk4 term_blks[term.area];
@ -73,15 +73,16 @@ void Loop(Ctrl *ctrl)
falling.blks = falling_blks; falling.blks = falling_blks;
for (int i = 0; i < 7779997; i++) { for (int i = 0; i < 7779997; i++) {
CtrlPoll(ctrl, in_buf);
printf("polled\n");
TetrMapToTermBuf(&board, &term); TetrMapToTermBuf(&board, &term);
TetrMapToTermBuf(&falling, &term); TetrMapToTermBuf(&falling, &term);
size_t size = TermBufToChars(&term, out, out_max); TermBufToChars(&term, out, out_max);
puts(out); //puts(out);
WindowsWait(0.1); printf("%u\n", WindowsWait(0.01));
} }
} }
@ -89,8 +90,8 @@ int main()
{ {
WindowsInit(); WindowsInit();
struct ctrl_bkt bkts[13]; Ctrl ctrl;
Ctrl ctrl = NewCtrl(bkts, 13); NEW_CTRL(ctrl, 13, 13);
for (size_t i = 0; i < 9; i++) { for (size_t i = 0; i < 9; i++) {
CtrlMap(&ctrl, key_binds[i], key_codes[i], KEY); CtrlMap(&ctrl, key_binds[i], key_codes[i], KEY);
@ -103,8 +104,15 @@ int main()
printf("set controls\n"); printf("set controls\n");
StartInput(&ctrl); struct InputBuffer in_buf = {
Loop(&ctrl); .count = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER
};
StartInput(&ctrl, &in_buf);
Loop(&ctrl, &in_buf);
JoinInput(&ctrl);
return 0; return 0;
} }

BIN
test.exe

Binary file not shown.