diff --git a/.vscode/settings.json b/.vscode/settings.json index 9070c03..94cdde1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -56,6 +56,8 @@ "stdlib.h": "c", "stdio.h": "c", "platform.h": "c", - "ringbuffer.h": "c" + "ringbuffer.h": "c", + "fumoengine.h": "c", + "fumocommon.h": "c" } } \ No newline at end of file diff --git a/source/fumoengine/fumocommon.h b/source/fumoengine/fumocommon.h index 80603da..1c077c0 100644 --- a/source/fumoengine/fumocommon.h +++ b/source/fumoengine/fumocommon.h @@ -6,6 +6,10 @@ #define ONE_E_9 1000000000 +typedef size_t usize; +typedef ptrdiff_t isize; + + typedef uint8_t u8; typedef uint_fast8_t u8f; @@ -22,14 +26,14 @@ typedef uint_fast64_t u64f; typedef int8_t i8; typedef int_fast8_t i8f; -typedef int16_t i16; -typedef int_fast16_t i16f; +typedef int16_t i16; +typedef int_fast16_t i16f; -typedef int32_t i32; -typedef int_fast32_t i32f; +typedef int32_t i32; +typedef int_fast32_t i32f; -typedef int64_t i64; -typedef int_fast64_t i64f; +typedef int64_t i64; +typedef int_fast64_t i64f; typedef u64 Time; diff --git a/source/fumoengine/fumoengine.c b/source/fumoengine/fumoengine.c index 4c030d4..a1d0db7 100644 --- a/source/fumoengine/fumoengine.c +++ b/source/fumoengine/fumoengine.c @@ -2,7 +2,7 @@ #include "platform.h" -void ErrorExit(char *message) +void Panic(char *message) { printf(message); exit(1); @@ -11,16 +11,16 @@ void ErrorExit(char *message) bool FumoInit(struct FumoGame *game) { if (!PlatformInit()) - ErrorExit("Platform failed to initialize"); + Panic("Platform failed to initialize"); if (!CreateController(&game->ctrl)) - ErrorExit("Out of memory"); + Panic("Out of memory"); if (!CreateEvent(&game->update)) - ErrorExit("Out of memory"); + Panic("Out of memory"); if (!BeginInputThread(&game->input_hand)) - ErrorExit("Input handle failed to initialize"); + Panic("Input handle failed to initialize"); return 0; } \ No newline at end of file diff --git a/source/fumoengine/fumoengine.h b/source/fumoengine/fumoengine.h index 9bbc60e..59e5a2e 100644 --- a/source/fumoengine/fumoengine.h +++ b/source/fumoengine/fumoengine.h @@ -17,7 +17,7 @@ struct FumoGame { }; -void ErrorExit(char *message); +void Panic(char *message); bool FumoInit(struct FumoGame *game); diff --git a/source/fumoengine/include/ringbuffer.h b/source/fumoengine/include/ringbuffer.h index 0ceacbd..57b0dde 100644 --- a/source/fumoengine/include/ringbuffer.h +++ b/source/fumoengine/include/ringbuffer.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include @@ -19,7 +20,10 @@ struct RingBufferHead { #define RINGBUF_T_INIT(RBUF_STRUCT, RBUF_ITEM, RBUF_LEN) \ (&(struct RingBufferT) { \ - .OFFSET = offsetof(RBUF_STRUCT, buf), \ + .OFFSET = offsetof(struct { \ + struct RingBufferHead head; \ + RBUF_ITEM item; \ + }, item), \ .SIZE = sizeof(RBUF_ITEM), \ .LEN = RBUF_LEN \ }) \ diff --git a/source/fumoengine/input/ctrl.c b/source/fumoengine/input/ctrl.c index c7b932e..a46df95 100644 --- a/source/fumoengine/input/ctrl.c +++ b/source/fumoengine/input/ctrl.c @@ -146,13 +146,14 @@ void dispatch_update(struct InputAxis *axis, struct InputRecord *rec) void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs) { - /*for (size_t i = 0; i < ctrl->pending_buf.len; i++) { + for (size_t i = 0; i < ctrl->pending_buf.len; i++) { struct InputAxis *axis = ctrl->pending_buf.axes[i]; axis->is_up = false; axis->is_down = false; } - ctrl->pending_buf.len = 0;*/ + + ctrl->pending_buf.len = 0; for (size_t i = 0; i < recs->head.len; i++) { struct InputRecord *rec = &recs->buf[i]; diff --git a/source/fumoengine/input/platforms/win.c b/source/fumoengine/input/platforms/win.c index e36b5bd..cfbbd15 100644 --- a/source/fumoengine/input/platforms/win.c +++ b/source/fumoengine/input/platforms/win.c @@ -161,7 +161,7 @@ bool dispatch_rec( size_t read_input(struct win_rec *buf, size_t n) { DWORD len; - if (!ReadConsoleInputW(win.input_hand, buf, n, &len)) + if (!ReadConsoleInputW(win.input_hand, (INPUT_RECORD *)buf, n, &len)) return 0; return len; diff --git a/source/fumoengine/terminal/term.c b/source/fumoengine/terminal/term.c index 682e24a..8840c31 100644 --- a/source/fumoengine/terminal/term.c +++ b/source/fumoengine/terminal/term.c @@ -1,30 +1,5 @@ -#include -#include -#include -#include -#include -#include +#include "term.h" -#include "fumocommon.h" - - -struct TChar4 { - char ch; - u8 bg : 4; - u8 fg : 4; -}; - -struct Terminal { - size_t wid; - size_t hgt; - size_t area; - struct TChar4 *blks; - - size_t buf_size; - char *buf; - - u16f refresh_rate; -}; size_t term_buf_size(size_t area, size_t hgt) { @@ -34,61 +9,30 @@ size_t term_buf_size(size_t area, size_t hgt) return reset_str_len + (max_color_str_len + 1) * area + hgt + 1; } -struct TChar4 *alloc_blks(size_t area) -{ - return calloc(area, sizeof(struct TChar4)); -} - -char *alloc_buf(size_t buf_size) -{ - return malloc(buf_size); -} - -bool NewTerm(struct Terminal *term, size_t wid, size_t hgt) +bool CreateTerminal(struct Terminal *term, size_t wid, size_t hgt) { size_t area = wid * hgt; size_t buf_size = term_buf_size(area, hgt); - struct TChar4 *blks = alloc_blks(area); - char *buf = alloc_buf(buf_size); + struct Char4 *chs = calloc(area, sizeof(struct Char4)); - if (blks == nullptr or buf == nullptr) + if (chs == nullptr) return false; *term = (struct Terminal) { .wid = wid, .hgt = hgt, .area = area, - .blks = blks, - .buf_size = buf_size, - .buf = buf, - - .refresh_rate = 60, + .chs = chs, }; + return true; } -bool ResizeTerm(struct Terminal *term, size_t wid, size_t hgt) +void FreeTerminal(struct Terminal *term) { - size_t area = wid * hgt; - size_t buf_size = term_buf_size(area, hgt); - - struct TChar4 *tchs = realloc(term->blks, area * sizeof(struct TChar4)); - char *buf = realloc(term->buf, buf_size); - - if (tchs == nullptr or buf == nullptr) - return false; - - term->blks = tchs; - term->buf = buf; - return true; -} - -void FreeTerm(struct Terminal *term) -{ - free(term->blks); - free(term->buf); + free(term->chs); } size_t u8_to_buf(char *buf, u8f x) @@ -124,7 +68,7 @@ size_t u8_to_buf(char *buf, u8f x) return len; } -size_t tch4_dif_to_buf(char *buf, struct TChar4 *dif, struct TChar4 *blk) +size_t ch4_dif_to_buf(char *buf, struct Char4 *dif, struct Char4 *blk) { size_t len = 0; @@ -159,9 +103,9 @@ size_t tch4_dif_to_buf(char *buf, struct TChar4 *dif, struct TChar4 *blk) return len; } -size_t TermOut(struct Terminal *term) +size_t TerminalPrint(char *dest, size_t n, struct Terminal *term) { - struct TChar4 dif; + struct Char4 dif; size_t len = 7; memcpy(term->buf, "\x1b[H\x1b[0m", 7); @@ -169,14 +113,14 @@ size_t TermOut(struct Terminal *term) for (size_t y = 0; y < term->hgt; y++) { for (size_t x = 0; x < term->wid; x++) { size_t i = y * term->wid + x; - struct TChar4 *blk = &term->blks[i]; + struct Char4 *blk = &term->chs[i]; // DEBUG if (blk->ch == 0) blk->ch = '#'; // DEBUG - len += tch4_dif_to_buf(term->buf + len, &dif, blk); + len += ch4_dif_to_buf(term->buf + len, &dif, blk); } term->buf[len++] = '\n'; } diff --git a/source/fumoengine/terminal/term.h b/source/fumoengine/terminal/term.h index eb1b345..082f50d 100644 --- a/source/fumoengine/terminal/term.h +++ b/source/fumoengine/terminal/term.h @@ -1,16 +1,14 @@ #pragma once #include -#include #include #include #include #include -#include #include "fumocommon.h" -struct TChar4 { +struct Char4 { char ch; u8 bg : 4; u8 fg : 4; @@ -20,18 +18,12 @@ struct Terminal { size_t wid; size_t hgt; size_t area; - struct TChar4 *blks; - size_t buf_size; - char *buf; - - u16f refresh_rate; + struct Char4 *chs; }; -bool NewTerm(struct Terminal *term, size_t wid, size_t hgt); +bool CreateTerminal(struct Terminal *term, size_t wid, size_t hgt); -bool ResizeTerm(struct Terminal *term, size_t wid, size_t hgt); +void FreeTerminal(struct Terminal *term); -void FreeTerm(struct Terminal *term); - -size_t TermOut(struct Terminal *term); \ No newline at end of file +size_t TerminalPrint(char *dest, size_t n, struct Terminal *term); \ No newline at end of file diff --git a/source/fumotris/fumotris.c b/source/fumotris/fumotris.c index 06946d7..6127712 100644 --- a/source/fumotris/fumotris.c +++ b/source/fumotris/fumotris.c @@ -40,14 +40,18 @@ void Loop(struct FumoGame *game) game->time = TimeNow(); if (!InputAquire(&game->input_hand)) - ErrorExit("Aquire failed"); + Panic("Aquire failed"); - //ControllerPoll(&game->ctrl, &game->input_hand.recs); + ControllerPoll(&game->ctrl, &game->input_hand.recs); + u64 val = ControllerGet(&game->ctrl, LEFT, BUTTON)->is_down; + printf("%u\n", val); if (!InputRelease(&game->input_hand)) - ErrorExit("Release failed"); + Panic("Release failed"); - //EventInvokeUpdate(&game->update, 0); + EventInvokeUpdate(&game->update, 0); + + _sleep(100); } } diff --git a/source/fumotris/tetr.c b/source/fumotris/tetr.c index 5681afa..816d8fd 100644 --- a/source/fumotris/tetr.c +++ b/source/fumotris/tetr.c @@ -43,8 +43,8 @@ void TetrMapToTermBuf(struct TetrMap *map, struct Terminal *term) size_t map_i = y * map->wid + x; size_t buf_i = (y + map->y) * term->wid + (x + map->x) * 2; - struct TChar4 *a = &term->blks[buf_i]; - struct TChar4 *b = &term->blks[buf_i + 1]; + struct Char4 *a = &term->chs[buf_i]; + struct Char4 *b = &term->chs[buf_i + 1]; if (map->blks[map_i] == 0) { a->ch = '('; diff --git a/test.exe b/test.exe index ada5cb8..cbb98a9 100644 Binary files a/test.exe and b/test.exe differ