diff --git a/source/fumoengine/include/dictionary.c b/source/fumoengine/include/dictionary.c index 3eae7c6..dbfb39e 100644 --- a/source/fumoengine/include/dictionary.c +++ b/source/fumoengine/include/dictionary.c @@ -1,4 +1,5 @@ #include "dictionary.h" +#include #include @@ -12,7 +13,7 @@ bool CreateDictionary(DictT T, struct Dictionary *dict) *dict = (struct Dictionary) { .filled = 0, .capacity = 16, - .bkts = bkts, + .bkts = bkts }; return true; @@ -28,9 +29,9 @@ void *index_bkt(DictT T, struct Dictionary *dict, usize i) return (u8 *)dict->bkts + i * T->BKT_SIZE; } -u32 get_key(DictT T, void *bkt) +u32 *get_key(DictT T, void *bkt) { - return *(u32 *)bkt; + return (u32 *)bkt; } void *get_val(DictT T, void *bkt) @@ -38,12 +39,18 @@ void *get_val(DictT T, void *bkt) return (u8 *)bkt + T->VAL_OFS; } +void set_bkt(DictT T, void *bkt, u32 key, void *val) +{ + *get_key(T, bkt) = key; + memcpy(get_val(T, bkt), val, T->VAL_SIZE); +} + void *probe_bkt(DictT T, struct Dictionary *dict, usize index, u32 key) { for (usize i = 0; i < dict->capacity; i++) { void *bkt = index_bkt(T, dict, (index + i) % dict->capacity); - if (get_key(T, bkt) == key) + if (*get_key(T, bkt) == key) return bkt; } @@ -55,7 +62,7 @@ void *probe_empty_bkt(DictT T, struct Dictionary *dict, usize index, u32 key) for (usize i = 0; i < dict->capacity; i++) { void *bkt = index_bkt(T, dict, (index + i) % dict->capacity); - u32 k = get_key(T, bkt); + u32 k = *get_key(T, bkt); if (k == 0 or k == key) return bkt; } @@ -75,7 +82,7 @@ void *DictionarySet(DictT T, struct Dictionary *dict, u32 key, void *val) usize index = key % dict->capacity; void *bkt = probe_empty_bkt(T, dict, index, key); - if (get_key(T, bkt) == 0) + if (*get_key(T, bkt) == 0) set_bkt(T, bkt, key, val); return bkt; diff --git a/source/fumoengine/input/ctrl.c b/source/fumoengine/input/ctrl.c index 150bf19..b7f994a 100644 --- a/source/fumoengine/input/ctrl.c +++ b/source/fumoengine/input/ctrl.c @@ -4,84 +4,78 @@ #define INIT_SIZE 16 -DictT CODES_T = DICT_T(struct ControlAxis); -DictT BINDS_T = DICT_T(struct ControlAxis *); +DictT BIND_T = DICT_T(struct InputAxis *); bool CreateController(struct Controller *ctrl) { - if (!CreateDictionary(CODES_T, &ctrl->codes)) + struct InputAxis *axes = calloc(16, sizeof(struct InputAxis)); + + if (axes == nullptr) return false; - if (!CreateDictionary(BINDS_T, &ctrl->binds)) + if (!CreateDictionary(BIND_T, &ctrl->binds)) return false; - *ctrl = (struct Controller) { .pending.len = 0 }; + *ctrl = (struct Controller) { + .pending_len = 0, + .axes = axes, + .axes_len = 0 + }; return true; } void FreeController(struct Controller *ctrl) { - FreeDictionary(&ctrl->codes); + free(ctrl->axes); FreeDictionary(&ctrl->binds); } -struct ControlAxis *find_axis(struct ctrl_dict *dict, union InputID id) +u32 hash_bind(u16f bind, u16f type) { - struct ctrl_bkt *bkt = find(dict, id); - if (bkt == nullptr) - return nullptr; - - return bkt->axis; + return bind + (type << 16); } -struct ControlAxis *ControllerMap( +struct InputAxis *ControllerMap( struct Controller *ctrl, struct ControlMapping *map ) { - struct ControlAxis *axis = DictionarySet(CODES_T, &ctrl->codes, map->code); - struct ctrl_bkt *bind_bkt = set(&ctrl->binds, map->bind, map->type); - - if (code_bkt->axis == nullptr) - code_bkt->axis = &ctrl->axis_vec.axes[ctrl->axis_vec.len++]; - else if (code_bkt->axis == bind_bkt->axis) - return nullptr; - - bind_bkt->axis = code_bkt->axis; - code_bkt->axis->id.type = map->type; + struct InputAxis *axis = &ctrl->axes[map->code]; - return code_bkt->axis; + u32 hash = hash_bind(map->bind, map->type); + struct InputAxis **bind = DictionarySet(BIND_T, &ctrl->binds, hash, axis); + + if (bind == nullptr) { + printf("whar"); + exit(1); + } + + *bind = axis; + axis->type = map->type; + + return axis; } bool ControllerMapMulti( struct Controller *ctrl, usize n, struct ControlMapping *maps, - struct ControlAxis **axis_ptrs + struct InputAxis **binds ) { for (usize i = 0; i < n; i++) { - struct ControlAxis *axis = ControllerMap(ctrl, maps + i); + struct InputAxis *axis = ControllerMap(ctrl, maps + i); if (axis == nullptr) return false; - axis_ptrs[i] = axis; + binds[i] = axis; } return true; } -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) - return nullptr; - - return code_bkt->axis; -} - -void dispatch_update(struct ControlAxis *axis, struct InputRecord *rec) +void dispatch_update(struct InputAxis *axis, struct InputRecord *rec) { if (rec->is_down and !axis->is_held) { axis->is_down = true; @@ -98,24 +92,26 @@ void dispatch_update(struct ControlAxis *axis, struct InputRecord *rec) void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs) { - for (usize i = 0; i < ctrl->pending.len; i++) { - struct ControlAxis *axis = ctrl->pending.buf[i]; + for (usize i = 0; i < ctrl->pending_len; i++) { + struct InputAxis *axis = ctrl->pending[i]; axis->is_up = false; axis->is_down = false; } - ctrl->pending.len = 0; + ctrl->pending_len = 0; for (usize i = 0; i < recs->head.len; i++) { - struct InputRecord *rec = &recs->buf[i]; + struct InputRecord *rec = recs->buf + i; + + u32 hash = hash_bind(rec->bind, rec->type); + struct InputAxis *axis = DictionaryFind(BIND_T, &ctrl->binds, hash); - struct ControlAxis *axis = find_axis(&ctrl->binds, rec->id); if (axis == nullptr) continue; dispatch_update(axis, rec); - ctrl->pending.buf[ctrl->pending.len++] = axis; + ctrl->pending[ctrl->pending_len++] = axis; } recs->head.len = 0; diff --git a/source/fumoengine/input/ctrl.h b/source/fumoengine/input/ctrl.h index ff49c58..24030c9 100644 --- a/source/fumoengine/input/ctrl.h +++ b/source/fumoengine/input/ctrl.h @@ -10,7 +10,7 @@ struct ControlMapping { u16 type; }; -struct ControlAxis { +struct InputAxis { nsec last_pressed; nsec last_released; @@ -21,22 +21,20 @@ struct ControlAxis { union InputData data; }; - union InputID id; + u16 type; - struct { - u8f is_down : 1; - u8f is_held : 1; - u8f is_up : 1; - }; + bool is_down; + bool is_held; + bool is_up; }; struct Controller { - struct { - struct ControlAxis *buf[IO_BUF_SIZE]; - u8f len; - } pending; + struct InputAxis *pending[IO_BUF_SIZE]; + usize pending_len; + + struct InputAxis *axes; + usize axes_len; - struct Dictionary codes; struct Dictionary binds; }; @@ -45,7 +43,7 @@ bool CreateController(struct Controller *ctrl); void FreeController(struct Controller *ctrl); -struct ControlAxis *ControllerMap( +struct InputAxis *ControllerMap( struct Controller *ctrl, struct ControlMapping *map ); @@ -54,10 +52,8 @@ bool ControllerMapMulti( struct Controller *ctrl, usize n, struct ControlMapping *maps, - struct ControlAxis **axis_ptrs + struct InputAxis **axis_ptrs ); -struct ControlAxis *ControllerGet(struct Controller *ctrl, u16f code, u16f type); - void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs); diff --git a/source/fumoengine/input/input.h b/source/fumoengine/input/input.h index dd4bb08..ae4e158 100644 --- a/source/fumoengine/input/input.h +++ b/source/fumoengine/input/input.h @@ -5,7 +5,6 @@ #include #include #include - #include "fumocommon.h" #include "ringbuffer.h" @@ -20,21 +19,6 @@ enum InputType { ESCAPE }; -union InputID { - u32 hash; - - struct { - union { - u16 code; - u16 bind; - u16 value; - }; - - u16 type; - }; -}; - - struct Button { u64 value; }; @@ -54,7 +38,6 @@ union InputData { struct Joystick input_js; }; - struct InputRecord { nsec time; @@ -65,13 +48,12 @@ struct InputRecord { union InputData data; }; - union InputID id; + u16 bind; + u16 type; - struct { - u8f is_down : 1; - u8f is_held : 1; - u8f is_up : 1; - }; + bool is_down; + bool is_held; + bool is_up; }; struct RecordBuffer { diff --git a/source/fumoengine/input/platforms/parseinput.c b/source/fumoengine/input/platforms/parseinput.c index 089fb02..b92717e 100644 --- a/source/fumoengine/input/platforms/parseinput.c +++ b/source/fumoengine/input/platforms/parseinput.c @@ -3,26 +3,32 @@ void ReadButton(struct InputRecord *rec, u16f bind, bool is_down) { - rec->id = (union InputID) { .bind = bind, .type = BUTTON }; + rec->bind = bind; + rec->type = BUTTON; + rec->is_down = is_down; rec->is_up = !is_down; } void ReadAxis(struct InputRecord *rec, u16f bind, u64 value) { - rec->id = (union InputID) { .bind = bind, .type = AXIS }; + rec->bind = bind; + rec->type = AXIS; + rec->axis.value = value; } void ReadJoystick(struct InputRecord *rec, u16f bind, i32 x, i32 y) { - rec->id = (union InputID) { .bind = bind, .type = JOYSTICK }; + rec->bind = bind; + rec->type = JOYSTICK; + rec->js.x = x; rec->js.y = y; } size_t UCS2ToUTF8(char *buf, u16f ucs2) -{ +{ if (ucs2 < 0xFF) { buf[0] = ucs2; return 1; diff --git a/source/fumoengine/input/platforms/win.c b/source/fumoengine/input/platforms/win.c index 314e789..b638b35 100644 --- a/source/fumoengine/input/platforms/win.c +++ b/source/fumoengine/input/platforms/win.c @@ -1,6 +1,5 @@ #include "win.h" #include - #include "parseinput.h" #include "ringbuffer.h" diff --git a/source/fumoengine/input/platforms/win.h b/source/fumoengine/input/platforms/win.h index 8625da5..835cf64 100644 --- a/source/fumoengine/input/platforms/win.h +++ b/source/fumoengine/input/platforms/win.h @@ -4,5 +4,4 @@ #include #include #include - #include "fumocommon.h" \ No newline at end of file diff --git a/source/fumotris/fumotris.c b/source/fumotris/fumotris.c index 028de4b..155efd1 100644 --- a/source/fumotris/fumotris.c +++ b/source/fumotris/fumotris.c @@ -3,7 +3,7 @@ struct Fumotris { struct ControlMapping mappings[BINDS_N]; - struct ControlAxis *input[BINDS_N]; + struct InputAxis *input[BINDS_N]; struct TetrMap board; struct TetrMap piece; diff --git a/test.exe b/test.exe index f44ad46..4ff9e84 100644 Binary files a/test.exe and b/test.exe differ