diff --git a/source/datastructures/ringbuffer.c b/source/datastructures/ringbuffer.c index 720e4c7..de2e8a3 100644 --- a/source/datastructures/ringbuffer.c +++ b/source/datastructures/ringbuffer.c @@ -1,15 +1,10 @@ #include "ringbuffer.h" #include -struct ring_buf { - struct RingBufferHead head; - u8 bytes[]; -}; - void *get_ptr(RingBufferT T, struct RingBufferHead *head, size_t i) { - struct ring_buf *ring = (struct ring_buf *)head; - return ring->bytes + T->SIZE * i; + u8 *bytes = (u8 *)head; + return bytes + T->OFFSET + T->SIZE * i; } 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) { memcpy(RingBufferNext(T, dest), item, T->SIZE); + dest->len += 1; } void RingBufferTransfer( RingBufferT T, 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++) { - void *to = RingBufferGet(T, dest_head, dest->len + i); - void *from = RingBufferGet(T, src_head, i); + void *to = RingBufferGet(T, dest, dest->len + i); + void *from = RingBufferGet(T, tmp, i); memcpy(to, from, T->SIZE); } 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) -{ - struct ring_buf *src = src_head; - - size_t copy_max = min_size(n, src->head.len); +size_t RingBufferOut( + RingBufferT T, + size_t n, + void *dest, + struct RingBufferHead *src +) { + size_t copy_max = min_size(n, src->len); 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); memcpy(to, from, T->SIZE); } + src->len -= copy_max; + return copy_max; } \ No newline at end of file diff --git a/source/datastructures/ringbuffer.h b/source/datastructures/ringbuffer.h index 8038491..267aa3f 100644 --- a/source/datastructures/ringbuffer.h +++ b/source/datastructures/ringbuffer.h @@ -8,13 +8,14 @@ #include "fumotris.h" typedef const struct RingBufferT { - size_t LEN; + size_t OFFSET; size_t SIZE; + size_t LEN; } *const RingBufferT; struct RingBufferHead { - u8f len; - u8f start; + size_t len; + size_t start; }; #define RINGBUF_HEAD_INIT ((struct RingBufferHead) { 0, 0 }) @@ -29,6 +30,8 @@ void *RingBufferGet( void *RingBufferNext(RingBufferT T, struct RingBufferHead *head); +void RingBufferAdd(RingBufferT T, struct RingBufferHead *dest, void *item); + void RingBufferTransfer( RingBufferT T, struct RingBufferHead *dest, diff --git a/source/io/ctrl.c b/source/io/ctrl.c index 2fda0ef..affe3ed 100644 --- a/source/io/ctrl.c +++ b/source/io/ctrl.c @@ -156,9 +156,10 @@ void CtrlPoll(struct Controller *ctrl) for (size_t i = 0; i < ctrl->recs.head.len; 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); - if (axis == nullptr) continue; diff --git a/source/io/input.c b/source/io/input.c index a1c5300..9440bff 100644 --- a/source/io/input.c +++ b/source/io/input.c @@ -4,13 +4,15 @@ #include "platform.h" const struct RingBufferT IO_BUF_T = { - .LEN = IO_BUF_SIZE, - .SIZE = sizeof(struct InputRecord) + .OFFSET = offsetof(struct InputRecordBuf, buf), + .SIZE = sizeof(struct InputRecord), + .LEN = IO_BUF_SIZE }; const struct RingBufferT STR_BUF_T = { - .LEN = STR_BUF_SIZE, - .SIZE = sizeof(char) + .OFFSET = offsetof(struct InputStringBuf, buf), + .SIZE = sizeof(char), + .LEN = STR_BUF_SIZE }; void *input_worker(void *arg) @@ -18,20 +20,13 @@ void *input_worker(void *arg) struct InputHandle *hand = arg; struct InputRecordBuf tmp_recs = { .head.len = 0, .head.start = 0 }; - struct InputStringBuf tmp_str = { .head.len = 0, .head.start = 0 }; while (!hand->is_terminating) { - if (!PlatformReadInput(&tmp_recs, &tmp_str)) { + if (!PlatformReadInput(&tmp_recs)) { hand->err = true; 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) { hand->err = true; return nullptr; @@ -45,7 +40,6 @@ void *input_worker(void *arg) } 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) { hand->err = true; diff --git a/source/io/platforms/platform.h b/source/io/platforms/platform.h index 0938bf1..f4dd9ca 100644 --- a/source/io/platforms/platform.h +++ b/source/io/platforms/platform.h @@ -16,7 +16,7 @@ bool PlatformInit(); bool PlatformGetRefreshRate(u16f *out); -bool PlatformReadInput(struct InputRecordBuf *in, struct InputStringBuf *str); +bool PlatformReadInput(struct InputRecordBuf *in); bool PlatformStopInput(); diff --git a/source/io/platforms/win.c b/source/io/platforms/win.c index 3f78519..f6b91cf 100644 --- a/source/io/platforms/win.c +++ b/source/io/platforms/win.c @@ -125,20 +125,15 @@ bool PlatformGetRefreshRate(u16f *out) return true; } -bool dispatch_rec( - struct InputRecord *out, - struct InputStringBuf *str, - struct win_rec *rec -) { - u8f type = rec->type | (rec->is_mouse & rec->mouse.flags); +bool dispatch_rec(struct InputRecord *out, struct win_rec *rec) { + u8f type = rec->type + | (rec->is_mouse & rec->mouse.flags) + | (rec->is_key & rec->key.is_down); switch (type) { case KEY_EVENT: { 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; } case MOUSE_MOVE: { @@ -166,7 +161,7 @@ bool dispatch_rec( return false; } -bool PlatformReadInput(struct InputRecordBuf *recs, struct InputStringBuf *str) +bool PlatformReadInput(struct InputRecordBuf *recs) { DWORD read_max = RingBufferEmpty(&IO_BUF_T, &recs->head); 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++) { 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; recs->head.len += 1; } diff --git a/source/main.c b/source/main.c index 359551a..3d1091f 100644 --- a/source/main.c +++ b/source/main.c @@ -45,17 +45,25 @@ int main() if (!BeginInputThread(&input_hand, &ctrl.recs, &ctrl.str)) ErrorExit("Input handle failed to initialize"); - _sleep(1000000); while (true) { if (!InputAquire(&input_hand)) 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)) ErrorExit("Release failed"); + _sleep(100); + EventInvokeUpdate(&game.Update, 0); } diff --git a/test.exe b/test.exe index 978fa63..cc8b6c2 100644 Binary files a/test.exe and b/test.exe differ