diff --git a/source/fumoengine/fumoengine.c b/source/fumoengine/fumoengine.c index 7f900cd..72c6e69 100644 --- a/source/fumoengine/fumoengine.c +++ b/source/fumoengine/fumoengine.c @@ -2,9 +2,6 @@ #include "platform.h" -VectorT FUMOSYS_VEC_T = VECTOR_T(struct FumoHook); - - void Panic(char *message) { printf(message); @@ -28,7 +25,7 @@ bool CreateFumoInstance(struct FumoInstance *instance) if (!CreateEvent(&instance->on_start)) Panic("Out of memory"); - if (!CreateEvent(FUMOSYS_VEC_T, &instance->on_update)) + if (!CreateEvent(&instance->on_update)) Panic("Out of memory"); instance->time = TimeNow(); @@ -36,28 +33,30 @@ bool CreateFumoInstance(struct FumoInstance *instance) return true; } -bool FumoInstanceRun(struct FumoInstance *instance) +bool FumoInstanceRun(struct FumoInstance *inst) { - usize buf_n = TerminalMaxOut(&instance->term); + EventInvoke(&inst->on_start, inst); + + usize buf_n = TerminalMaxOut(&inst->term); char *buf = malloc(buf_n); while (true) { - if (!InputAquire(&instance->input_hand)) + if (!InputAquire(&inst->input_hand)) Panic("Aquire failed"); - ControllerPoll(&instance->ctrl, &instance->input_hand.recs); + ControllerPoll(&inst->ctrl, &inst->input_hand.recs); - if (!InputRelease(&instance->input_hand)) + if (!InputRelease(&inst->input_hand)) Panic("Release failed"); nsec now = TimeNow(); - instance->frametime = now - instance->time; - FumoInvoke(instance, &instance->on_update); - instance->time = now; + inst->frametime = now - inst->time; + EventInvoke(&inst->on_update, inst); + inst->time = now; - TerminalPrint(&instance->term, buf, buf_n); + TerminalPrint(&inst->term, buf, buf_n); puts(buf); - _sleep(100); + //_sleep(100); } } \ No newline at end of file diff --git a/source/fumoengine/fumoengine.h b/source/fumoengine/fumoengine.h index 7997863..3c18458 100644 --- a/source/fumoengine/fumoengine.h +++ b/source/fumoengine/fumoengine.h @@ -24,6 +24,4 @@ void Panic(char *message); bool CreateFumoInstance(struct FumoInstance *game); -bool FumoInstanceHook(struct Vector *vec, update_func update, void *state); - bool FumoInstanceRun(struct FumoInstance *game); \ No newline at end of file diff --git a/source/fumoengine/include/event.c b/source/fumoengine/include/event.c index fecef60..8814345 100644 --- a/source/fumoengine/include/event.c +++ b/source/fumoengine/include/event.c @@ -22,7 +22,7 @@ void FreeEvent(struct Event *event) free(event->methods); } -bool EventRegister(struct Event *event, callback func, void *instance) +bool EventAdd(struct Event *event, handler callback, void *instance) { if (event->len == event->capacity) { usize new_size = event->capacity * 2 * sizeof(struct Method); @@ -36,7 +36,7 @@ bool EventRegister(struct Event *event, callback func, void *instance) } event->methods[event->len++] = (struct Method) { - .func = func, + .callback = callback, .instance = instance }; @@ -47,6 +47,6 @@ void EventInvoke(struct Event *event, void *state) { for (usize i = 0; i < event->len; i++) { struct Method *method = event->methods + i; - method->func(state, method->instance); + method->callback(state, method->instance); } } \ No newline at end of file diff --git a/source/fumoengine/include/event.h b/source/fumoengine/include/event.h index 1efd478..bb25b41 100644 --- a/source/fumoengine/include/event.h +++ b/source/fumoengine/include/event.h @@ -5,10 +5,10 @@ #include "fumocommon.h" -typedef void (*callback)(void *state, void *instance); +typedef void (*handler)(void *state, void *instance); struct Method { - callback func; + handler callback; void *instance; }; @@ -19,8 +19,8 @@ struct Event { }; -bool CreateEvent(struct Event *event, void *state); +bool CreateEvent(struct Event *event); -bool EventRegister(struct Event *event, callback func, void *instance); +bool EventAdd(struct Event *event, handler callback, void *instance); -void EventInvoke(struct Event *event); \ No newline at end of file +void EventInvoke(struct Event *event, void *state); \ No newline at end of file diff --git a/source/fumoengine/input/ctrl.c b/source/fumoengine/input/ctrl.c index b80440a..f75dc3d 100644 --- a/source/fumoengine/input/ctrl.c +++ b/source/fumoengine/input/ctrl.c @@ -1,5 +1,4 @@ #include "ctrl.h" - #include "ringbuffer.h" #define INIT_SIZE 16 @@ -9,7 +8,7 @@ bool CreateController(struct Controller *ctrl) { struct ctrl_bkt *code_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt)); struct ctrl_bkt *bind_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt)); - struct InputAxis *axes = calloc(INIT_SIZE, sizeof(struct InputAxis)); + struct ControlAxis *axes = calloc(INIT_SIZE, sizeof(struct ControlAxis)); if (code_bkts == nullptr or bind_bkts == nullptr or axes == nullptr) return false; @@ -43,19 +42,19 @@ void FreeController(struct Controller *ctrl) free(ctrl->axis_vec.axes); } -struct ctrl_bkt *get_bkt(struct ctrl_dict *dict, size_t i) { +struct ctrl_bkt *get_bkt(struct ctrl_dict *dict, usize i) { return &dict->bkts[i]; } -size_t wrap_index(size_t i, size_t max) { +usize wrap_index(usize i, usize max) { return i % (SIZE_MAX - max + 1); } struct ctrl_bkt *find_set(struct ctrl_dict *dict, union InputID id) { - size_t i = id.hash % dict->capacity; + usize i = id.hash % dict->capacity; - size_t last = wrap_index(i - 1, dict->capacity); + usize last = wrap_index(i - 1, dict->capacity); while (i != last) { struct ctrl_bkt *bkt = get_bkt(dict, i); @@ -77,9 +76,9 @@ struct ctrl_bkt *find_set(struct ctrl_dict *dict, union InputID id) struct ctrl_bkt *find(struct ctrl_dict *dict, union InputID id) { - size_t i = id.hash % dict->capacity; + usize i = id.hash % dict->capacity; - size_t last = wrap_index(i - 1, dict->capacity); + usize last = wrap_index(i - 1, dict->capacity); while (i != last) { struct ctrl_bkt *bkt = get_bkt(dict, i); @@ -92,7 +91,7 @@ struct ctrl_bkt *find(struct ctrl_dict *dict, union InputID id) return nullptr; } -struct InputAxis *find_axis(struct ctrl_dict *dict, union InputID id) +struct ControlAxis *find_axis(struct ctrl_dict *dict, union InputID id) { struct ctrl_bkt *bkt = find(dict, id); if (bkt == nullptr) @@ -105,10 +104,10 @@ union InputID as_id(u16f value, u16f type) { return (union InputID) { .value = value, .type = type }; } -bool ControllerMap(struct Controller *ctrl, u16f code, u16f bind, u16f type) +bool ControllerMap(struct Controller *ctrl, struct ControlMapping *mapping) { - struct ctrl_bkt *code_bkt = find_set(&ctrl->codes, as_id(code, type)); - struct ctrl_bkt *bind_bkt = find_set(&ctrl->binds, as_id(bind, type)); + struct ctrl_bkt *code_bkt = find_set(&ctrl->codes, as_id(mapping->code, mapping->type)); + struct ctrl_bkt *bind_bkt = find_set(&ctrl->binds, as_id(mapping->bind, mapping->type)); if (code_bkt->axis == nullptr) code_bkt->axis = &ctrl->axis_vec.axes[ctrl->axis_vec.len++]; @@ -116,25 +115,28 @@ bool ControllerMap(struct Controller *ctrl, u16f code, u16f bind, u16f type) return false; bind_bkt->axis = code_bkt->axis; - code_bkt->axis->id.type = type; + code_bkt->axis->id.type = mapping->type; + + mapping->axis = code_bkt->axis; return true; } bool ControllerMapMulti( struct Controller *ctrl, - usize n_maps, - struct ControlMapping *maps + usize n, + struct ControlMapping *mappings ) { - for (usize i = 0; i < n_maps; i++) { - struct ControlMapping *map = maps + i; - if (!ControllerMap(ctrl, map->code, map->bind, map->type)) + for (usize i = 0; i < n; i++) { + struct ControlMapping *mapping = mappings + i; + + if (!ControllerMap(ctrl, mapping)) return false; } return true; } -struct InputAxis *ControllerGet(struct Controller *ctrl, u16f code, u16f type) +struct ControlAxis *ControllerGet(struct Controller *ctrl, u16f code, u16f type) { struct ctrl_bkt *code_bkt = find(&ctrl->codes, as_id(code, type)); if (code_bkt == nullptr) @@ -143,7 +145,7 @@ struct InputAxis *ControllerGet(struct Controller *ctrl, u16f code, u16f type) return code_bkt->axis; } -void dispatch_update(struct InputAxis *axis, struct InputRecord *rec) +void dispatch_update(struct ControlAxis *axis, struct InputRecord *rec) { if (rec->is_down and !axis->is_held) { axis->is_down = true; @@ -160,8 +162,8 @@ void dispatch_update(struct InputAxis *axis, struct InputRecord *rec) void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs) { - for (size_t i = 0; i < ctrl->pending_buf.len; i++) { - struct InputAxis *axis = ctrl->pending_buf.axes[i]; + for (usize i = 0; i < ctrl->pending_buf.len; i++) { + struct ControlAxis *axis = ctrl->pending_buf.axes[i]; axis->is_up = false; axis->is_down = false; @@ -169,10 +171,10 @@ void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs) ctrl->pending_buf.len = 0; - for (size_t i = 0; i < recs->head.len; i++) { + for (usize i = 0; i < recs->head.len; i++) { struct InputRecord *rec = &recs->buf[i]; - struct InputAxis *axis = find_axis(&ctrl->binds, rec->id); + struct ControlAxis *axis = find_axis(&ctrl->binds, rec->id); if (axis == nullptr) continue; diff --git a/source/fumoengine/input/ctrl.h b/source/fumoengine/input/ctrl.h index 1399c7b..e8f6344 100644 --- a/source/fumoengine/input/ctrl.h +++ b/source/fumoengine/input/ctrl.h @@ -9,7 +9,7 @@ #include "input.h" -struct InputAxis { +struct ControlAxis { nsec last_pressed; nsec last_released; @@ -30,7 +30,7 @@ struct InputAxis { }; struct ctrl_bkt { - struct InputAxis *axis; + struct ControlAxis *axis; union InputID id; }; @@ -41,14 +41,14 @@ struct ctrl_dict { }; struct ctrl_axis_vec { - struct InputAxis *axes; + struct ControlAxis *axes; u16f size; u16f len; }; struct Controller { struct { - struct InputAxis *axes[IO_BUF_SIZE]; + struct ControlAxis *axes[IO_BUF_SIZE]; u8f len; } pending_buf; @@ -61,21 +61,23 @@ struct ControlMapping { u16 code; u16 bind; u16 type; + struct ControlAxis *axis; }; bool CreateController(struct Controller *ctrl); + void FreeController(struct Controller *ctrl); -bool ControllerMap(struct Controller *ctrl, u16f code, u16f bind, u16f type); +bool ControllerMap(struct Controller *ctrl, struct ControlMapping *mapping); bool ControllerMapMulti( struct Controller *ctrl, - usize n_maps, - struct ControlMapping *maps + usize n, + struct ControlMapping *mappings ); -struct InputAxis *ControllerGet(struct Controller *ctrl, u16f code, u16f type); +struct ControlAxis *ControllerGet(struct Controller *ctrl, u16f code, u16f type); void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs); diff --git a/source/fumotris/fumotris.c b/source/fumotris/fumotris.c index 89c1898..b7b3c6e 100644 --- a/source/fumotris/fumotris.c +++ b/source/fumotris/fumotris.c @@ -1,35 +1,48 @@ #include "fumotris.h" -void Update(struct FumoInstance *instance, void *args) -{ - struct Fumotris *game = args; +struct Fumotris { + struct TetrMap board; + struct TetrMap piece; +}; - TetrMapDraw(&game->board, &instance->term); + +void FumotrisStart(void *engine, void *app) +{ + struct FumoInstance *inst = engine; + struct Fumotris *game = app; + + ControllerMapMulti(&inst->ctrl, CODE_COUNT, mappings_global); + + CreateTetrMap(&game->board, 10, 10); + CreateTetrMap(&game->piece, 3, 3); + game->piece.blks = T; } -bool CreateFumotris(struct Fumotris *game) +void FumotrisUpdate(void *engine, void *app) { - if (!CreateTetrMap(&game->board, 10, 10)) - return false; + struct FumoInstance *inst = engine; + struct Fumotris *game = app; - return true; + if (mappings_global[LEFT].axis->is_down) + game->piece.x -= 1; + if (mappings_global[RIGHT].axis->is_down) + game->piece.x += 1; + + TetrMapDraw(&game->board, &inst->term); + TetrMapDraw(&game->piece, &inst->term); } int main() { - struct FumoInstance instance; - CreateFumoInstance(&instance); + struct FumoInstance inst; + CreateFumoInstance(&inst); struct Fumotris game; - CreateFumotris(&game); + EventAdd(&inst.on_start, FumotrisStart, &game); + EventAdd(&inst.on_update, FumotrisUpdate, &game); - ControllerMapMulti(&instance.ctrl, CODE_COUNT, MAPPINGS); - - FumoInstanceHook(&instance.on_update, Update, &game); - - - FumoInstanceRun(&instance); + FumoInstanceRun(&inst); return 0; } \ No newline at end of file diff --git a/source/fumotris/fumotris.h b/source/fumotris/fumotris.h index efedc28..bbdd4bc 100644 --- a/source/fumotris/fumotris.h +++ b/source/fumotris/fumotris.h @@ -20,7 +20,7 @@ enum ControlCode { MOUSE }; -struct ControlMapping MAPPINGS[12] = { +struct ControlMapping mappings_global[12] = { { LEFT, 0x25, BUTTON }, { RIGHT, 0x27, BUTTON }, { SOFT_DROP, 0x28, BUTTON }, @@ -36,10 +36,6 @@ struct ControlMapping MAPPINGS[12] = { }; -struct Fumotris { - struct TetrMap board; - struct TetrMap piece; -}; const u8 I[16] = { 0, 0, 0, 0, diff --git a/test.exe b/test.exe index 32de7d9..f44ad46 100644 Binary files a/test.exe and b/test.exe differ