time, c things
just learned you don't need to include everything all the damn time
This commit is contained in:
parent
21a56c3566
commit
5feb20b352
63
Fumofumotris.code-workspace
Normal file
63
Fumofumotris.code-workspace
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
{
|
||||||
|
"folders": [
|
||||||
|
{
|
||||||
|
"path": "."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"settings": {
|
||||||
|
"files.associations": {
|
||||||
|
"pthread.h": "c",
|
||||||
|
"fumotris.h": "c",
|
||||||
|
"string.h": "c",
|
||||||
|
"input.h": "c",
|
||||||
|
"stdint.h": "c",
|
||||||
|
"iso646.h": "c",
|
||||||
|
"win.h": "c",
|
||||||
|
"array": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"compare": "cpp",
|
||||||
|
"concepts": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"numbers": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"execution": "cpp",
|
||||||
|
"stdbool.h": "c"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,11 +5,17 @@
|
||||||
#include "win.h"
|
#include "win.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct timespec TimeNow()
|
struct Time {
|
||||||
|
u32 sec;
|
||||||
|
u32 nsec;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Time TimeNow()
|
||||||
{
|
{
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
|
// Need to check for failiure
|
||||||
timespec_get(&ts, TIME_UTC);
|
timespec_get(&ts, TIME_UTC);
|
||||||
return ts;
|
return (struct Time) { ts.tv_sec, ts.tv_nsec };
|
||||||
}
|
}
|
||||||
|
|
||||||
double TimeNowDouble()
|
double TimeNowDouble()
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
struct timespec TimeNow();
|
struct Time {
|
||||||
|
u32 sec;
|
||||||
|
u32 nsec;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Time TimeNow();
|
||||||
|
|
||||||
double TimeNowDouble();
|
double TimeNowDouble();
|
||||||
|
|
202
source/io/ctrl.c
202
source/io/ctrl.c
|
@ -1,89 +1,49 @@
|
||||||
#include <iso646.h>
|
#include "ctrl.h"
|
||||||
#include <pthread.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include "fumotris.h"
|
struct ctrl_bkt {
|
||||||
#include "hash.h"
|
union InputID id;
|
||||||
#include "input.h"
|
struct InputAxis *axis;
|
||||||
|
|
||||||
struct CtrlAxis {
|
|
||||||
struct timespec last_pressed;
|
|
||||||
struct timespec last_released;
|
|
||||||
|
|
||||||
u8 type;
|
|
||||||
u8 is_down;
|
|
||||||
u8 is_held;
|
|
||||||
u8 is_up;
|
|
||||||
|
|
||||||
union {
|
|
||||||
struct Button but;
|
|
||||||
struct Axis axis;
|
|
||||||
struct Joystick js;
|
|
||||||
union InputData data;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ctrl_dict {
|
struct ctrl_dict {
|
||||||
size_t capacity;
|
struct ctrl_bkt *bkts;
|
||||||
size_t filled;
|
|
||||||
|
|
||||||
struct ctrl_bkt {
|
u16f capacity;
|
||||||
hashtype hash;
|
u16f filled;
|
||||||
u16 value;
|
|
||||||
u8 type;
|
|
||||||
|
|
||||||
struct CtrlAxis *axis;
|
|
||||||
} *bkts;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Controller {
|
struct axis_vec {
|
||||||
struct ctrl_dict codes;
|
struct InputAxis *axes;
|
||||||
struct ctrl_dict binds;
|
u16f size;
|
||||||
struct CtrlAxis *axes;
|
u16f len;
|
||||||
|
|
||||||
struct InputBuffer input_buf;
|
|
||||||
|
|
||||||
struct {
|
|
||||||
size_t len;
|
|
||||||
struct CtrlAxis *axes[IO_BUF_SIZE];
|
|
||||||
} pending_buf;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bool NewCtrl(struct Controller *ctrl, size_t code_cap, size_t bind_cap)
|
size_t a = sizeof(struct ctrl_dict);
|
||||||
|
|
||||||
|
bool NewCtrl(struct Controller *ctrl, size_t init_axes)
|
||||||
{
|
{
|
||||||
struct ctrl_bkt *code_bkts = calloc(code_cap, sizeof(struct ctrl_bkt));
|
struct ctrl_bkt *code_bkts = calloc(init_axes, sizeof(struct ctrl_bkt));
|
||||||
struct ctrl_bkt *bind_bkts = calloc(bind_cap, sizeof(struct ctrl_bkt));
|
struct ctrl_bkt *bind_bkts = calloc(init_axes, sizeof(struct ctrl_bkt));
|
||||||
struct CtrlAxis *axes = calloc(code_cap, sizeof(struct CtrlAxis));
|
struct InputAxis *axes = calloc(init_axes, sizeof(struct InputAxis));
|
||||||
|
|
||||||
if (code_bkts == nullptr or bind_bkts == nullptr or axes == nullptr)
|
if (code_bkts == nullptr or bind_bkts == nullptr or axes == nullptr)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (size_t i = 0; i < code_cap; i++) {
|
|
||||||
code_bkts[i].axis = axes + i;
|
|
||||||
}
|
|
||||||
|
|
||||||
*ctrl = (struct Controller) {
|
*ctrl = (struct Controller) {
|
||||||
|
.buf.len = 0,
|
||||||
|
.pending_buf.len = 0,
|
||||||
|
|
||||||
|
.axis_vec.axes = axes,
|
||||||
.codes = (struct ctrl_dict) {
|
.codes = (struct ctrl_dict) {
|
||||||
.capacity = code_cap,
|
|
||||||
.filled = 0,
|
|
||||||
.bkts = code_bkts,
|
.bkts = code_bkts,
|
||||||
|
.capacity = init_axes,
|
||||||
|
.filled = 0,
|
||||||
},
|
},
|
||||||
.binds = (struct ctrl_dict) {
|
.binds = (struct ctrl_dict) {
|
||||||
.capacity = bind_cap,
|
|
||||||
.filled = 0,
|
|
||||||
.bkts = bind_bkts,
|
.bkts = bind_bkts,
|
||||||
|
.capacity = init_axes,
|
||||||
|
.filled = 0,
|
||||||
},
|
},
|
||||||
.axes = axes,
|
|
||||||
|
|
||||||
.input_buf = (struct InputBuffer) {
|
|
||||||
.len = 0,
|
|
||||||
},
|
|
||||||
.pending_buf.len = 0,
|
|
||||||
};
|
};
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -92,7 +52,7 @@ void FreeCtrl(struct Controller *ctrl)
|
||||||
{
|
{
|
||||||
free(ctrl->codes.bkts);
|
free(ctrl->codes.bkts);
|
||||||
free(ctrl->binds.bkts);
|
free(ctrl->binds.bkts);
|
||||||
free(ctrl->axes);
|
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, size_t i)
|
||||||
|
@ -100,122 +60,101 @@ struct ctrl_bkt *get_bkt(struct ctrl_dict *dict, size_t i)
|
||||||
return &dict->bkts[i];
|
return &dict->bkts[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_bkt(struct ctrl_bkt *bkt, hashtype hash, u16f value, u8f type)
|
|
||||||
{
|
|
||||||
bkt->hash = hash;
|
|
||||||
bkt->value = value;
|
|
||||||
bkt->type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t wrap_index(size_t i, size_t max)
|
size_t wrap_index(size_t i, size_t max)
|
||||||
{
|
{
|
||||||
return i % (SIZE_MAX - max + 1);
|
return i % (SIZE_MAX - max + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
hashtype hash_id(u16f value, u8f type)
|
struct ctrl_bkt *find_or_set(struct ctrl_dict *dict, union InputID id)
|
||||||
{
|
{
|
||||||
struct { u16 id; u8 type; } id = { value, type };
|
const size_t index = id.hash % dict->capacity;
|
||||||
return Hash(&id, sizeof(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool find_or_set(struct ctrl_dict *dict, struct ctrl_bkt **out, u16f value, u8f type)
|
|
||||||
{
|
|
||||||
hashtype hash = hash_id(value, type);
|
|
||||||
const size_t index = hash % dict->capacity;
|
|
||||||
|
|
||||||
size_t i = index;
|
size_t i = index;
|
||||||
while (i != wrap_index(index - 1, dict->capacity)) {
|
while (i != wrap_index(index - 1, dict->capacity)) {
|
||||||
struct ctrl_bkt *bkt = get_bkt(dict, i);
|
struct ctrl_bkt *bkt = get_bkt(dict, i);
|
||||||
|
|
||||||
if (bkt->hash == 0) {
|
if (bkt->axis == nullptr) {
|
||||||
set_bkt(bkt, hash, value, type);
|
bkt->id.hash = id.hash;
|
||||||
dict->filled += 1;
|
dict->filled += 1;
|
||||||
*out = bkt;
|
|
||||||
return false;
|
return bkt;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bkt->value == value and bkt->type == type) {
|
if (bkt->id.hash == id.hash) {
|
||||||
*out = bkt;
|
return bkt;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
i = (i + 1) % dict->capacity;
|
i = (i + 1) % dict->capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = nullptr;
|
return nullptr;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ctrl_bkt *find(struct ctrl_dict *dict, u16f value, u8f type)
|
struct ctrl_bkt *find(struct ctrl_dict *dict, union InputID id)
|
||||||
{
|
{
|
||||||
hashtype hash = hash_id(value, type);
|
const size_t index = id.hash % dict->capacity;
|
||||||
const size_t index = hash % dict->capacity;
|
|
||||||
|
|
||||||
size_t i = index;
|
size_t i = index;
|
||||||
while (i != wrap_index(index - 1, dict->capacity)) {
|
while (i != wrap_index(index - 1, dict->capacity)) {
|
||||||
struct ctrl_bkt *bkt = get_bkt(dict, i);
|
struct ctrl_bkt *bkt = get_bkt(dict, i);
|
||||||
if (bkt->hash == 0)
|
|
||||||
goto next;
|
|
||||||
|
|
||||||
if (bkt->value == value and bkt->type == type)
|
if (bkt->id.hash == id.hash)
|
||||||
return bkt;
|
return bkt;
|
||||||
|
|
||||||
next:
|
|
||||||
i = (i + 1) % dict->capacity;
|
i = (i + 1) % dict->capacity;
|
||||||
};
|
};
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CtrlAxis *find_axis(struct ctrl_dict *dict, u16f value, u8f type)
|
struct InputAxis *find_axis(struct ctrl_dict *dict, union InputID id)
|
||||||
{
|
{
|
||||||
struct ctrl_bkt *bkt = find(dict, value, type);
|
struct ctrl_bkt *bkt = find(dict, id);
|
||||||
if (bkt == nullptr)
|
if (bkt == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
return bkt->axis;
|
return bkt->axis;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CtrlMap(struct Controller *ctrl, u16f code, u16f bind, u8f type)
|
union InputID to_id(u16f value, u16f type)
|
||||||
{
|
{
|
||||||
if (ctrl->codes.filled >= ctrl->codes.capacity or ctrl->binds.filled >= ctrl->binds.capacity) {
|
return (union InputID) { .value = value, .type = type };
|
||||||
printf("fatal error");
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ctrl_bkt *code_bkt;
|
bool CtrlMap(struct Controller *ctrl, u16f code, u16f type, u16f bind)
|
||||||
find_or_set(&ctrl->codes, &code_bkt, code, type);
|
{
|
||||||
|
struct ctrl_bkt *code_bkt = find_or_set(&ctrl->codes, to_id(code, type));
|
||||||
|
struct ctrl_bkt *bind_bkt = find_or_set(&ctrl->binds, to_id(bind, type));
|
||||||
|
|
||||||
struct ctrl_bkt *bind_bkt;
|
if (code_bkt->axis == nullptr)
|
||||||
bool bind_existed = find_or_set(&ctrl->binds, &bind_bkt, bind, type);
|
code_bkt->axis = &ctrl->axis_vec.axes[ctrl->axis_vec.len++];
|
||||||
|
else if (code_bkt->axis == bind_bkt->axis)
|
||||||
if(bind_existed and bind_bkt->axis == code_bkt->axis)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bind_bkt->axis = code_bkt->axis;
|
bind_bkt->axis = code_bkt->axis;
|
||||||
code_bkt->axis->type = type;
|
code_bkt->axis->id.type = type;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CtrlAxis *CtrlGet(struct Controller *ctrl, u16f code, u8f type)
|
struct InputAxis *CtrlGet(struct Controller *ctrl, u16f code, u16f type)
|
||||||
{
|
{
|
||||||
struct ctrl_bkt *code_bkt = find(&ctrl->codes, code, type);
|
struct ctrl_bkt *code_bkt = find(&ctrl->codes, to_id(code, type));
|
||||||
if (code_bkt == nullptr)
|
if (code_bkt == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
return code_bkt->axis;
|
return code_bkt->axis;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispatch_update(struct CtrlAxis *axis, struct InputRecord *rec)
|
void dispatch_update(struct InputAxis *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;
|
||||||
axis->is_held = true;
|
axis->is_held = true;
|
||||||
axis->last_pressed = rec->timestamp;
|
axis->last_pressed = rec->time;
|
||||||
} else if (rec->is_up) {
|
} else if (rec->is_up) {
|
||||||
axis->is_up = true;
|
axis->is_up = true;
|
||||||
axis->is_held = false;
|
axis->is_held = false;
|
||||||
axis->last_released = rec->timestamp;
|
axis->last_released = rec->time;
|
||||||
}
|
}
|
||||||
|
|
||||||
axis->data = rec->data;
|
axis->data = rec->data;
|
||||||
|
@ -224,17 +163,18 @@ void dispatch_update(struct CtrlAxis *axis, struct InputRecord *rec)
|
||||||
bool CtrlPoll(struct Controller *ctrl)
|
bool CtrlPoll(struct Controller *ctrl)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < ctrl->pending_buf.len; i++) {
|
for (size_t i = 0; i < ctrl->pending_buf.len; i++) {
|
||||||
struct CtrlAxis *axis = ctrl->pending_buf.axes[i];
|
struct InputAxis *axis = ctrl->pending_buf.axes[i];
|
||||||
|
|
||||||
axis->is_up = false;
|
axis->is_up = false;
|
||||||
axis->is_down = false;
|
axis->is_down = false;
|
||||||
}
|
}
|
||||||
ctrl->pending_buf.len = ctrl->input_buf.len;
|
ctrl->pending_buf.len = ctrl->buf.len;
|
||||||
|
|
||||||
for (size_t i = 0; i < ctrl->input_buf.len; i++) {
|
for (size_t i = 0; i < ctrl->buf.len; i++) {
|
||||||
struct InputRecord *rec = &ctrl->input_buf.records[i];
|
struct InputRecord *rec = &ctrl->buf.recs[i];
|
||||||
struct CtrlAxis *axis = find_axis(&ctrl->binds, rec->bind, rec->type);
|
|
||||||
|
|
||||||
|
union InputID rec_id = to_id(rec->bind, rec->type);
|
||||||
|
struct InputAxis *axis = find_axis(&ctrl->binds, rec_id);
|
||||||
if (axis == nullptr)
|
if (axis == nullptr)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -242,7 +182,7 @@ bool CtrlPoll(struct Controller *ctrl)
|
||||||
|
|
||||||
ctrl->pending_buf.axes[i] = axis;
|
ctrl->pending_buf.axes[i] = axis;
|
||||||
}
|
}
|
||||||
ctrl->input_buf.len = 0;
|
ctrl->buf.len = 0;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -250,23 +190,33 @@ bool CtrlPoll(struct Controller *ctrl)
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
struct Controller ctrl;
|
struct Controller ctrl;
|
||||||
if (!NewCtrl(&ctrl, 3, 3))
|
if (!NewCtrl(&ctrl, 3))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
CtrlMap(&ctrl, 123, 111, BUTTON);
|
CtrlMap(&ctrl, 123, BUTTON, 111);
|
||||||
|
CtrlMap(&ctrl, 0, BUTTON, 8);
|
||||||
|
|
||||||
ctrl.input_buf.records[ctrl.input_buf.len++] = (struct InputRecord) {
|
ctrl.buf.recs[ctrl.buf.len++] = (struct InputRecord) {
|
||||||
.bind = 111,
|
.bind = 111,
|
||||||
.type = BUTTON,
|
.type = BUTTON,
|
||||||
.is_down = true,
|
.is_down = true,
|
||||||
.but.value = 69
|
.but.value = 69
|
||||||
};
|
};
|
||||||
|
ctrl.buf.recs[ctrl.buf.len++] = (struct InputRecord) {
|
||||||
|
.bind = 8,
|
||||||
|
.type = BUTTON,
|
||||||
|
.is_down = true,
|
||||||
|
.but.value = 1000
|
||||||
|
};
|
||||||
|
|
||||||
CtrlPoll(&ctrl);
|
CtrlPoll(&ctrl);
|
||||||
|
|
||||||
struct CtrlAxis *a = CtrlGet(&ctrl, 123, BUTTON);
|
struct InputAxis *a = CtrlGet(&ctrl, 123, BUTTON);
|
||||||
printf("%u\n", a->but.value);
|
printf("%u\n", a->but.value);
|
||||||
|
|
||||||
|
struct InputAxis *b = CtrlGet(&ctrl, 0, BUTTON);
|
||||||
|
printf("%u\n", b->but.value);
|
||||||
|
|
||||||
printf("success");
|
printf("success");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -5,21 +5,14 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include "fumotris.h"
|
#include "fumotris.h"
|
||||||
#include "hash.h"
|
#include "gametime.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
struct CtrlAxis {
|
struct InputAxis {
|
||||||
struct timespec last_pressed;
|
struct Time last_pressed;
|
||||||
struct timespec last_released;
|
struct Time last_released;
|
||||||
|
|
||||||
u8 type;
|
|
||||||
u8 is_down;
|
|
||||||
u8 is_held;
|
|
||||||
u8 is_up;
|
|
||||||
|
|
||||||
union {
|
union {
|
||||||
struct Button but;
|
struct Button but;
|
||||||
|
@ -27,40 +20,50 @@ struct CtrlAxis {
|
||||||
struct Joystick js;
|
struct Joystick js;
|
||||||
union InputData data;
|
union InputData data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
union InputID id;
|
||||||
|
u8 is_down;
|
||||||
|
u8 is_held;
|
||||||
|
u8 is_up;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ctrl_dict {
|
static struct ctrl_bkt {
|
||||||
size_t capacity;
|
union InputID id;
|
||||||
size_t filled;
|
struct InputAxis *axis;
|
||||||
|
};
|
||||||
|
|
||||||
struct ctrl_bkt {
|
static struct ctrl_dict {
|
||||||
hashtype hash;
|
struct ctrl_bkt *bkts;
|
||||||
u16 value;
|
|
||||||
u8 type;
|
|
||||||
|
|
||||||
struct CtrlAxis *axis;
|
u16f capacity;
|
||||||
} *bkts;
|
u16f filled;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct axis_vec {
|
||||||
|
struct InputAxis *axes;
|
||||||
|
|
||||||
|
u16f size;
|
||||||
|
u16f len;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Controller {
|
struct Controller {
|
||||||
|
struct InputBuffer buf;
|
||||||
|
struct {
|
||||||
|
struct InputAxis *axes[IO_BUF_SIZE];
|
||||||
|
u16f len;
|
||||||
|
} pending_buf;
|
||||||
|
|
||||||
|
struct axis_vec axis_vec;
|
||||||
struct ctrl_dict codes;
|
struct ctrl_dict codes;
|
||||||
struct ctrl_dict binds;
|
struct ctrl_dict binds;
|
||||||
struct CtrlAxis *axes;
|
|
||||||
|
|
||||||
struct InputBuffer input_buf;
|
|
||||||
|
|
||||||
struct {
|
|
||||||
size_t len;
|
|
||||||
struct CtrlAxis *axes[IO_BUF_SIZE];
|
|
||||||
} pending_buf;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bool NewCtrl(struct Controller *ctrl, size_t code_cap, size_t bind_cap);
|
bool NewCtrl(struct Controller *ctrl, size_t init_axes);
|
||||||
|
|
||||||
void FreeCtrl(struct Controller *ctrl);
|
void FreeCtrl(struct Controller *ctrl);
|
||||||
|
|
||||||
bool CtrlMap(struct Controller *ctrl, u16f code, u16f bind, u8f type);
|
bool CtrlMap(struct Controller *ctrl, u16f code, u16f type, u16f bind);
|
||||||
|
|
||||||
struct CtrlAxis *CtrlGet(struct Controller *ctrl, u16f code, u8f type);
|
struct InputAxis *CtrlGet(struct Controller *ctrl, u16f code, u16f type);
|
||||||
|
|
||||||
bool CtrlPoll(struct Controller *ctrl);
|
bool CtrlPoll(struct Controller *ctrl);
|
|
@ -1,64 +1,11 @@
|
||||||
#include <iso646.h>
|
#include "input.h"
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "fumotris.h"
|
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
|
|
||||||
#define IO_BUF_SIZE 16
|
|
||||||
|
|
||||||
enum InputType {
|
|
||||||
BUTTON,
|
|
||||||
AXIS,
|
|
||||||
JOYSTICK,
|
|
||||||
ESCAPE
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Button {
|
|
||||||
u64 value;
|
|
||||||
};
|
|
||||||
struct Axis {
|
|
||||||
i64 value;
|
|
||||||
};
|
|
||||||
struct Joystick {
|
|
||||||
i32 x;
|
|
||||||
i32 y;
|
|
||||||
};
|
|
||||||
union InputData {
|
|
||||||
struct Button input_but;
|
|
||||||
struct Axis input_axis;
|
|
||||||
struct Joystick input_js;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct InputRecord {
|
|
||||||
u16f bind;
|
|
||||||
struct timespec timestamp;
|
|
||||||
|
|
||||||
u8 type;
|
|
||||||
u8 is_down;
|
|
||||||
u8 is_held;
|
|
||||||
u8 is_up;
|
|
||||||
|
|
||||||
union {
|
|
||||||
struct Button but;
|
|
||||||
struct Axis axis;
|
|
||||||
struct Joystick js;
|
|
||||||
union InputData data;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
struct InputBuffer {
|
|
||||||
size_t len;
|
|
||||||
size_t start;
|
|
||||||
struct InputRecord records[IO_BUF_SIZE];
|
|
||||||
};
|
|
||||||
|
|
||||||
struct InputRecord *in_buf_get(struct InputBuffer *buf, size_t i)
|
struct InputRecord *in_buf_get(struct InputBuffer *buf, size_t i)
|
||||||
{
|
{
|
||||||
return buf->records + (buf->start + 1) % IO_BUF_SIZE;
|
return buf->recs + (buf->start + 1) % IO_BUF_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest)
|
void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest)
|
||||||
|
@ -78,7 +25,7 @@ void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest)
|
||||||
|
|
||||||
void InputBufferCopy(struct InputBuffer *buf, struct InputRecord *src)
|
void InputBufferCopy(struct InputBuffer *buf, struct InputRecord *src)
|
||||||
{
|
{
|
||||||
buf->records[(buf->start + buf->len) % IO_BUF_SIZE] = *src;
|
buf->recs[(buf->start + buf->len) % IO_BUF_SIZE] = *src;
|
||||||
buf->len += 1;
|
buf->len += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "fumotris.h"
|
#include "fumotris.h"
|
||||||
|
#include "gametime.h"
|
||||||
|
|
||||||
#define IO_BUF_SIZE 16
|
#define IO_BUF_SIZE 16
|
||||||
|
|
||||||
|
@ -16,6 +17,14 @@ enum InputType {
|
||||||
ESCAPE
|
ESCAPE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
union InputID {
|
||||||
|
struct {
|
||||||
|
union { u16 code; u16 bind; u16 value };
|
||||||
|
u16 type;
|
||||||
|
};
|
||||||
|
u32 hash;
|
||||||
|
};
|
||||||
|
|
||||||
struct Button {
|
struct Button {
|
||||||
u64 value;
|
u64 value;
|
||||||
};
|
};
|
||||||
|
@ -33,13 +42,7 @@ union InputData {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct InputRecord {
|
struct InputRecord {
|
||||||
u16f bind;
|
struct Time time;
|
||||||
struct timespec timestamp;
|
|
||||||
|
|
||||||
u8 type;
|
|
||||||
u8 is_down;
|
|
||||||
u8 is_held;
|
|
||||||
u8 is_up;
|
|
||||||
|
|
||||||
union {
|
union {
|
||||||
struct Button but;
|
struct Button but;
|
||||||
|
@ -47,12 +50,19 @@ struct InputRecord {
|
||||||
struct Joystick js;
|
struct Joystick js;
|
||||||
union InputData data;
|
union InputData data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
u16f bind;
|
||||||
|
|
||||||
|
u8 type;
|
||||||
|
u8 is_down;
|
||||||
|
u8 is_held;
|
||||||
|
u8 is_up;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct InputBuffer {
|
struct InputBuffer {
|
||||||
size_t len;
|
struct InputRecord recs[IO_BUF_SIZE];
|
||||||
size_t start;
|
u8f len;
|
||||||
struct InputRecord records[IO_BUF_SIZE];
|
u8f start;
|
||||||
};
|
};
|
||||||
|
|
||||||
void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest);
|
void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest);
|
||||||
|
|
|
@ -114,7 +114,7 @@ void Loop(struct Instance *game)
|
||||||
|
|
||||||
bool Start(struct Instance *game)
|
bool Start(struct Instance *game)
|
||||||
{
|
{
|
||||||
if (!NewCtrl(&game->ctrl, code_count, code_count))
|
if (!NewCtrl(&game->ctrl))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (size_t i = 0; i < code_count; i++) {
|
for (size_t i = 0; i < code_count; i++) {
|
||||||
|
@ -144,7 +144,7 @@ int main()
|
||||||
if(!PlatformInit())
|
if(!PlatformInit())
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
InputStart(&game.ctrl.input_buf, );
|
InputStart(&game.ctrl.buf, );
|
||||||
Loop(&game);
|
Loop(&game);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue