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-06 05:52:30 +00:00
|
|
|
void Panic(char *message)
|
2024-04-19 20:23:11 +00:00
|
|
|
{
|
|
|
|
printf(message);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-05-15 22:10:47 +00:00
|
|
|
bool CoroutineAdd(struct Instance *inst, void *state, coroutine_handler callback)
|
|
|
|
{
|
|
|
|
return VectorAdd(&inst->coroutines, &(struct Coroutine) {
|
|
|
|
.callback = callback,
|
|
|
|
.state = state,
|
|
|
|
.next_scheduled = inst->time
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoroutineTryInvoke(struct Instance *inst, struct Coroutine *co)
|
|
|
|
{
|
|
|
|
while (inst->time > co->next_scheduled) {
|
|
|
|
nsec wait = co->callback(inst, co->state);
|
|
|
|
if (wait == 0) {
|
|
|
|
co->next_scheduled = inst->time;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
co->next_scheduled += wait;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CreateFumoInstance(struct Instance *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 (!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");
|
|
|
|
|
2024-05-15 22:10:47 +00:00
|
|
|
if (!CreateEvent(&instance->on_draw))
|
|
|
|
Panic("Out of memory");
|
|
|
|
|
|
|
|
if (!CreateVector(&instance->coroutines, sizeof(struct Coroutine)))
|
|
|
|
Panic("Out of memory");
|
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
instance->time = TimeNow();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-15 22:10:47 +00:00
|
|
|
bool FumoInstanceRun(struct Instance *inst)
|
2024-05-07 22:10:15 +00:00
|
|
|
{
|
2024-05-08 12:47:46 +00:00
|
|
|
EventInvoke(&inst->on_start, inst);
|
|
|
|
|
2024-05-07 22:10:15 +00:00
|
|
|
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-15 22:10:47 +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 22:10:47 +00:00
|
|
|
|
2024-05-15 05:44:13 +00:00
|
|
|
for (usize i = 0; i < inst->coroutines.len; i++) {
|
2024-05-15 22:10:47 +00:00
|
|
|
CoroutineTryInvoke(inst, VectorGet(&inst->coroutines, i));
|
2024-05-15 05:44:13 +00:00
|
|
|
}
|
2024-05-07 22:10:15 +00:00
|
|
|
|
2024-05-15 05:44:13 +00:00
|
|
|
// Draw
|
|
|
|
EventInvoke(&inst->on_draw, inst);
|
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-03-25 05:34:59 +00:00
|
|
|
}
|