;
This commit is contained in:
Julia 2024-04-29 23:43:41 -05:00
parent 2e976a8d2e
commit f65cc081b3
8 changed files with 52 additions and 51 deletions

View file

@ -1,15 +1,10 @@
#include "ringbuffer.h" #include "ringbuffer.h"
#include <string.h> #include <string.h>
struct ring_buf {
struct RingBufferHead head;
u8 bytes[];
};
void *get_ptr(RingBufferT T, struct RingBufferHead *head, size_t i) void *get_ptr(RingBufferT T, struct RingBufferHead *head, size_t i)
{ {
struct ring_buf *ring = (struct ring_buf *)head; u8 *bytes = (u8 *)head;
return ring->bytes + T->SIZE * i; return bytes + T->OFFSET + T->SIZE * i;
} }
size_t RingBufferEmpty(RingBufferT T, struct RingBufferHead *head) size_t RingBufferEmpty(RingBufferT T, struct RingBufferHead *head)
@ -32,36 +27,41 @@ void *RingBufferNext(RingBufferT T, struct RingBufferHead *head)
void RingBufferAdd(RingBufferT T, struct RingBufferHead *dest, void *item) void RingBufferAdd(RingBufferT T, struct RingBufferHead *dest, void *item)
{ {
memcpy(RingBufferNext(T, dest), item, T->SIZE); memcpy(RingBufferNext(T, dest), item, T->SIZE);
dest->len += 1;
} }
void RingBufferTransfer( void RingBufferTransfer(
RingBufferT T, RingBufferT T,
struct RingBufferHead *dest, struct RingBufferHead *dest,
struct RingBufferHead *src struct RingBufferHead *tmp
) { ) {
size_t copy_max = min_size(T->LEN - dest->len, src->len); size_t copy_max = min_size(T->LEN - dest->len, tmp->len);
for (size_t i = 0; i < copy_max; i++) { for (size_t i = 0; i < copy_max; i++) {
void *to = RingBufferGet(T, dest_head, dest->len + i); void *to = RingBufferGet(T, dest, dest->len + i);
void *from = RingBufferGet(T, src_head, i); void *from = RingBufferGet(T, tmp, i);
memcpy(to, from, T->SIZE); memcpy(to, from, T->SIZE);
} }
dest->len += copy_max; dest->len += copy_max;
src->len -= copy_max; tmp->len -= copy_max;
} }
size_t RingBufferOut(RingBufferT T, size_t n, void *dest, void *src_head) size_t RingBufferOut(
{ RingBufferT T,
struct ring_buf *src = src_head; size_t n,
void *dest,
size_t copy_max = min_size(n, src->head.len); struct RingBufferHead *src
) {
size_t copy_max = min_size(n, src->len);
for (size_t i = 0; i < copy_max; i++) { for (size_t i = 0; i < copy_max; i++) {
void *to = (char *)dest + i * T->SIZE; void *to = (u8 *)dest + i * T->SIZE;
void *from = RingBufferGet(T, src, i); void *from = RingBufferGet(T, src, i);
memcpy(to, from, T->SIZE); memcpy(to, from, T->SIZE);
} }
src->len -= copy_max;
return copy_max; return copy_max;
} }

View file

@ -8,13 +8,14 @@
#include "fumotris.h" #include "fumotris.h"
typedef const struct RingBufferT { typedef const struct RingBufferT {
size_t LEN; size_t OFFSET;
size_t SIZE; size_t SIZE;
size_t LEN;
} *const RingBufferT; } *const RingBufferT;
struct RingBufferHead { struct RingBufferHead {
u8f len; size_t len;
u8f start; size_t start;
}; };
#define RINGBUF_HEAD_INIT ((struct RingBufferHead) { 0, 0 }) #define RINGBUF_HEAD_INIT ((struct RingBufferHead) { 0, 0 })
@ -29,6 +30,8 @@ void *RingBufferGet(
void *RingBufferNext(RingBufferT T, struct RingBufferHead *head); void *RingBufferNext(RingBufferT T, struct RingBufferHead *head);
void RingBufferAdd(RingBufferT T, struct RingBufferHead *dest, void *item);
void RingBufferTransfer( void RingBufferTransfer(
RingBufferT T, RingBufferT T,
struct RingBufferHead *dest, struct RingBufferHead *dest,

View file

@ -156,9 +156,10 @@ void CtrlPoll(struct Controller *ctrl)
for (size_t i = 0; i < ctrl->recs.head.len; i++) { for (size_t i = 0; i < ctrl->recs.head.len; i++) {
struct InputRecord *rec = &ctrl->recs.buf[i]; struct InputRecord *rec = &ctrl->recs.buf[i];
if (rec->id.type == BUTTON and rec->is_down)
ctrl->str
struct InputAxis *axis = find_axis(&ctrl->binds, rec->id); struct InputAxis *axis = find_axis(&ctrl->binds, rec->id);
if (axis == nullptr) if (axis == nullptr)
continue; continue;

View file

@ -4,13 +4,15 @@
#include "platform.h" #include "platform.h"
const struct RingBufferT IO_BUF_T = { const struct RingBufferT IO_BUF_T = {
.LEN = IO_BUF_SIZE, .OFFSET = offsetof(struct InputRecordBuf, buf),
.SIZE = sizeof(struct InputRecord) .SIZE = sizeof(struct InputRecord),
.LEN = IO_BUF_SIZE
}; };
const struct RingBufferT STR_BUF_T = { const struct RingBufferT STR_BUF_T = {
.LEN = STR_BUF_SIZE, .OFFSET = offsetof(struct InputStringBuf, buf),
.SIZE = sizeof(char) .SIZE = sizeof(char),
.LEN = STR_BUF_SIZE
}; };
void *input_worker(void *arg) void *input_worker(void *arg)
@ -18,20 +20,13 @@ void *input_worker(void *arg)
struct InputHandle *hand = arg; struct InputHandle *hand = arg;
struct InputRecordBuf tmp_recs = { .head.len = 0, .head.start = 0 }; struct InputRecordBuf tmp_recs = { .head.len = 0, .head.start = 0 };
struct InputStringBuf tmp_str = { .head.len = 0, .head.start = 0 };
while (!hand->is_terminating) { while (!hand->is_terminating) {
if (!PlatformReadInput(&tmp_recs, &tmp_str)) { if (!PlatformReadInput(&tmp_recs)) {
hand->err = true; hand->err = true;
return nullptr; return nullptr;
} }
printf("input read, len:%u\n", hand->recs->head.len);
for (int i = 0; i < hand->recs->head.len; i++) {
struct InputRecord *rec = RingBufferGet(&IO_BUF_T, &hand->recs->head, i);
printf("\ti:%u, type:%u, but:%u\n", i, rec->id.type, rec->id.bind);
}
if (pthread_mutex_lock(&hand->mutex) != 0) { if (pthread_mutex_lock(&hand->mutex) != 0) {
hand->err = true; hand->err = true;
return nullptr; return nullptr;
@ -45,7 +40,6 @@ void *input_worker(void *arg)
} }
RingBufferTransfer(&IO_BUF_T, &hand->recs->head, &tmp_recs.head); RingBufferTransfer(&IO_BUF_T, &hand->recs->head, &tmp_recs.head);
RingBufferTransfer(&STR_BUF_T, &hand->str->head, &tmp_str.head);
if (pthread_mutex_unlock(&hand->mutex) != 0) { if (pthread_mutex_unlock(&hand->mutex) != 0) {
hand->err = true; hand->err = true;

View file

@ -16,7 +16,7 @@ bool PlatformInit();
bool PlatformGetRefreshRate(u16f *out); bool PlatformGetRefreshRate(u16f *out);
bool PlatformReadInput(struct InputRecordBuf *in, struct InputStringBuf *str); bool PlatformReadInput(struct InputRecordBuf *in);
bool PlatformStopInput(); bool PlatformStopInput();

View file

@ -125,20 +125,15 @@ bool PlatformGetRefreshRate(u16f *out)
return true; return true;
} }
bool dispatch_rec( bool dispatch_rec(struct InputRecord *out, struct win_rec *rec) {
struct InputRecord *out, u8f type = rec->type
struct InputStringBuf *str, | (rec->is_mouse & rec->mouse.flags)
struct win_rec *rec | (rec->is_key & rec->key.is_down);
) {
u8f type = rec->type | (rec->is_mouse & rec->mouse.flags);
switch (type) { switch (type) {
case KEY_EVENT: { case KEY_EVENT: {
ReadButton(out, rec->key.vk_code, rec->key.is_down); ReadButton(out, rec->key.vk_code, rec->key.is_down);
char *to = RingBufferGet(&STR_BUF_T, &str->head, str->head.len);
str->head.len += UCS2ToUTF8(to, rec->key.ucs2_char);
return true; return true;
} }
case MOUSE_MOVE: { case MOUSE_MOVE: {
@ -166,7 +161,7 @@ bool dispatch_rec(
return false; return false;
} }
bool PlatformReadInput(struct InputRecordBuf *recs, struct InputStringBuf *str) bool PlatformReadInput(struct InputRecordBuf *recs)
{ {
DWORD read_max = RingBufferEmpty(&IO_BUF_T, &recs->head); DWORD read_max = RingBufferEmpty(&IO_BUF_T, &recs->head);
union record win_buf[read_max]; union record win_buf[read_max];
@ -180,7 +175,7 @@ bool PlatformReadInput(struct InputRecordBuf *recs, struct InputStringBuf *str)
for (size_t i = 0; i < filled; i++) { for (size_t i = 0; i < filled; i++) {
struct InputRecord *rec = RingBufferNext(&IO_BUF_T, &recs->head); struct InputRecord *rec = RingBufferNext(&IO_BUF_T, &recs->head);
if (dispatch_rec(rec, str, &win_buf->native + i)) { if (dispatch_rec(rec, &win_buf->native + i)) {
rec->time = now; rec->time = now;
recs->head.len += 1; recs->head.len += 1;
} }

View file

@ -45,17 +45,25 @@ int main()
if (!BeginInputThread(&input_hand, &ctrl.recs, &ctrl.str)) if (!BeginInputThread(&input_hand, &ctrl.recs, &ctrl.str))
ErrorExit("Input handle failed to initialize"); ErrorExit("Input handle failed to initialize");
_sleep(1000000);
while (true) { while (true) {
if (!InputAquire(&input_hand)) if (!InputAquire(&input_hand))
ErrorExit("Aquire failed"); ErrorExit("Aquire failed");
//printf("%u\n", ctrl.recs.head.len); CtrlPoll(&ctrl);
//CtrlPoll(&ctrl);
struct InputAxis *a = CtrlGet(&ctrl, 0, BUTTON);
printf("%u", a);
printf("\t%u\n", a->is_down);
char silly[100] = { 0 };
CtrlInputString(&ctrl, 100, silly);
printf("%s\n", silly);
if (!InputRelease(&input_hand)) if (!InputRelease(&input_hand))
ErrorExit("Release failed"); ErrorExit("Release failed");
_sleep(100);
EventInvokeUpdate(&game.Update, 0); EventInvokeUpdate(&game.Update, 0);
} }

BIN
test.exe

Binary file not shown.