From e3ed625feb6c4e75b6a1420afcc601e285b1b235 Mon Sep 17 00:00:00 2001 From: Julia <145168563+julia-aph@users.noreply.github.com> Date: Wed, 15 May 2024 00:44:13 -0500 Subject: [PATCH] sd sdfg --- source/fumoengine/fumoengine.c | 29 ++++++++-- source/fumoengine/fumoengine.h | 9 ++++ source/fumoengine/include/vector.c | 8 ++- source/fumotris/fumotris.c | 87 ++++++++++++++++++------------ source/fumotris/fumotris.h | 6 --- source/fumotris/tetra.c | 8 +-- source/fumotris/tetra.h | 10 +++- 7 files changed, 104 insertions(+), 53 deletions(-) diff --git a/source/fumoengine/fumoengine.c b/source/fumoengine/fumoengine.c index 1217379..0ed53bd 100644 --- a/source/fumoengine/fumoengine.c +++ b/source/fumoengine/fumoengine.c @@ -2,6 +2,9 @@ #include "platform.h" +const VectorT FUMOCO_T = VECTOR_T(struct FumoCoroutine); + + void Panic(char *message) { printf(message); @@ -41,6 +44,12 @@ bool FumoInstanceRun(struct FumoInstance *inst) char *buf = malloc(buf_n); while (true) { + // Time + nsec now = TimeNow(); + inst->frametime = now - inst->time; + inst->time = now; + + // Input if (!InputAquire(&inst->input_hand)) Panic("Aquire failed"); @@ -48,16 +57,28 @@ bool FumoInstanceRun(struct FumoInstance *inst) if (!InputRelease(&inst->input_hand)) Panic("Release failed"); - - nsec now = TimeNow(); - inst->frametime = now - inst->time; - inst->time = now; + // Update EventInvoke(&inst->on_update, inst); + for (usize i = 0; i < inst->coroutines.len; i++) { + struct FumoCoroutine *co = VectorGet(FUMOCO_T, &inst->coroutines, i); + co->callback(); + } + // Draw + EventInvoke(&inst->on_draw, inst); TerminalPrint(&inst->term, buf, buf_n); puts(buf); //_sleep(100); } +} + +bool CoroutineAdd(struct FumoInstance *inst, handler callback, nsec period) +{ + return VectorAdd(FUMOCO_T, &inst->coroutines, &(struct FumoCoroutine) { + .callback = callback, + .timer = 0, + .period = period + }); } \ No newline at end of file diff --git a/source/fumoengine/fumoengine.h b/source/fumoengine/fumoengine.h index 3c18458..0b277ae 100644 --- a/source/fumoengine/fumoengine.h +++ b/source/fumoengine/fumoengine.h @@ -7,6 +7,12 @@ #include "vector.h" +struct FumoCoroutine { + handler callback; + nsec timer; + nsec period; +}; + struct FumoInstance { struct Controller ctrl; struct InputHandle input_hand; @@ -14,6 +20,9 @@ struct FumoInstance { struct Event on_start; struct Event on_update; + struct Event on_draw; + + struct Vector coroutines; nsec time; nsec frametime; diff --git a/source/fumoengine/include/vector.c b/source/fumoengine/include/vector.c index 2db2bfd..19e1db7 100644 --- a/source/fumoengine/include/vector.c +++ b/source/fumoengine/include/vector.c @@ -10,11 +10,9 @@ bool CreateVector(VectorT T, struct Vector *vec) if (array == nullptr) return false; - *vec = (struct Vector) { - .len = 0, - .capacity = 16, - .array = array - }; + vec->len = 0; + vec->capacity = 16; + vec->array = array; return true; } diff --git a/source/fumotris/fumotris.c b/source/fumotris/fumotris.c index 3a91d4b..498641e 100644 --- a/source/fumotris/fumotris.c +++ b/source/fumotris/fumotris.c @@ -1,62 +1,79 @@ #include "fumotris.h" +#include struct Fumotris { struct Tetra board; struct Tetra piece; - nsec timer; + bool is_ground; }; +struct TetraTemplate *bag[7] = { &I, &O, &T, &S, &Z, &J, &L }; +usize bag_i = 0; + + +void shuffle() +{ + for (usize i = 6; i >= 0; i--) { + usize swap = rand() % i; + + struct TetraTemplate *tmp = bag[swap]; + bag[swap] = bag[i]; + bag[i] = tmp; + } +} + +void place_piece(struct Fumotris *fumo) +{ + TetraOverlay(&fumo->piece, &fumo->board); + SetTetra(&fumo->piece, bag[bag_i++], 0, 0); + + if (bag_i == 7) { + shuffle(); + bag_i = 0; + } +} + +i16 get_horizontal(struct Controller *ctrl) +{ + return (-(i16)ctrl->axes[LEFT].is_down) + ctrl->axes[RIGHT].is_down; +} + void FumotrisStart(struct FumoInstance *inst, struct Fumotris *fumo) { ControllerBindMulti(&inst->ctrl, BINDS_N, controls_g, codes_g, types_g); - CreateTetra(&fumo->board, 10, 10); - SetTetra(&fumo->piece, T, 3, 3, 0, 0); - fumo->timer = 0; + + + fumo->is_ground = false; + + SetTetra(&fumo->piece, bag[bag_i++], 0, 0); } void FumotrisUpdate(struct FumoInstance *inst, struct Fumotris *fumo) { - i16 horizontal = 0; - if (inst->ctrl.axes[LEFT].is_down) - horizontal -= 1; - if (inst->ctrl.axes[RIGHT].is_down) - horizontal += 1; - TetraMove(&fumo->piece, &fumo->board, horizontal, 0); + TetraMove(&fumo->piece, &fumo->board, get_horizontal(&inst->ctrl), 0); - - if (inst->ctrl.axes[SOFT_DROP].is_down) + if (inst->ctrl.axes[SOFT_DROP].is_down) { TetraMove(&fumo->piece, &fumo->board, 0, 1); - + } if (inst->ctrl.axes[HARD_DROP].is_down) { while (TetraMove(&fumo->piece, &fumo->board, 0, 1)); - - fumo->timer = 0; - TetraOverlay(&fumo->piece, &fumo->board); - SetTetra(&fumo->piece, I, 4, 4, 0, 0); - - return; + place_piece(fumo); } +} - fumo->timer += inst->frametime; - while (fumo->timer > 5e8) { - fumo->timer -= 5e8; - - if (!TetraMove(&fumo->piece, &fumo->board, 0, 1)) { - if (!fumo->is_ground) { - fumo->is_ground = true; - } else { - TetraOverlay(&fumo->piece, &fumo->board); - SetTetra(&fumo->piece, I, 4, 4, 0, 0); - - fumo->is_ground = false; - } - } +void FumotrisOnFall(struct FumoInstance *inst, struct Fumotris *fumo) +{ + if (!TetraMove(&fumo->piece, &fumo->board, 0, 1)) { + if (!fumo->is_ground) + fumo->is_ground = true; + else + place_piece(fumo); } } @@ -73,8 +90,12 @@ int main() CreateFumoInstance(&inst); struct Fumotris game; + EventAdd(&inst.on_start, FumotrisStart, &game); EventAdd(&inst.on_update, FumotrisUpdate, &game); + EventAdd(&inst.on_draw, FumotrisDraw, &game); + + VectorAdd(FUMOCO_T, &inst.coroutines, ); FumoInstanceRun(&inst); diff --git a/source/fumotris/fumotris.h b/source/fumotris/fumotris.h index 9b5a9e2..3aa2b06 100644 --- a/source/fumotris/fumotris.h +++ b/source/fumotris/fumotris.h @@ -22,12 +22,6 @@ enum FumotrisControls { MOUSE }; -struct TetraTemplate { - u8 *blks; - u16 wid; - u16 hgt; -}; - u16 controls_g[BINDS_N] = { LEFT, diff --git a/source/fumotris/tetra.c b/source/fumotris/tetra.c index cd823fc..890cf1f 100644 --- a/source/fumotris/tetra.c +++ b/source/fumotris/tetra.c @@ -24,11 +24,11 @@ void FreeTetra(struct Tetra *tetra) free(tetra->blks); } -void SetTetra(struct Tetra *map, u8 *blks, u16 wid, u16 hgt, i16 x, i16 y) +void SetTetra(struct Tetra *map, struct TetraTemplate *t, i16 x, i16 y) { - map->blks = blks; - map->wid = wid; - map->hgt = hgt; + map->blks = t->blks; + map->wid = t->wid; + map->hgt = t->hgt; map->x = x; map->y = y; diff --git a/source/fumotris/tetra.h b/source/fumotris/tetra.h index 800bbd3..f0838cc 100644 --- a/source/fumotris/tetra.h +++ b/source/fumotris/tetra.h @@ -16,11 +16,19 @@ struct Tetra { u8f rot; }; +struct TetraTemplate { + u8 *blks; + + u16 wid; + u16 hgt; +}; + + bool CreateTetra(struct Tetra *map, u16 wid, u16 hgt); void FreeTetra(struct Tetra *map); -void SetTetra(struct Tetra *map, u8 *blks, u16 wid, u16 hgt, i16 x, i16 y); +void SetTetra(struct Tetra *map, struct TetraTemplate *t, i16 x, i16 y); void TetraTerminalClear(struct Tetra *board, struct Terminal *term);