Fumofumotris/source/fumoengine/fumoengine.c

84 lines
1.9 KiB
C
Raw Normal View History

2024-05-02 22:17:37 +00:00
#include "fumoengine.h"
#include "platform.h"
2024-04-30 21:41:31 +00:00
2024-03-25 05:34:59 +00:00
2024-05-15 05:44:13 +00:00
const VectorT FUMOCO_T = VECTOR_T(struct FumoCoroutine);
2024-05-06 05:52:30 +00:00
void Panic(char *message)
2024-04-19 20:23:11 +00:00
{
printf(message);
exit(1);
}
2024-05-07 22:10:15 +00:00
bool CreateFumoInstance(struct FumoInstance *instance)
2024-04-09 07:00:48 +00:00
{
2024-04-19 20:23:11 +00:00
if (!PlatformInit())
2024-05-06 05:52:30 +00:00
Panic("Platform failed to initialize");
2024-04-19 20:23:11 +00:00
2024-05-07 22:10:15 +00:00
if (!CreateController(&instance->ctrl))
2024-05-06 05:52:30 +00:00
Panic("Out of memory");
2024-04-03 23:31:47 +00:00
2024-05-07 22:10:15 +00:00
if (!CreateInputThread(&instance->input_hand))
Panic("Input handle failed to initialize");
if (!CreateTerminal(&instance->term, 20, 10))
2024-05-06 05:52:30 +00:00
Panic("Out of memory");
2024-04-22 22:13:13 +00:00
2024-05-07 22:10:15 +00:00
if (!CreateEvent(&instance->on_start))
Panic("Out of memory");
2024-05-08 12:47:46 +00:00
if (!CreateEvent(&instance->on_update))
2024-05-07 22:10:15 +00:00
Panic("Out of memory");
instance->time = TimeNow();
return true;
}
2024-05-08 12:47:46 +00:00
bool FumoInstanceRun(struct FumoInstance *inst)
2024-05-07 22:10:15 +00:00
{
2024-05-08 12:47:46 +00:00
EventInvoke(&inst->on_start, inst);
usize buf_n = TerminalMaxOut(&inst->term);
2024-05-07 22:10:15 +00:00
char *buf = malloc(buf_n);
while (true) {
2024-05-15 05:44:13 +00:00
// Time
nsec now = TimeNow();
inst->frametime = now - inst->time;
inst->time = now;
// Input
2024-05-08 12:47:46 +00:00
if (!InputAquire(&inst->input_hand))
2024-05-07 22:10:15 +00:00
Panic("Aquire failed");
2024-05-08 12:47:46 +00:00
ControllerPoll(&inst->ctrl, &inst->input_hand.recs);
2024-05-07 22:10:15 +00:00
2024-05-08 12:47:46 +00:00
if (!InputRelease(&inst->input_hand))
2024-05-07 22:10:15 +00:00
Panic("Release failed");
2024-05-14 04:20:20 +00:00
2024-05-15 05:44:13 +00:00
// Update
2024-05-14 04:20:20 +00:00
EventInvoke(&inst->on_update, inst);
2024-05-15 05:44:13 +00:00
for (usize i = 0; i < inst->coroutines.len; i++) {
struct FumoCoroutine *co = VectorGet(FUMOCO_T, &inst->coroutines, i);
co->callback();
}
2024-05-07 22:10:15 +00:00
2024-05-15 05:44:13 +00:00
// Draw
EventInvoke(&inst->on_draw, inst);
2024-05-08 12:47:46 +00:00
TerminalPrint(&inst->term, buf, buf_n);
2024-05-07 22:10:15 +00:00
puts(buf);
2024-04-03 23:31:47 +00:00
2024-05-08 12:47:46 +00:00
//_sleep(100);
2024-05-07 22:10:15 +00:00
}
2024-05-15 05:44:13 +00:00
}
bool CoroutineAdd(struct FumoInstance *inst, handler callback, nsec period)
{
return VectorAdd(FUMOCO_T, &inst->coroutines, &(struct FumoCoroutine) {
.callback = callback,
.timer = 0,
.period = period
});
2024-03-25 05:34:59 +00:00
}