sdfg
This commit is contained in:
Julia 2024-05-15 00:44:13 -05:00
parent 582b157454
commit e3ed625feb
7 changed files with 104 additions and 53 deletions

View file

@ -2,6 +2,9 @@
#include "platform.h" #include "platform.h"
const VectorT FUMOCO_T = VECTOR_T(struct FumoCoroutine);
void Panic(char *message) void Panic(char *message)
{ {
printf(message); printf(message);
@ -41,6 +44,12 @@ bool FumoInstanceRun(struct FumoInstance *inst)
char *buf = malloc(buf_n); char *buf = malloc(buf_n);
while (true) { while (true) {
// Time
nsec now = TimeNow();
inst->frametime = now - inst->time;
inst->time = now;
// Input
if (!InputAquire(&inst->input_hand)) if (!InputAquire(&inst->input_hand))
Panic("Aquire failed"); Panic("Aquire failed");
@ -49,15 +58,27 @@ bool FumoInstanceRun(struct FumoInstance *inst)
if (!InputRelease(&inst->input_hand)) if (!InputRelease(&inst->input_hand))
Panic("Release failed"); Panic("Release failed");
nsec now = TimeNow(); // Update
inst->frametime = now - inst->time;
inst->time = now;
EventInvoke(&inst->on_update, inst); 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); TerminalPrint(&inst->term, buf, buf_n);
puts(buf); puts(buf);
//_sleep(100); //_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
});
}

View file

@ -7,6 +7,12 @@
#include "vector.h" #include "vector.h"
struct FumoCoroutine {
handler callback;
nsec timer;
nsec period;
};
struct FumoInstance { struct FumoInstance {
struct Controller ctrl; struct Controller ctrl;
struct InputHandle input_hand; struct InputHandle input_hand;
@ -14,6 +20,9 @@ struct FumoInstance {
struct Event on_start; struct Event on_start;
struct Event on_update; struct Event on_update;
struct Event on_draw;
struct Vector coroutines;
nsec time; nsec time;
nsec frametime; nsec frametime;

View file

@ -10,11 +10,9 @@ bool CreateVector(VectorT T, struct Vector *vec)
if (array == nullptr) if (array == nullptr)
return false; return false;
*vec = (struct Vector) { vec->len = 0;
.len = 0, vec->capacity = 16;
.capacity = 16, vec->array = array;
.array = array
};
return true; return true;
} }

View file

@ -1,62 +1,79 @@
#include "fumotris.h" #include "fumotris.h"
#include <stdlib.h>
struct Fumotris { struct Fumotris {
struct Tetra board; struct Tetra board;
struct Tetra piece; struct Tetra piece;
nsec timer;
bool is_ground; 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) void FumotrisStart(struct FumoInstance *inst, struct Fumotris *fumo)
{ {
ControllerBindMulti(&inst->ctrl, BINDS_N, controls_g, codes_g, types_g); ControllerBindMulti(&inst->ctrl, BINDS_N, controls_g, codes_g, types_g);
CreateTetra(&fumo->board, 10, 10); CreateTetra(&fumo->board, 10, 10);
SetTetra(&fumo->piece, T, 3, 3, 0, 0);
fumo->timer = 0;
fumo->is_ground = false; fumo->is_ground = false;
SetTetra(&fumo->piece, bag[bag_i++], 0, 0);
} }
void FumotrisUpdate(struct FumoInstance *inst, struct Fumotris *fumo) void FumotrisUpdate(struct FumoInstance *inst, struct Fumotris *fumo)
{ {
i16 horizontal = 0; TetraMove(&fumo->piece, &fumo->board, get_horizontal(&inst->ctrl), 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);
if (inst->ctrl.axes[SOFT_DROP].is_down) {
if (inst->ctrl.axes[SOFT_DROP].is_down)
TetraMove(&fumo->piece, &fumo->board, 0, 1); TetraMove(&fumo->piece, &fumo->board, 0, 1);
}
if (inst->ctrl.axes[HARD_DROP].is_down) { if (inst->ctrl.axes[HARD_DROP].is_down) {
while (TetraMove(&fumo->piece, &fumo->board, 0, 1)); while (TetraMove(&fumo->piece, &fumo->board, 0, 1));
place_piece(fumo);
fumo->timer = 0;
TetraOverlay(&fumo->piece, &fumo->board);
SetTetra(&fumo->piece, I, 4, 4, 0, 0);
return;
} }
}
fumo->timer += inst->frametime; void FumotrisOnFall(struct FumoInstance *inst, struct Fumotris *fumo)
while (fumo->timer > 5e8) { {
fumo->timer -= 5e8; if (!TetraMove(&fumo->piece, &fumo->board, 0, 1)) {
if (!fumo->is_ground)
if (!TetraMove(&fumo->piece, &fumo->board, 0, 1)) { fumo->is_ground = true;
if (!fumo->is_ground) { else
fumo->is_ground = true; place_piece(fumo);
} else {
TetraOverlay(&fumo->piece, &fumo->board);
SetTetra(&fumo->piece, I, 4, 4, 0, 0);
fumo->is_ground = false;
}
}
} }
} }
@ -73,8 +90,12 @@ int main()
CreateFumoInstance(&inst); CreateFumoInstance(&inst);
struct Fumotris game; struct Fumotris game;
EventAdd(&inst.on_start, FumotrisStart, &game); EventAdd(&inst.on_start, FumotrisStart, &game);
EventAdd(&inst.on_update, FumotrisUpdate, &game); EventAdd(&inst.on_update, FumotrisUpdate, &game);
EventAdd(&inst.on_draw, FumotrisDraw, &game);
VectorAdd(FUMOCO_T, &inst.coroutines, );
FumoInstanceRun(&inst); FumoInstanceRun(&inst);

View file

@ -22,12 +22,6 @@ enum FumotrisControls {
MOUSE MOUSE
}; };
struct TetraTemplate {
u8 *blks;
u16 wid;
u16 hgt;
};
u16 controls_g[BINDS_N] = { u16 controls_g[BINDS_N] = {
LEFT, LEFT,

View file

@ -24,11 +24,11 @@ void FreeTetra(struct Tetra *tetra)
free(tetra->blks); 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->blks = t->blks;
map->wid = wid; map->wid = t->wid;
map->hgt = hgt; map->hgt = t->hgt;
map->x = x; map->x = x;
map->y = y; map->y = y;

View file

@ -16,11 +16,19 @@ struct Tetra {
u8f rot; u8f rot;
}; };
struct TetraTemplate {
u8 *blks;
u16 wid;
u16 hgt;
};
bool CreateTetra(struct Tetra *map, u16 wid, u16 hgt); bool CreateTetra(struct Tetra *map, u16 wid, u16 hgt);
void FreeTetra(struct Tetra *map); 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); void TetraTerminalClear(struct Tetra *board, struct Terminal *term);