gay and drepresed

fdggf
This commit is contained in:
Julia 2024-04-04 08:27:54 -05:00
parent 21c89ae7f9
commit 1d5c402f19
7 changed files with 25 additions and 68 deletions

View file

@ -19,7 +19,7 @@ enum CtrlType {
ESCAPE ESCAPE
}; };
struct CtrlRecord { struct Record {
u16 id; u16 id;
u8 type; u8 type;
@ -40,19 +40,11 @@ struct CtrlRecord {
}; };
struct RecordBuffer { struct RecordBuffer {
struct CtrlRecord records[IO_BUF_SIZE]; struct Record records[IO_BUF_SIZE];
size_t count; size_t count;
pthread_mutex_t mutex; pthread_mutex_t mutex;
}; };
struct RecordBuffer NewInputBuffer()
{
struct RecordBuffer buf;
buf.count = 0;
buf.mutex = PTHREAD_MUTEX_INITIALIZER;
return buf;
}
struct Axis { struct Axis {
union { union {
struct { struct {
@ -263,7 +255,7 @@ struct Axis *CtrlGet(struct Ctrl *ctrl, u16f code, u8f type)
return code_bkt->axis; return code_bkt->axis;
} }
void update_key(struct Axis *axis, struct CtrlRecord *record) void update_key(struct Axis *axis, struct Record *record)
{ {
if (record->data.key.is_down) if (record->data.key.is_down)
axis->last_pressed = record->timestamp; axis->last_pressed = record->timestamp;
@ -273,13 +265,13 @@ void update_key(struct Axis *axis, struct CtrlRecord *record)
axis->data.key.is_down = record->data.key.is_down; axis->data.key.is_down = record->data.key.is_down;
} }
void update_axis(struct Axis *axis, struct CtrlRecord *record) void update_axis(struct Axis *axis, struct Record *record)
{ {
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;
} }
void update_joystick(struct Axis *axis, struct CtrlRecord *record) void update_joystick(struct Axis *axis, struct Record *record)
{ {
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;
@ -289,7 +281,7 @@ void update_joystick(struct Axis *axis, struct CtrlRecord *record)
bool CtrlPoll(struct Ctrl *ctrl, struct RecordBuffer *rec_buf) bool CtrlPoll(struct Ctrl *ctrl, struct RecordBuffer *rec_buf)
{ {
for (size_t i = 0; i < rec_buf->count; i++) { for (size_t i = 0; i < rec_buf->count; i++) {
struct CtrlRecord *rec = &rec_buf->records[i]; struct Record *rec = &rec_buf->records[i];
struct Axis *axis = find_axis(&ctrl->binds, rec->id, rec->type); struct Axis *axis = find_axis(&ctrl->binds, rec->id, rec->type);
if (axis == nullptr) if (axis == nullptr)

View file

@ -19,7 +19,7 @@ enum CtrlType {
ESCAPE ESCAPE
}; };
struct CtrlRecord { struct Record {
u16 id; u16 id;
u8 type; u8 type;
@ -40,13 +40,11 @@ struct CtrlRecord {
}; };
struct RecordBuffer { struct RecordBuffer {
struct CtrlRecord records[IO_BUF_SIZE]; struct Record records[IO_BUF_SIZE];
size_t count; size_t count;
pthread_mutex_t mutex; pthread_mutex_t mutex;
}; };
struct RecordBuffer NewInputBuffer();
struct Axis { struct Axis {
union { union {
struct { struct {
@ -105,7 +103,7 @@ struct Ctrl {
pthread_mutex_t mutex; pthread_mutex_t mutex;
}; };
struct Ctrl NewCtrl(struct ctrl_dict *codes, struct ctrl_dict *binds, struct Axis *axes); struct Ctrl NewCtrl();
bool CtrlMap(struct Ctrl *ctrl, u16f code, u16f bind, u8f type); bool CtrlMap(struct Ctrl *ctrl, u16f code, u16f bind, u8f type);

View file

@ -32,12 +32,12 @@ void *block_input(void *args_ptr)
return nullptr; return nullptr;
} }
void StartInput(Ctrl *ctrl, struct RecordBuffer *buf) void StartInput(struct Ctrl *ctrl, struct RecordBuffer *buf)
{ {
pthread_create(&ctrl->thread, nullptr, block_input, buf); pthread_create(&ctrl->thread, nullptr, block_input, buf);
} }
void JoinInput(Ctrl *ctrl) void JoinInput(struct Ctrl *ctrl)
{ {
pthread_join(ctrl->thread, nullptr); pthread_join(ctrl->thread, nullptr);
} }

View file

@ -68,7 +68,7 @@ bool WinGetRefreshRate(u32f *out)
return true; return true;
} }
void set_key_record(struct CtrlRecord *record, KEY_EVENT_RECORD win_key) void set_key_record(struct Record *record, KEY_EVENT_RECORD win_key)
{ {
record->type = KEY; record->type = KEY;
record->id = win_key.wVirtualKeyCode; record->id = win_key.wVirtualKeyCode;
@ -78,7 +78,7 @@ void set_key_record(struct CtrlRecord *record, KEY_EVENT_RECORD win_key)
record->type = ESCAPE; record->type = ESCAPE;
} }
bool set_mouse_record(struct CtrlRecord *record, MOUSE_EVENT_RECORD win_mouse) bool set_mouse_record(struct Record *record, MOUSE_EVENT_RECORD win_mouse)
{ {
switch (win_mouse.dwEventFlags) { switch (win_mouse.dwEventFlags) {
case MOUSE_WHEELED: case MOUSE_WHEELED:
@ -103,7 +103,7 @@ bool set_mouse_record(struct CtrlRecord *record, MOUSE_EVENT_RECORD win_mouse)
return true; return true;
} }
bool dispatch_record(struct CtrlRecord *record, INPUT_RECORD win_record) bool dispatch_record(struct Record *record, INPUT_RECORD win_record)
{ {
switch (win_record.EventType) { switch (win_record.EventType) {
case KEY_EVENT: case KEY_EVENT:
@ -140,7 +140,7 @@ bool WinBlockInput(struct RecordBuffer *buf)
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++) {
struct CtrlRecord record; struct Record record;
record.timestamp = now; record.timestamp = now;
bool include = dispatch_record(&record, win_buf[i]); bool include = dispatch_record(&record, win_buf[i]);

View file

@ -90,49 +90,19 @@ const struct CtrlBind ctrl_binds[12] = {
{ MOUSE, 0, JOYSTICK } { MOUSE, 0, JOYSTICK }
}; };
void Draw(struct Terminal *term) void Draw(struct Instance *game)
{ {
pthread_mutex_lock(&term->mutex);
struct TChar4 term_blks[term->area];
pthread_cond_signal(&term->is_initialized);
pthread_mutex_unlock(&term->mutex);
size_t buf_size = TermBufSize(term);
char buf[buf_size];
while (true) { while (true) {
pthread_mutex_lock(&term->mutex); Call(&game->on_draw, game);
pthread_cond_wait(&term->draw_ready, &term->mutex);
TermOut(term, buf, buf_size);
pthread_mutex_unlock(&term->mutex);
puts(buf);
} }
} }
void Update(struct Terminal *term, struct Ctrl *ctrl, struct RecordBuffer *rec_buf) void Update(struct Instance *game)
{ {
pthread_mutex_lock(&term->mutex);
pthread_cond_wait(&term->is_initialized, &term->mutex);
pthread_mutex_unlock(&term->mutex);
while (true) { while (true) {
// Input Call(&game->on_update, game);
pthread_mutex_lock(&rec_buf->mutex);
CtrlPoll(ctrl, rec_buf);
pthread_mutex_unlock(&rec_buf->mutex);
// Game logic
// Draw
pthread_mutex_lock(&term->mutex);
pthread_cond_signal(&term->draw_ready);
pthread_mutex_unlock(&term->mutex);
} }
} }
@ -178,7 +148,7 @@ int main()
}; };
struct Instance game = { struct Instance game = {
.term = NewTerm(nullptr, 20, 20), .term = NewTerm(20, 20),
.ctrl = NewCtrl(), .ctrl = NewCtrl(),
.on_start = NewDelegate(16), .on_start = NewDelegate(16),

View file

@ -27,17 +27,14 @@ struct Terminal {
pthread_cond_t draw_ready; pthread_cond_t draw_ready;
}; };
struct Terminal NewTerm(struct TChar4 *blks, size_t wid, size_t hgt) struct Terminal NewTerm(size_t wid, size_t hgt)
{ {
size_t area = wid * hgt;
memset(blks, 0, sizeof(struct TChar4) * area);
return (struct Terminal) { return (struct Terminal) {
.wid = wid, .wid = wid,
.hgt = hgt, .hgt = hgt,
.area = area, .area = wid * hgt,
.refresh_rate = 60, .refresh_rate = 60,
.blks = blks, .blks = nullptr,
.mutex = PTHREAD_MUTEX_INITIALIZER, .mutex = PTHREAD_MUTEX_INITIALIZER,
.is_initialized = PTHREAD_COND_INITIALIZER, .is_initialized = PTHREAD_COND_INITIALIZER,

View file

@ -28,7 +28,7 @@ struct Terminal {
pthread_cond_t draw_ready; pthread_cond_t draw_ready;
}; };
struct Terminal NewTerm(struct TChar4 *blks, size_t wid, size_t hgt); struct Terminal NewTerm(size_t wid, size_t hgt);
size_t TermBufSize(struct Terminal *term); size_t TermBufSize(struct Terminal *term);