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"
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");
@ -49,15 +58,27 @@ 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
});
}

View file

@ -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;

View file

@ -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;
}

View file

@ -1,62 +1,79 @@
#include "fumotris.h"
#include <stdlib.h>
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);

View file

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

View file

@ -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;

View file

@ -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);