errors n all that

i hate muflrgf
This commit is contained in:
Julia 2024-04-19 01:38:45 -05:00
parent 5feb20b352
commit acfb0067be
10 changed files with 149 additions and 210 deletions

View file

@ -51,6 +51,9 @@
"streambuf": "cpp", "streambuf": "cpp",
"typeinfo": "cpp", "typeinfo": "cpp",
"execution": "cpp", "execution": "cpp",
"stdbool.h": "c" "stdbool.h": "c",
"gametime.h": "c",
"stdlib.h": "c",
"stdio.h": "c"
} }
} }

View file

@ -28,17 +28,44 @@ typedef int_fast32_t i32f;
typedef int64_t i64; typedef int64_t i64;
typedef int_fast64_t i64f; typedef int_fast64_t i64f;
enum CtrlCode { const u8 I[16] = {
LEFT, 0, 0, 0, 0,
RIGHT, 0, 0, 0, 0,
SOFT_DROP, 1, 1, 1, 1,
HARD_DROP, 0, 0, 0, 0
ROTATE_CCW, };
ROTATE_CW,
ROTATE_180, const u8 O[4] = {
SWAP, 1, 1,
ESC, 1, 1
VSCROLL, };
HSCROLL,
MOUSE const u8 T[9] = {
0, 1, 0,
1, 1, 1,
0, 0, 0
};
const u8 S[9] = {
0, 1, 1,
1, 1, 0,
0, 0, 0
};
const u8 Z[9] = {
1, 1, 0,
0, 1, 1,
0, 0, 0
};
const u8 J[9] = {
1, 0, 0,
1, 1, 1,
0, 0, 0
};
const u8 L[9] = {
0, 0, 1,
1, 1, 1,
0, 0, 0
}; };

View file

@ -1,25 +1,5 @@
#include "ctrl.h" #include "ctrl.h"
struct ctrl_bkt {
union InputID id;
struct InputAxis *axis;
};
struct ctrl_dict {
struct ctrl_bkt *bkts;
u16f capacity;
u16f filled;
};
struct axis_vec {
struct InputAxis *axes;
u16f size;
u16f len;
};
size_t a = sizeof(struct ctrl_dict);
bool NewCtrl(struct Controller *ctrl, size_t init_axes) bool NewCtrl(struct Controller *ctrl, size_t init_axes)
{ {
struct ctrl_bkt *code_bkts = calloc(init_axes, sizeof(struct ctrl_bkt)); struct ctrl_bkt *code_bkts = calloc(init_axes, sizeof(struct ctrl_bkt));

View file

@ -27,21 +27,19 @@ struct InputAxis {
u8 is_up; u8 is_up;
}; };
static struct ctrl_bkt { struct ctrl_bkt {
union InputID id;
struct InputAxis *axis; struct InputAxis *axis;
union InputID id;
}; };
static struct ctrl_dict { struct ctrl_dict {
struct ctrl_bkt *bkts; struct ctrl_bkt *bkts;
u16f capacity; u16f capacity;
u16f filled; u16f filled;
}; };
static struct axis_vec { struct ctrl_axis_vec {
struct InputAxis *axes; struct InputAxis *axes;
u16f size; u16f size;
u16f len; u16f len;
}; };
@ -53,7 +51,7 @@ struct Controller {
u16f len; u16f len;
} pending_buf; } pending_buf;
struct axis_vec axis_vec; struct ctrl_axis_vec axis_vec;
struct ctrl_dict codes; struct ctrl_dict codes;
struct ctrl_dict binds; struct ctrl_dict binds;
}; };
@ -66,4 +64,41 @@ bool CtrlMap(struct Controller *ctrl, u16f code, u16f type, u16f bind);
struct InputAxis *CtrlGet(struct Controller *ctrl, u16f code, u16f type); struct InputAxis *CtrlGet(struct Controller *ctrl, u16f code, u16f type);
bool CtrlPoll(struct Controller *ctrl); bool CtrlPoll(struct Controller *ctrl);
enum ControlCode {
LEFT,
RIGHT,
SOFT_DROP,
HARD_DROP,
ROTATE_CCW,
ROTATE_CW,
ROTATE_180,
SWAP,
ESC,
VSCROLL,
HSCROLL,
MOUSE
};
struct ControlBind {
enum ControlCode code;
u16 bind;
u8 type;
};
const size_t code_count = 12;
const struct ControlBind ctrl_binds[12] = {
{ LEFT, 0x25, BUTTON },
{ RIGHT, 0x27, BUTTON },
{ SOFT_DROP, 0x28, BUTTON },
{ HARD_DROP, 0x20, BUTTON },
{ ROTATE_CCW, 'Z', BUTTON },
{ ROTATE_CW, 'X', BUTTON },
{ ROTATE_180, 'A', BUTTON },
{ SWAP, 'C', BUTTON },
{ ESC, 0x1B, BUTTON },
{ VSCROLL, 0, AXIS },
{ HSCROLL, 1, AXIS },
{ MOUSE, 0, JOYSTICK }
};

View file

@ -3,30 +3,31 @@
#include "platform.h" #include "platform.h"
struct InputRecord *in_buf_get(struct InputBuffer *buf, size_t i) struct InputRecord *buf_get(struct InputBuffer *buf, size_t i) {
{ return buf->recs + (buf->start + i) % IO_BUF_SIZE;
return buf->recs + (buf->start + 1) % IO_BUF_SIZE;
} }
void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest) size_t max_size(size_t a, size_t b) {
{ return a > b ? a : b;
size_t n = IO_BUF_SIZE - (tmp->len > dest->len ? tmp->len : dest->len);
for (size_t i = 0; i < n; i++) {
*in_buf_get(dest, dest->len + i) = *in_buf_get(tmp, i);
}
if (n < tmp->len)
tmp->start += n;
tmp->len -= n;
dest->len += n;
} }
void InputBufferCopy(struct InputBuffer *buf, struct InputRecord *src) void InputBufferTransfer(struct InputBuffer *dest, struct InputBuffer *src)
{ {
buf->recs[(buf->start + buf->len) % IO_BUF_SIZE] = *src; size_t copy_amt = IO_BUF_SIZE - max_size(dest->len, src->len);
buf->len += 1;
for (size_t i = 0; i < copy_amt; i++)
*buf_get(dest, dest->len + i) = *buf_get(src, i);
if (copy_amt < src->len)
src->start += copy_amt;
src->len -= copy_amt;
dest->len += copy_amt;
}
void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *rec)
{
*buf_get(buf, buf->len++) = *rec;
} }
struct input_args { struct input_args {
@ -34,7 +35,7 @@ struct input_args {
pthread_mutex_t *mutex; pthread_mutex_t *mutex;
}; };
void *block_input(struct input_args *args) void *input_thread_loop(struct input_args *args)
{ {
struct InputBuffer *buf = args->buf; struct InputBuffer *buf = args->buf;
pthread_mutex_t *mutex = args->mutex; pthread_mutex_t *mutex = args->mutex;
@ -56,7 +57,7 @@ void *block_input(struct input_args *args)
return nullptr; return nullptr;
} }
bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex) bool CreateInputThread(struct InputBuffer *buf, pthread_mutex_t *mutex)
{ {
struct input_args *args = malloc(sizeof(struct input_args)); struct input_args *args = malloc(sizeof(struct input_args));
*args = (struct input_args) { *args = (struct input_args) {
@ -65,5 +66,5 @@ bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex)
}; };
pthread_t thread; pthread_t thread;
return pthread_create(&thread, nullptr, block_input, args) == 0; return pthread_create(&thread, nullptr, input_thread_loop, args) == 0;
} }

View file

@ -35,6 +35,7 @@ struct Joystick {
i32 x; i32 x;
i32 y; i32 y;
}; };
union InputData { union InputData {
struct Button input_but; struct Button input_but;
struct Axis input_axis; struct Axis input_axis;
@ -67,6 +68,6 @@ struct InputBuffer {
void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest); void InputBufferTransfer(struct InputBuffer *tmp, struct InputBuffer *dest);
void InputBufferCopy(struct InputBuffer *buf, struct InputRecord *src); void InputBufferAdd(struct InputBuffer *buf, struct InputRecord *src);
bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex); bool InputStart(struct InputBuffer *buf, pthread_mutex_t *mutex);

View file

@ -1,4 +1,30 @@
#pragma once #pragma once
#include <iso646.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
#include "gametime.h"
#ifdef _WIN32 #ifdef _WIN32
#include "win.h" #include "win.h"
#endif #endif
enum PlatformError {
PLTF_E_INITFAIL,
};
struct Error {
int e;
};
bool PlatformInit();
bool PlatformGetRefreshRate(u16f *out);
bool PlatformReadInput(struct InputBuffer *buf);
bool PlatformWait(struct Time relative);

View file

@ -1,13 +1,9 @@
#include <iso646.h> #include "win.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> #include <windows.h>
#include "fumotris.h"
#include "ctrl.h"
#include "gametime.h" #include "gametime.h"
#include "input.h"
static struct windows { static struct windows {
HANDLE timer; HANDLE timer;
@ -121,22 +117,22 @@ bool PlatformReadInput(struct InputBuffer *buf)
if (!ReadConsoleInputW(win.input_handle, win_buf, max_records, &filled)) if (!ReadConsoleInputW(win.input_handle, win_buf, max_records, &filled))
return false; return false;
struct InputRecord rec = { .timestamp = TimeNow() }; struct InputRecord rec = { .time = TimeNow() };
for (size_t i = 0; i < filled; i++) { for (size_t i = 0; i < filled; i++) {
if (!copy_rec(&rec, win_buf + i)) if (!copy_rec(&rec, win_buf + i))
continue; continue;
InputBufferCopy(buf, &rec); InputBufferAdd(buf, &rec);
} }
return true; return true;
} }
bool PlatformWait(struct timespec relative) bool PlatformWait(struct Time relative)
{ {
LARGE_INTEGER duration; LARGE_INTEGER duration;
duration.QuadPart = -10000000 * relative.tv_sec - relative.tv_nsec / 100; duration.QuadPart = -10000000 * relative.sec - relative.nsec / 100;
if (!SetWaitableTimer(win.timer, &duration, 0, NULL,NULL, FALSE)) if (!SetWaitableTimer(win.timer, &duration, 0, NULL,NULL, FALSE))
return false; return false;

View file

@ -4,14 +4,5 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include "fumotris.h" #include "fumotris.h"
bool PlatformInit();
bool PlatformGetRefreshRate(u16f *out);
bool PlatformReadInput(struct InputBuffer *buf);
bool PlatformWait(struct timespec relative);

View file

@ -14,127 +14,6 @@
#include "event.h" #include "event.h"
#include "platform.h" #include "platform.h"
struct Instance {
struct Controller ctrl;
struct Terminal term;
struct Delegate on_start;
struct Delegate on_update;
struct Delegate on_draw;
};
const u8 I[16] = {
0, 0, 0, 0,
0, 0, 0, 0,
1, 1, 1, 1,
0, 0, 0, 0
};
const u8 O[4] = {
1, 1,
1, 1
};
const u8 T[9] = {
0, 1, 0,
1, 1, 1,
0, 0, 0
};
const u8 S[9] = {
0, 1, 1,
1, 1, 0,
0, 0, 0
};
const u8 Z[9] = {
1, 1, 0,
0, 1, 1,
0, 0, 0
};
const u8 J[9] = {
1, 0, 0,
1, 1, 1,
0, 0, 0
};
const u8 L[9] = {
0, 0, 1,
1, 1, 1,
0, 0, 0
};
struct CtrlBind {
enum CtrlCode code;
u16 bind;
u8 type;
};
const size_t code_count = 12;
const struct CtrlBind ctrl_binds[12] = {
{ LEFT, 0x25, BUTTON },
{ RIGHT, 0x27, BUTTON },
{ SOFT_DROP, 0x28, BUTTON },
{ HARD_DROP, 0x20, BUTTON },
{ ROTATE_CCW, 'Z', BUTTON },
{ ROTATE_CW, 'X', BUTTON },
{ ROTATE_180, 'A', BUTTON },
{ SWAP, 'C', BUTTON },
{ ESC, 0x1B, BUTTON },
{ VSCROLL, 0, AXIS },
{ HSCROLL, 1, AXIS },
{ MOUSE, 0, JOYSTICK }
};
void *Update(void *args)
{
struct Instance *game = args;
while (true) {
// Input
CtrlPoll(&game->ctrl);
if (CtrlGet(&game->ctrl, LEFT, BUTTON)->is_down)
printf("left down this frame\n");
// Game logic
//Invoke(&game->on_update, game);
// Draw
//TermOut(&game->term);
//puts(game->term.buf);
}
}
void Loop(struct Instance *game)
{
pthread_t update_thread;
pthread_create(&update_thread, nullptr, Update, (void *)game);
}
bool Start(struct Instance *game)
{
if (!NewCtrl(&game->ctrl))
return false;
for (size_t i = 0; i < code_count; i++) {
const struct CtrlBind *bind = &ctrl_binds[i];
CtrlMap(&game->ctrl, bind->code, bind->bind, bind->type);
}
if (!NewTerm(&game->term, 20, 20))
return false;
if (!NewDelegate(&game->on_start, 16))
return false;
if (!NewDelegate(&game->on_update, 16))
return false;
if (!NewDelegate(&game->on_draw, 16))
return false;
return true;
}
int main() int main()
{ {
struct Instance game; struct Instance game;
@ -144,7 +23,7 @@ int main()
if(!PlatformInit()) if(!PlatformInit())
exit(1); exit(1);
InputStart(&game.ctrl.buf, ); CreateInputThread(&game.ctrl.buf, );
Loop(&game); Loop(&game);
return 0; return 0;