oh
This commit is contained in:
Julia 2024-04-10 15:05:56 -05:00
parent a836e54ccb
commit ca32c2ce76
7 changed files with 97 additions and 60 deletions

23
source/game/hash.c Normal file
View file

@ -0,0 +1,23 @@
#include <iso646.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
typedef u32 hashtype;
hashtype Hash(void *item, size_t size)
{
u8 *data = (u8 *)item;
u32 h = 98317;
for (size_t i = 0; i < size; i++) {
h ^= data[i];
h *= 0x5bd1e995;
h ^= h >> 15;
}
return h;
}

12
source/game/hash.h Normal file
View file

@ -0,0 +1,12 @@
#pragma once
#include <iso646.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
typedef u32 hashtype;
hashtype Hash(void *item, size_t size);

View file

@ -8,6 +8,7 @@
#include <time.h>
#include "fumotris.h"
#include "hash.h"
#define IO_BUF_SIZE 16
@ -19,24 +20,23 @@ enum InputType {
};
struct Button {
u32 value;
bool is_down;
bool is_held;
bool is_up;
};
u64 value;
} but;
struct Axis {
i64 value;
};
} axis;
struct Joystick {
i32 x;
i32 y;
};
} js;
struct InputRecord {
u16 bind;
u16f bind;
u8 type;
u8 is_down;
u8 is_held;
u8 is_up;
union {
struct Button but;
@ -48,39 +48,12 @@ struct InputRecord {
};
struct InputAxis {
u8 type;
union {
struct Button but;
struct Axis axis;
struct Joystick js;
};
struct input;
struct timespec last_pressed;
struct timespec last_released;
};
typedef u32 hashtype;
hashtype Hash(void *item, size_t size)
{
u8 *data = (u8 *)item;
u32 h = 98317;
for (size_t i = 0; i < size; i++) {
h ^= data[i];
h *= 0x5bd1e995;
h ^= h >> 15;
}
return h;
}
hashtype hash_id(u16f value, u8f type)
{
struct { u16 id; u8 type; } id = { value, type };
return Hash(&id, sizeof(id));
}
struct ctrl_dict {
size_t capacity;
size_t filled;
@ -99,9 +72,10 @@ struct Controller {
struct ctrl_dict binds;
struct InputAxis *axes;
struct {
struct InputBuffer {
struct InputRecord records[IO_BUF_SIZE];
size_t len;
size_t start;
} input_buf;
struct {
@ -138,6 +112,7 @@ bool NewCtrl(struct Controller *ctrl, size_t code_cap, size_t bind_cap)
.input_buf = {
.len = 0,
.start = 0,
},
.pending_buf = {
.len = 0,
@ -170,6 +145,12 @@ size_t wrap_index(size_t i, size_t max)
return i % (SIZE_MAX - max + 1);
}
hashtype hash_id(u16f value, u8f type)
{
struct { u16 id; u8 type; } id = { value, type };
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);

View file

@ -94,9 +94,10 @@ struct Controller {
struct ctrl_dict binds;
struct InputAxis *axes;
struct {
struct InputBuffer {
struct InputRecord records[IO_BUF_SIZE];
size_t len;
size_t start;
} input_buf;
struct {

View file

@ -12,32 +12,45 @@
#include "win.h"
#endif
struct Input {
struct Controller ctrl;
pthread_t thread;
pthread_mutex_t access_mutex;
};
struct InputRecord *get_at(struct InputBuffer *buf, size_t i)
{
return &buf->records[(buf->start + i) % IO_BUF_SIZE];
}
void *block_input(void *args_ptr)
{
struct RecordBuffer *rec_buf = args_ptr;
struct Input *in = args_ptr;
struct InputBuffer tmp_buf = { .len = 0 };
while (true) {
bool success;
#ifdef _WIN32
success = WindowsBlockInput(rec_buf);
if (!WindowsReadInputBuffer(&tmp_buf))
return false;
#endif
if (!success) {
printf("winfail");
exit(1);
pthread_mutex_lock(&in->access_mutex);
for (size_t i = 0; i < tmp_buf.len; i++) {
*get_at(&in->ctrl.input_buf, i) = *get_at();
}
pthread_mutex_unlock(&in->access_mutex);
}
return nullptr;
}
void StartInput(struct Controller *ctrl)
bool StartInput(struct Input *in)
{
pthread_create(&ctrl->thread, nullptr, block_input, &ctrl->buf);
return pthread_create(&in->thread, nullptr, block_input, in) == 0;
}
void JoinInput(struct Controller *ctrl)
bool JoinInput(struct Input *in)
{
pthread_join(ctrl->thread, nullptr);
return pthread_join(in->thread, nullptr) == 0;
}

View file

@ -8,6 +8,13 @@
#include "ctrl.h"
#include "fumotris.h"
void StartInput(struct Controller *ctrl);
struct Input {
struct Controller ctrl;
void JoinInput(struct Controller *ctrl);
pthread_t thread;
pthread_mutex_t access_mutex;
};
bool StartInput(struct Input *in);
bool JoinInput(struct Input *in);

View file

@ -67,8 +67,8 @@ void set_key_record(struct InputRecord *rec, KEY_EVENT_RECORD win_key)
rec->type = KEY;
rec->bind = win_key.wVirtualKeyCode;
rec->button.is_down = win_key.bKeyDown;
rec->button.is_up = !win_key.bKeyDown;
rec->but.is_down = win_key.bKeyDown;
rec->but.is_up = !win_key.bKeyDown;
if (win_key.wVirtualKeyCode == VK_ESCAPE)
rec->type = ESCAPE;
@ -90,8 +90,8 @@ bool set_mouse_record(struct InputRecord *rec, MOUSE_EVENT_RECORD win_mouse)
case MOUSE_MOVED:
rec->type = JOYSTICK;
rec->bind = 0;
rec->joystick.x = win_mouse.dwMousePosition.X;
rec->joystick.y = win_mouse.dwMousePosition.Y;
rec->js.x = win_mouse.dwMousePosition.X;
rec->js.y = win_mouse.dwMousePosition.Y;
break;
default:
return false;
@ -116,9 +116,9 @@ bool dispatch_record(struct InputRecord *rec, INPUT_RECORD win_rec)
return true;
}
bool WinBlockInput(struct RecordBuffer *buf)
bool WinBlockInput(struct Controller *ctrl)
{
size_t win_size = IO_BUF_SIZE - buf->count;
size_t win_size = IO_BUF_SIZE - ctrl->input_buf.len;
INPUT_RECORD win_buf[win_size];
DWORD count;