dicks
This commit is contained in:
Julia 2024-05-09 00:32:58 -05:00
parent fb8c14a443
commit 7c58e46fc4
9 changed files with 80 additions and 95 deletions

View file

@ -1,4 +1,5 @@
#include "dictionary.h" #include "dictionary.h"
#include <stdlib.h>
#include <string.h> #include <string.h>
@ -12,7 +13,7 @@ bool CreateDictionary(DictT T, struct Dictionary *dict)
*dict = (struct Dictionary) { *dict = (struct Dictionary) {
.filled = 0, .filled = 0,
.capacity = 16, .capacity = 16,
.bkts = bkts, .bkts = bkts
}; };
return true; return true;
@ -28,9 +29,9 @@ void *index_bkt(DictT T, struct Dictionary *dict, usize i)
return (u8 *)dict->bkts + i * T->BKT_SIZE; 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) void *get_val(DictT T, void *bkt)
@ -38,12 +39,18 @@ void *get_val(DictT T, void *bkt)
return (u8 *)bkt + T->VAL_OFS; 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) void *probe_bkt(DictT T, struct Dictionary *dict, usize index, u32 key)
{ {
for (usize i = 0; i < dict->capacity; i++) { for (usize i = 0; i < dict->capacity; i++) {
void *bkt = index_bkt(T, dict, (index + i) % dict->capacity); void *bkt = index_bkt(T, dict, (index + i) % dict->capacity);
if (get_key(T, bkt) == key) if (*get_key(T, bkt) == key)
return bkt; 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++) { for (usize i = 0; i < dict->capacity; i++) {
void *bkt = index_bkt(T, dict, (index + i) % dict->capacity); 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) if (k == 0 or k == key)
return bkt; return bkt;
} }
@ -75,7 +82,7 @@ void *DictionarySet(DictT T, struct Dictionary *dict, u32 key, void *val)
usize index = key % dict->capacity; usize index = key % dict->capacity;
void *bkt = probe_empty_bkt(T, dict, index, key); 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); set_bkt(T, bkt, key, val);
return bkt; return bkt;

View file

@ -4,84 +4,78 @@
#define INIT_SIZE 16 #define INIT_SIZE 16
DictT CODES_T = DICT_T(struct ControlAxis); DictT BIND_T = DICT_T(struct InputAxis *);
DictT BINDS_T = DICT_T(struct ControlAxis *);
bool CreateController(struct Controller *ctrl) bool CreateController(struct Controller *ctrl)
{ {
if (!CreateDictionary(CODES_T, &ctrl->codes)) struct InputAxis *axes = calloc(16, sizeof(struct InputAxis));
if (axes == nullptr)
return false; return false;
if (!CreateDictionary(BINDS_T, &ctrl->binds)) if (!CreateDictionary(BIND_T, &ctrl->binds))
return false; return false;
*ctrl = (struct Controller) { .pending.len = 0 }; *ctrl = (struct Controller) {
.pending_len = 0,
.axes = axes,
.axes_len = 0
};
return true; return true;
} }
void FreeController(struct Controller *ctrl) void FreeController(struct Controller *ctrl)
{ {
FreeDictionary(&ctrl->codes); free(ctrl->axes);
FreeDictionary(&ctrl->binds); 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); return bind + (type << 16);
if (bkt == nullptr)
return nullptr;
return bkt->axis;
} }
struct ControlAxis *ControllerMap( struct InputAxis *ControllerMap(
struct Controller *ctrl, struct Controller *ctrl,
struct ControlMapping *map struct ControlMapping *map
) { ) {
struct ControlAxis *axis = DictionarySet(CODES_T, &ctrl->codes, map->code); struct InputAxis *axis = &ctrl->axes[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;
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( bool ControllerMapMulti(
struct Controller *ctrl, struct Controller *ctrl,
usize n, usize n,
struct ControlMapping *maps, struct ControlMapping *maps,
struct ControlAxis **axis_ptrs struct InputAxis **binds
) { ) {
for (usize i = 0; i < n; i++) { for (usize i = 0; i < n; i++) {
struct ControlAxis *axis = ControllerMap(ctrl, maps + i); struct InputAxis *axis = ControllerMap(ctrl, maps + i);
if (axis == nullptr) if (axis == nullptr)
return false; return false;
axis_ptrs[i] = axis; binds[i] = axis;
} }
return true; return true;
} }
struct ControlAxis *ControllerGet(struct Controller *ctrl, u16f code, u16f type) void dispatch_update(struct InputAxis *axis, struct InputRecord *rec)
{
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)
{ {
if (rec->is_down and !axis->is_held) { if (rec->is_down and !axis->is_held) {
axis->is_down = true; 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) void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs)
{ {
for (usize i = 0; i < ctrl->pending.len; i++) { for (usize i = 0; i < ctrl->pending_len; i++) {
struct ControlAxis *axis = ctrl->pending.buf[i]; struct InputAxis *axis = ctrl->pending[i];
axis->is_up = false; axis->is_up = false;
axis->is_down = false; axis->is_down = false;
} }
ctrl->pending.len = 0; ctrl->pending_len = 0;
for (usize i = 0; i < recs->head.len; i++) { 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) if (axis == nullptr)
continue; continue;
dispatch_update(axis, rec); dispatch_update(axis, rec);
ctrl->pending.buf[ctrl->pending.len++] = axis; ctrl->pending[ctrl->pending_len++] = axis;
} }
recs->head.len = 0; recs->head.len = 0;

View file

@ -10,7 +10,7 @@ struct ControlMapping {
u16 type; u16 type;
}; };
struct ControlAxis { struct InputAxis {
nsec last_pressed; nsec last_pressed;
nsec last_released; nsec last_released;
@ -21,22 +21,20 @@ struct ControlAxis {
union InputData data; union InputData data;
}; };
union InputID id; u16 type;
struct { bool is_down;
u8f is_down : 1; bool is_held;
u8f is_held : 1; bool is_up;
u8f is_up : 1;
};
}; };
struct Controller { struct Controller {
struct { struct InputAxis *pending[IO_BUF_SIZE];
struct ControlAxis *buf[IO_BUF_SIZE]; usize pending_len;
u8f len;
} pending; struct InputAxis *axes;
usize axes_len;
struct Dictionary codes;
struct Dictionary binds; struct Dictionary binds;
}; };
@ -45,7 +43,7 @@ bool CreateController(struct Controller *ctrl);
void FreeController(struct Controller *ctrl); void FreeController(struct Controller *ctrl);
struct ControlAxis *ControllerMap( struct InputAxis *ControllerMap(
struct Controller *ctrl, struct Controller *ctrl,
struct ControlMapping *map struct ControlMapping *map
); );
@ -54,10 +52,8 @@ bool ControllerMapMulti(
struct Controller *ctrl, struct Controller *ctrl,
usize n, usize n,
struct ControlMapping *maps, 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); void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs);

View file

@ -5,7 +5,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h> #include <pthread.h>
#include "fumocommon.h" #include "fumocommon.h"
#include "ringbuffer.h" #include "ringbuffer.h"
@ -20,21 +19,6 @@ enum InputType {
ESCAPE ESCAPE
}; };
union InputID {
u32 hash;
struct {
union {
u16 code;
u16 bind;
u16 value;
};
u16 type;
};
};
struct Button { struct Button {
u64 value; u64 value;
}; };
@ -54,7 +38,6 @@ union InputData {
struct Joystick input_js; struct Joystick input_js;
}; };
struct InputRecord { struct InputRecord {
nsec time; nsec time;
@ -65,13 +48,12 @@ struct InputRecord {
union InputData data; union InputData data;
}; };
union InputID id; u16 bind;
u16 type;
struct { bool is_down;
u8f is_down : 1; bool is_held;
u8f is_held : 1; bool is_up;
u8f is_up : 1;
};
}; };
struct RecordBuffer { struct RecordBuffer {

View file

@ -3,26 +3,32 @@
void ReadButton(struct InputRecord *rec, u16f bind, bool is_down) 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_down = is_down;
rec->is_up = !is_down; rec->is_up = !is_down;
} }
void ReadAxis(struct InputRecord *rec, u16f bind, u64 value) 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; rec->axis.value = value;
} }
void ReadJoystick(struct InputRecord *rec, u16f bind, i32 x, i32 y) 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.x = x;
rec->js.y = y; rec->js.y = y;
} }
size_t UCS2ToUTF8(char *buf, u16f ucs2) size_t UCS2ToUTF8(char *buf, u16f ucs2)
{ {
if (ucs2 < 0xFF) { if (ucs2 < 0xFF) {
buf[0] = ucs2; buf[0] = ucs2;
return 1; return 1;

View file

@ -1,6 +1,5 @@
#include "win.h" #include "win.h"
#include <windows.h> #include <windows.h>
#include "parseinput.h" #include "parseinput.h"
#include "ringbuffer.h" #include "ringbuffer.h"

View file

@ -4,5 +4,4 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "fumocommon.h" #include "fumocommon.h"

View file

@ -3,7 +3,7 @@
struct Fumotris { struct Fumotris {
struct ControlMapping mappings[BINDS_N]; struct ControlMapping mappings[BINDS_N];
struct ControlAxis *input[BINDS_N]; struct InputAxis *input[BINDS_N];
struct TetrMap board; struct TetrMap board;
struct TetrMap piece; struct TetrMap piece;

BIN
test.exe

Binary file not shown.