diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 4fa3689..ae83481 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -3,7 +3,8 @@ { "name": "Win32", "includePath": [ - "${workspaceFolder}/**" + "${workspaceFolder}/**", + "${workspaceFolder}/source/fumoengine/input" ], "defines": [ "_DEBUG", diff --git a/Fumofumotris.code-workspace b/Fumofumotris.code-workspace index 8400082..76dc364 100644 --- a/Fumofumotris.code-workspace +++ b/Fumofumotris.code-workspace @@ -57,7 +57,8 @@ "streambuf": "cpp", "typeinfo": "cpp", "execution": "cpp", - "stdbool.h": "c" + "stdbool.h": "c", + "fumoengine.h": "c" } } } \ No newline at end of file diff --git a/source/game/gametime.c b/source/fumoengine/fumocommon.c similarity index 75% rename from source/game/gametime.c rename to source/fumoengine/fumocommon.c index 50d9b43..fd256b3 100644 --- a/source/game/gametime.c +++ b/source/fumoengine/fumocommon.c @@ -1,7 +1,11 @@ -#include "gametime.h" +#include "fumocommon.h" #include -#define ONE_E_9 1000000000 + +size_t MinSize(size_t a, size_t b) +{ + return a < b ? a : b; +} Time TimeNow() { diff --git a/source/fumotris.h b/source/fumoengine/fumocommon.h similarity index 52% rename from source/fumotris.h rename to source/fumoengine/fumocommon.h index 4fb5281..80603da 100644 --- a/source/fumotris.h +++ b/source/fumoengine/fumocommon.h @@ -1,9 +1,10 @@ #pragma once -#include #include #define nullptr ((void *)0) +#define ONE_E_9 1000000000 + typedef uint8_t u8; typedef uint_fast8_t u8f; @@ -31,47 +32,11 @@ typedef int64_t i64; typedef int_fast64_t i64f; -size_t min_size(size_t a, size_t b); +typedef u64 Time; -/*const u8 I[16] = { - 0, 0, 0, 0, - 0, 0, 0, 0, - 1, 1, 1, 1, - 0, 0, 0, 0 -}; +size_t MinSize(size_t a, size_t b); -const u8 O[4] = { - 1, 1, - 1, 1 -}; +Time TimeNow(); -const u8 T[9] = { - 0, 1, 0, - 1, 1, 1, - 0, 0, 0 -}; - -const u8 S[9] = { - 0, 1, 1, - 1, 1, 0, - 0, 0, 0 -}; - -const u8 Z[9] = { - 1, 1, 0, - 0, 1, 1, - 0, 0, 0 -}; - -const u8 J[9] = { - 1, 0, 0, - 1, 1, 1, - 0, 0, 0 -}; - -const u8 L[9] = { - 0, 0, 1, - 1, 1, 1, - 0, 0, 0 -};*/ \ No newline at end of file +double TimeNowD(); \ No newline at end of file diff --git a/source/main.c b/source/fumoengine/fumoengine.c similarity index 51% rename from source/main.c rename to source/fumoengine/fumoengine.c index 56c9fb5..be46ffb 100644 --- a/source/main.c +++ b/source/fumoengine/fumoengine.c @@ -1,17 +1,8 @@ -#include -#include -#include -#include -#include -#include -#include - #include "ctrl.h" -#include "input.h" -#include "fumotris.h" -#include "term.h" + #include "event.h" -#include "platform.h" +#include "fumocommon.h" + void ErrorExit(char *message) { @@ -19,32 +10,30 @@ void ErrorExit(char *message) exit(1); } -struct Game { - struct Event Start; - struct Event Draw; - struct Event Update; +struct FumoGame { + struct Controller ctrl; + struct InputHandle input_hand; + + struct Event start; + struct Event draw; + struct Event update; Time time; }; -struct Event hi; - -int main() +bool FumoEngineInit(struct FumoGame *game) { if (!PlatformInit()) ErrorExit("Platform failed to initialize"); - struct Controller ctrl; - if (!CreateCtrl(&ctrl)) + if (!CreateCtrl(&game->ctrl)) ErrorExit("Out of memory"); - CtrlMap(&ctrl, 0, BUTTON, 'A'); + //CtrlMap(&ctrl, 0, BUTTON, 'A'); - struct Game game; - CreateEvent(&game.Update); + CreateEvent(&game->update); - struct InputHandle input_hand; - if (!BeginInputThread(&input_hand, &ctrl.recs, &ctrl.str)) + if (!BeginInputThread(&game->input_hand, &game->input_hand.recs, &ctrl.str)) ErrorExit("Input handle failed to initialize"); while (true) { @@ -56,9 +45,7 @@ int main() if (!InputRelease(&input_hand)) ErrorExit("Release failed"); - _sleep(100); - - EventInvokeUpdate(&game.Update, 0); + EventInvokeUpdate(&game.update, 0); } return 0; diff --git a/source/game/event.c b/source/fumoengine/include/event.c similarity index 100% rename from source/game/event.c rename to source/fumoengine/include/event.c diff --git a/source/game/event.h b/source/fumoengine/include/event.h similarity index 81% rename from source/game/event.h rename to source/fumoengine/include/event.h index 5e4182a..2145d1a 100644 --- a/source/game/event.h +++ b/source/fumoengine/include/event.h @@ -5,8 +5,8 @@ #include #include -#include "fumotris.h" -#include "gametime.h" +#include "fumocommon.h" + union func { void (*generic)(void *arg); diff --git a/source/misc/hash.c b/source/fumoengine/include/hash.c similarity index 92% rename from source/misc/hash.c rename to source/fumoengine/include/hash.c index 5c5ee3b..dd37526 100644 --- a/source/misc/hash.c +++ b/source/fumoengine/include/hash.c @@ -4,7 +4,8 @@ #include #include -#include "fumotris.h" +#include "fumocommon.h" + typedef u32 hashtype; diff --git a/source/misc/hash.h b/source/fumoengine/include/hash.h similarity index 68% rename from source/misc/hash.h rename to source/fumoengine/include/hash.h index 3db0175..3e3b328 100644 --- a/source/misc/hash.h +++ b/source/fumoengine/include/hash.h @@ -5,7 +5,8 @@ #include #include -#include "fumotris.h" +#include "fumocommon.h" + typedef u32 hashtype; diff --git a/source/datastructures/ringbuffer.c b/source/fumoengine/include/ringbuffer.c similarity index 93% rename from source/datastructures/ringbuffer.c rename to source/fumoengine/include/ringbuffer.c index de2e8a3..19c9496 100644 --- a/source/datastructures/ringbuffer.c +++ b/source/fumoengine/include/ringbuffer.c @@ -1,6 +1,8 @@ #include "ringbuffer.h" + #include + void *get_ptr(RingBufferT T, struct RingBufferHead *head, size_t i) { u8 *bytes = (u8 *)head; @@ -35,7 +37,7 @@ void RingBufferTransfer( struct RingBufferHead *dest, struct RingBufferHead *tmp ) { - size_t copy_max = min_size(T->LEN - dest->len, tmp->len); + size_t copy_max = MinSize(T->LEN - dest->len, tmp->len); for (size_t i = 0; i < copy_max; i++) { void *to = RingBufferGet(T, dest, dest->len + i); @@ -53,7 +55,7 @@ size_t RingBufferOut( void *dest, struct RingBufferHead *src ) { - size_t copy_max = min_size(n, src->len); + size_t copy_max = MinSize(n, src->len); for (size_t i = 0; i < copy_max; i++) { void *to = (u8 *)dest + i * T->SIZE; diff --git a/source/datastructures/ringbuffer.h b/source/fumoengine/include/ringbuffer.h similarity index 92% rename from source/datastructures/ringbuffer.h rename to source/fumoengine/include/ringbuffer.h index 267aa3f..67627fa 100644 --- a/source/datastructures/ringbuffer.h +++ b/source/fumoengine/include/ringbuffer.h @@ -2,10 +2,9 @@ #include #include #include -#include -#include -#include "fumotris.h" +#include "fumocommon.h" + typedef const struct RingBufferT { size_t OFFSET; diff --git a/source/io/ctrl.c b/source/fumoengine/input/ctrl.c similarity index 92% rename from source/io/ctrl.c rename to source/fumoengine/input/ctrl.c index e99ce18..5eea0b9 100644 --- a/source/io/ctrl.c +++ b/source/fumoengine/input/ctrl.c @@ -4,6 +4,7 @@ #define INIT_SIZE 16 + bool CreateCtrl(struct Controller *ctrl) { struct ctrl_bkt *code_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt)); @@ -14,7 +15,6 @@ bool CreateCtrl(struct Controller *ctrl) return false; *ctrl = (struct Controller) { - .recs.head = RINGBUF_HEAD_INIT, .pending_buf.len = 0, .axis_vec = (struct ctrl_axis_vec) { @@ -144,7 +144,7 @@ void dispatch_update(struct InputAxis *axis, struct InputRecord *rec) axis->data = rec->data; } -void CtrlPoll(struct Controller *ctrl) +void CtrlPoll(struct Controller *ctrl, struct InputRecordBuf *recs) { for (size_t i = 0; i < ctrl->pending_buf.len; i++) { struct InputAxis *axis = ctrl->pending_buf.axes[i]; @@ -154,8 +154,8 @@ void CtrlPoll(struct Controller *ctrl) } ctrl->pending_buf.len = 0; - for (size_t i = 0; i < ctrl->recs.head.len; i++) { - struct InputRecord *rec = &ctrl->recs.buf[i]; + for (size_t i = 0; i < recs->head.len; i++) { + struct InputRecord *rec = &recs->buf[i]; struct InputAxis *axis = find_axis(&ctrl->binds, rec->id); if (axis == nullptr) @@ -165,12 +165,7 @@ void CtrlPoll(struct Controller *ctrl) ctrl->pending_buf.axes[ctrl->pending_buf.len++] = axis; } - ctrl->recs.head.len = 0; -} - -size_t CtrlInputString(struct Controller *ctrl, size_t n, char *buf) -{ - return RingBufferOut(&STR_BUF_T, n, buf, &ctrl->str.head); + recs->head.len = 0; } /*int main() diff --git a/source/io/ctrl.h b/source/fumoengine/input/ctrl.h similarity index 88% rename from source/io/ctrl.h rename to source/fumoengine/input/ctrl.h index 875475a..595085d 100644 --- a/source/io/ctrl.h +++ b/source/fumoengine/input/ctrl.h @@ -5,10 +5,10 @@ #include #include -#include "fumotris.h" -#include "gametime.h" +#include "fumocommon.h" #include "input.h" + struct InputAxis { Time last_pressed; Time last_released; @@ -47,9 +47,6 @@ struct ctrl_axis_vec { }; struct Controller { - struct InputRecordBuf recs; - struct InputStringBuf str; - struct { struct InputAxis *axes[IO_BUF_SIZE]; u8f len; @@ -68,9 +65,7 @@ bool CtrlMap(struct Controller *ctrl, u16f code, u16f type, u16f bind); struct InputAxis *CtrlGet(struct Controller *ctrl, u16f code, u16f type); -void CtrlPoll(struct Controller *ctrl); - -size_t CtrlInputString(struct Controller *ctrl, size_t n, char *buf); +void CtrlPoll(struct Controller *ctrl, struct InputRecordBuf *recs); enum ControlCode { LEFT, diff --git a/source/io/input.c b/source/fumoengine/input/input.c similarity index 95% rename from source/io/input.c rename to source/fumoengine/input/input.c index 24906c8..7882318 100644 --- a/source/io/input.c +++ b/source/fumoengine/input/input.c @@ -3,6 +3,7 @@ #include "platform.h" + const struct RingBufferT IO_BUF_T = { .OFFSET = offsetof(struct InputRecordBuf, buf), .SIZE = sizeof(struct InputRecord), @@ -15,6 +16,7 @@ const struct RingBufferT STR_BUF_T = { .LEN = STR_BUF_SIZE }; + void *input_worker(void *arg) { struct InputHandle *hand = arg; @@ -110,4 +112,9 @@ bool InputRelease(struct InputHandle *hand) return false; return true; +} + +size_t InputString(struct InputStringBuf *str, size_t n, char *buf) +{ + return RingBufferOut(&STR_BUF_T, n, buf, &str->head); } \ No newline at end of file diff --git a/source/io/input.h b/source/fumoengine/input/input.h similarity index 76% rename from source/io/input.h rename to source/fumoengine/input/input.h index d21d391..76ec18d 100644 --- a/source/io/input.h +++ b/source/fumoengine/input/input.h @@ -6,13 +6,17 @@ #include #include -#include "fumotris.h" -#include "gametime.h" +#include "fumocommon.h" #include "ringbuffer.h" #define IO_BUF_SIZE 16 #define STR_BUF_SIZE (IO_BUF_SIZE * 4) + +extern const struct RingBufferT IO_BUF_T; +extern const struct RingBufferT STR_BUF_T; + + enum InputType { BUTTON, AXIS, @@ -32,12 +36,15 @@ union InputID { u32 hash; }; + struct Button { u64 value; }; + struct Axis { i64 value; }; + struct Joystick { i32 x; i32 y; @@ -49,6 +56,7 @@ union InputData { struct Joystick input_js; }; + struct InputRecord { Time time; @@ -68,22 +76,20 @@ struct InputRecord { }; }; -extern const struct RingBufferT IO_BUF_T; -extern const struct RingBufferT STR_BUF_T; +struct InputBuffer { + struct { + struct RingBufferHead head; + struct InputRecord buf[IO_BUF_SIZE]; + } recs; -struct InputRecordBuf { - struct RingBufferHead head; - struct InputRecord buf[IO_BUF_SIZE]; -}; - -struct InputStringBuf { - struct RingBufferHead head; - char buf[STR_BUF_SIZE]; + struct { + struct RingBufferHead head; + char buf[STR_BUF_SIZE]; + } str; }; struct InputHandle { - struct InputRecordBuf *recs; - struct InputStringBuf *str; + struct InputBuffer in; pthread_t thread; pthread_mutex_t mutex; @@ -95,12 +101,13 @@ struct InputHandle { bool BeginInputThread( struct InputHandle *hand, - struct InputRecordBuf *in, - struct InputStringBuf *str + struct InputBuffer *in, ); bool EndInputThread(struct InputHandle *hand); bool InputAquire(struct InputHandle *hand); -bool InputRelease(struct InputHandle *hand); \ No newline at end of file +bool InputRelease(struct InputHandle *hand); + +size_t InputString(struct Controller *ctrl, size_t n, char *buf); \ No newline at end of file diff --git a/source/io/platforms/parseinput.c b/source/fumoengine/input/platforms/parseinput.c similarity index 99% rename from source/io/platforms/parseinput.c rename to source/fumoengine/input/platforms/parseinput.c index a627478..089fb02 100644 --- a/source/io/platforms/parseinput.c +++ b/source/fumoengine/input/platforms/parseinput.c @@ -1,5 +1,6 @@ #include "parseinput.h" + void ReadButton(struct InputRecord *rec, u16f bind, bool is_down) { rec->id = (union InputID) { .bind = bind, .type = BUTTON }; diff --git a/source/io/platforms/parseinput.h b/source/fumoengine/input/platforms/parseinput.h similarity index 83% rename from source/io/platforms/parseinput.h rename to source/fumoengine/input/platforms/parseinput.h index cb14119..48edcb4 100644 --- a/source/io/platforms/parseinput.h +++ b/source/fumoengine/input/platforms/parseinput.h @@ -4,9 +4,10 @@ #include #include -#include "fumotris.h" +#include "fumocommon.h" #include "input.h" + void ReadButton(struct InputRecord *rec, u16f bind, bool is_down); void ReadAxis(struct InputRecord *rec, u16f bind, u64 value); diff --git a/source/io/platforms/platform.h b/source/fumoengine/input/platforms/platform.h similarity index 80% rename from source/io/platforms/platform.h rename to source/fumoengine/input/platforms/platform.h index 0938bf1..291d9e0 100644 --- a/source/io/platforms/platform.h +++ b/source/fumoengine/input/platforms/platform.h @@ -5,13 +5,13 @@ #include #include -#include "fumotris.h" -#include "gametime.h" +#include "fumocommon.h" #ifdef _WIN32 #include "win.h" #endif + bool PlatformInit(); bool PlatformGetRefreshRate(u16f *out); diff --git a/source/io/platforms/win.c b/source/fumoengine/input/platforms/win.c similarity index 98% rename from source/io/platforms/win.c rename to source/fumoengine/input/platforms/win.c index 66efe67..57837d7 100644 --- a/source/io/platforms/win.c +++ b/source/fumoengine/input/platforms/win.c @@ -1,9 +1,6 @@ #include "win.h" - #include -#include "gametime.h" -#include "input.h" #include "parseinput.h" #include "ringbuffer.h" diff --git a/source/io/platforms/win.h b/source/fumoengine/input/platforms/win.h similarity index 83% rename from source/io/platforms/win.h rename to source/fumoengine/input/platforms/win.h index f8e9eb7..8625da5 100644 --- a/source/io/platforms/win.h +++ b/source/fumoengine/input/platforms/win.h @@ -5,4 +5,4 @@ #include #include -#include "fumotris.h" \ No newline at end of file +#include "fumocommon.h" \ No newline at end of file diff --git a/source/term/term.c b/source/fumoengine/terminal/term.c similarity index 98% rename from source/term/term.c rename to source/fumoengine/terminal/term.c index c829669..682e24a 100644 --- a/source/term/term.c +++ b/source/fumoengine/terminal/term.c @@ -1,13 +1,12 @@ #include -#include #include #include #include #include #include -#include -#include "fumotris.h" +#include "fumocommon.h" + struct TChar4 { char ch; diff --git a/source/term/term.h b/source/fumoengine/terminal/term.h similarity index 87% rename from source/term/term.h rename to source/fumoengine/terminal/term.h index 7d27978..eb1b345 100644 --- a/source/term/term.h +++ b/source/fumoengine/terminal/term.h @@ -6,9 +6,9 @@ #include #include #include -#include -#include "fumotris.h" +#include "fumocommon.h" + struct TChar4 { char ch; diff --git a/source/fumotris.c b/source/fumotris.c index ac5853f..cad8a4e 100644 --- a/source/fumotris.c +++ b/source/fumotris.c @@ -1,6 +1,43 @@ -#include "fumotris.h" -size_t min_size(size_t a, size_t b) -{ - return a < b ? a : b; -} \ No newline at end of file + +/*const u8 I[16] = { + 0, 0, 0, 0, + 0, 0, 0, 0, + 1, 1, 1, 1, + 0, 0, 0, 0 +}; + +const u8 O[4] = { + 1, 1, + 1, 1 +}; + +const u8 T[9] = { + 0, 1, 0, + 1, 1, 1, + 0, 0, 0 +}; + +const u8 S[9] = { + 0, 1, 1, + 1, 1, 0, + 0, 0, 0 +}; + +const u8 Z[9] = { + 1, 1, 0, + 0, 1, 1, + 0, 0, 0 +}; + +const u8 J[9] = { + 1, 0, 0, + 1, 1, 1, + 0, 0, 0 +}; + +const u8 L[9] = { + 0, 0, 1, + 1, 1, 1, + 0, 0, 0 +};*/ \ No newline at end of file diff --git a/source/fumo/tetr.c b/source/fumotris/tetr.c similarity index 98% rename from source/fumo/tetr.c rename to source/fumotris/tetr.c index c579f0d..5681afa 100644 --- a/source/fumo/tetr.c +++ b/source/fumotris/tetr.c @@ -6,9 +6,10 @@ #include #include -#include "fumotris.h" +#include "fumocommon.h" #include "term.h" + struct TetrMap { size_t wid; size_t hgt; diff --git a/source/fumo/tetr.h b/source/fumotris/tetr.h similarity index 90% rename from source/fumo/tetr.h rename to source/fumotris/tetr.h index 896e510..57b7eb4 100644 --- a/source/fumo/tetr.h +++ b/source/fumotris/tetr.h @@ -6,9 +6,10 @@ #include #include -#include "fumotris.h" +#include "fumocommon.h" #include "term.h" + struct TetrMap { size_t wid; size_t hgt; diff --git a/source/game/gametime.h b/source/game/gametime.h deleted file mode 100644 index 630a436..0000000 --- a/source/game/gametime.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include -#include - -#include "fumotris.h" - -typedef u64 Time; - -Time TimeNow(); - -double TimeNowD(); diff --git a/test.exe b/test.exe index c3eb1ce..a855f5e 100644 Binary files a/test.exe and b/test.exe differ