a
This commit is contained in:
Julia 2024-04-30 16:41:31 -05:00
parent 9db68c9754
commit 1dcea1a2cf
27 changed files with 139 additions and 148 deletions

View file

@ -3,7 +3,8 @@
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
"${workspaceFolder}/**",
"${workspaceFolder}/source/fumoengine/input"
],
"defines": [
"_DEBUG",

View file

@ -57,7 +57,8 @@
"streambuf": "cpp",
"typeinfo": "cpp",
"execution": "cpp",
"stdbool.h": "c"
"stdbool.h": "c",
"fumoengine.h": "c"
}
}
}

View file

@ -1,7 +1,11 @@
#include "gametime.h"
#include "fumocommon.h"
#include <time.h>
#define ONE_E_9 1000000000
size_t MinSize(size_t a, size_t b)
{
return a < b ? a : b;
}
Time TimeNow()
{

View file

@ -1,9 +1,10 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#define nullptr ((void *)0)
#define ONE_E_9 1000000000
typedef uint8_t u8;
typedef uint_fast8_t u8f;
@ -31,47 +32,11 @@ typedef int64_t i64;
typedef int_fast64_t i64f;
size_t min_size(size_t a, size_t b);
typedef u64 Time;
/*const u8 I[16] = {
0, 0, 0, 0,
0, 0, 0, 0,
1, 1, 1, 1,
0, 0, 0, 0
};
size_t MinSize(size_t a, size_t b);
const u8 O[4] = {
1, 1,
1, 1
};
Time TimeNow();
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
};*/
double TimeNowD();

View file

@ -1,17 +1,8 @@
#include <iso646.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ctrl.h"
#include "input.h"
#include "fumotris.h"
#include "term.h"
#include "event.h"
#include "platform.h"
#include "fumocommon.h"
void ErrorExit(char *message)
{
@ -19,32 +10,30 @@ void ErrorExit(char *message)
exit(1);
}
struct Game {
struct Event Start;
struct Event Draw;
struct Event Update;
struct FumoGame {
struct Controller ctrl;
struct InputHandle input_hand;
struct Event start;
struct Event draw;
struct Event update;
Time time;
};
struct Event hi;
int main()
bool FumoEngineInit(struct FumoGame *game)
{
if (!PlatformInit())
ErrorExit("Platform failed to initialize");
struct Controller ctrl;
if (!CreateCtrl(&ctrl))
if (!CreateCtrl(&game->ctrl))
ErrorExit("Out of memory");
CtrlMap(&ctrl, 0, BUTTON, 'A');
//CtrlMap(&ctrl, 0, BUTTON, 'A');
struct Game game;
CreateEvent(&game.Update);
CreateEvent(&game->update);
struct InputHandle input_hand;
if (!BeginInputThread(&input_hand, &ctrl.recs, &ctrl.str))
if (!BeginInputThread(&game->input_hand, &game->input_hand.recs, &ctrl.str))
ErrorExit("Input handle failed to initialize");
while (true) {
@ -56,9 +45,7 @@ int main()
if (!InputRelease(&input_hand))
ErrorExit("Release failed");
_sleep(100);
EventInvokeUpdate(&game.Update, 0);
EventInvokeUpdate(&game.update, 0);
}
return 0;

View file

@ -5,8 +5,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
#include "gametime.h"
#include "fumocommon.h"
union func {
void (*generic)(void *arg);

View file

@ -4,7 +4,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
#include "fumocommon.h"
typedef u32 hashtype;

View file

@ -5,7 +5,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
#include "fumocommon.h"
typedef u32 hashtype;

View file

@ -1,6 +1,8 @@
#include "ringbuffer.h"
#include <string.h>
void *get_ptr(RingBufferT T, struct RingBufferHead *head, size_t i)
{
u8 *bytes = (u8 *)head;
@ -35,7 +37,7 @@ void RingBufferTransfer(
struct RingBufferHead *dest,
struct RingBufferHead *tmp
) {
size_t copy_max = min_size(T->LEN - dest->len, tmp->len);
size_t copy_max = MinSize(T->LEN - dest->len, tmp->len);
for (size_t i = 0; i < copy_max; i++) {
void *to = RingBufferGet(T, dest, dest->len + i);
@ -53,7 +55,7 @@ size_t RingBufferOut(
void *dest,
struct RingBufferHead *src
) {
size_t copy_max = min_size(n, src->len);
size_t copy_max = MinSize(n, src->len);
for (size_t i = 0; i < copy_max; i++) {
void *to = (u8 *)dest + i * T->SIZE;

View file

@ -2,10 +2,9 @@
#include <iso646.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
#include "fumocommon.h"
typedef const struct RingBufferT {
size_t OFFSET;

View file

@ -4,6 +4,7 @@
#define INIT_SIZE 16
bool CreateCtrl(struct Controller *ctrl)
{
struct ctrl_bkt *code_bkts = calloc(INIT_SIZE, sizeof(struct ctrl_bkt));
@ -14,7 +15,6 @@ bool CreateCtrl(struct Controller *ctrl)
return false;
*ctrl = (struct Controller) {
.recs.head = RINGBUF_HEAD_INIT,
.pending_buf.len = 0,
.axis_vec = (struct ctrl_axis_vec) {
@ -144,7 +144,7 @@ void dispatch_update(struct InputAxis *axis, struct InputRecord *rec)
axis->data = rec->data;
}
void CtrlPoll(struct Controller *ctrl)
void CtrlPoll(struct Controller *ctrl, struct InputRecordBuf *recs)
{
for (size_t i = 0; i < ctrl->pending_buf.len; i++) {
struct InputAxis *axis = ctrl->pending_buf.axes[i];
@ -154,8 +154,8 @@ void CtrlPoll(struct Controller *ctrl)
}
ctrl->pending_buf.len = 0;
for (size_t i = 0; i < ctrl->recs.head.len; i++) {
struct InputRecord *rec = &ctrl->recs.buf[i];
for (size_t i = 0; i < recs->head.len; i++) {
struct InputRecord *rec = &recs->buf[i];
struct InputAxis *axis = find_axis(&ctrl->binds, rec->id);
if (axis == nullptr)
@ -165,12 +165,7 @@ void CtrlPoll(struct Controller *ctrl)
ctrl->pending_buf.axes[ctrl->pending_buf.len++] = axis;
}
ctrl->recs.head.len = 0;
}
size_t CtrlInputString(struct Controller *ctrl, size_t n, char *buf)
{
return RingBufferOut(&STR_BUF_T, n, buf, &ctrl->str.head);
recs->head.len = 0;
}
/*int main()

View file

@ -5,10 +5,10 @@
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
#include "gametime.h"
#include "fumocommon.h"
#include "input.h"
struct InputAxis {
Time last_pressed;
Time last_released;
@ -47,9 +47,6 @@ struct ctrl_axis_vec {
};
struct Controller {
struct InputRecordBuf recs;
struct InputStringBuf str;
struct {
struct InputAxis *axes[IO_BUF_SIZE];
u8f len;
@ -68,9 +65,7 @@ bool CtrlMap(struct Controller *ctrl, u16f code, u16f type, u16f bind);
struct InputAxis *CtrlGet(struct Controller *ctrl, u16f code, u16f type);
void CtrlPoll(struct Controller *ctrl);
size_t CtrlInputString(struct Controller *ctrl, size_t n, char *buf);
void CtrlPoll(struct Controller *ctrl, struct InputRecordBuf *recs);
enum ControlCode {
LEFT,

View file

@ -3,6 +3,7 @@
#include "platform.h"
const struct RingBufferT IO_BUF_T = {
.OFFSET = offsetof(struct InputRecordBuf, buf),
.SIZE = sizeof(struct InputRecord),
@ -15,6 +16,7 @@ const struct RingBufferT STR_BUF_T = {
.LEN = STR_BUF_SIZE
};
void *input_worker(void *arg)
{
struct InputHandle *hand = arg;
@ -110,4 +112,9 @@ bool InputRelease(struct InputHandle *hand)
return false;
return true;
}
size_t InputString(struct InputStringBuf *str, size_t n, char *buf)
{
return RingBufferOut(&STR_BUF_T, n, buf, &str->head);
}

View file

@ -6,13 +6,17 @@
#include <stdlib.h>
#include <pthread.h>
#include "fumotris.h"
#include "gametime.h"
#include "fumocommon.h"
#include "ringbuffer.h"
#define IO_BUF_SIZE 16
#define STR_BUF_SIZE (IO_BUF_SIZE * 4)
extern const struct RingBufferT IO_BUF_T;
extern const struct RingBufferT STR_BUF_T;
enum InputType {
BUTTON,
AXIS,
@ -32,12 +36,15 @@ union InputID {
u32 hash;
};
struct Button {
u64 value;
};
struct Axis {
i64 value;
};
struct Joystick {
i32 x;
i32 y;
@ -49,6 +56,7 @@ union InputData {
struct Joystick input_js;
};
struct InputRecord {
Time time;
@ -68,22 +76,20 @@ struct InputRecord {
};
};
extern const struct RingBufferT IO_BUF_T;
extern const struct RingBufferT STR_BUF_T;
struct InputBuffer {
struct {
struct RingBufferHead head;
struct InputRecord buf[IO_BUF_SIZE];
} recs;
struct InputRecordBuf {
struct RingBufferHead head;
struct InputRecord buf[IO_BUF_SIZE];
};
struct InputStringBuf {
struct RingBufferHead head;
char buf[STR_BUF_SIZE];
struct {
struct RingBufferHead head;
char buf[STR_BUF_SIZE];
} str;
};
struct InputHandle {
struct InputRecordBuf *recs;
struct InputStringBuf *str;
struct InputBuffer in;
pthread_t thread;
pthread_mutex_t mutex;
@ -95,12 +101,13 @@ struct InputHandle {
bool BeginInputThread(
struct InputHandle *hand,
struct InputRecordBuf *in,
struct InputStringBuf *str
struct InputBuffer *in,
);
bool EndInputThread(struct InputHandle *hand);
bool InputAquire(struct InputHandle *hand);
bool InputRelease(struct InputHandle *hand);
bool InputRelease(struct InputHandle *hand);
size_t InputString(struct Controller *ctrl, size_t n, char *buf);

View file

@ -1,5 +1,6 @@
#include "parseinput.h"
void ReadButton(struct InputRecord *rec, u16f bind, bool is_down)
{
rec->id = (union InputID) { .bind = bind, .type = BUTTON };

View file

@ -4,9 +4,10 @@
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
#include "fumocommon.h"
#include "input.h"
void ReadButton(struct InputRecord *rec, u16f bind, bool is_down);
void ReadAxis(struct InputRecord *rec, u16f bind, u64 value);

View file

@ -5,13 +5,13 @@
#include <stdio.h>
#include <stdlib.h>
#include "fumotris.h"
#include "gametime.h"
#include "fumocommon.h"
#ifdef _WIN32
#include "win.h"
#endif
bool PlatformInit();
bool PlatformGetRefreshRate(u16f *out);

View file

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

View file

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

View file

@ -1,13 +1,12 @@
#include <iso646.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "fumotris.h"
#include "fumocommon.h"
struct TChar4 {
char ch;

View file

@ -6,9 +6,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "fumotris.h"
#include "fumocommon.h"
struct TChar4 {
char ch;

View file

@ -1,6 +1,43 @@
#include "fumotris.h"
size_t min_size(size_t a, size_t b)
{
return a < b ? a : b;
}
/*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
};*/

View file

@ -6,9 +6,10 @@
#include <stdlib.h>
#include <string.h>
#include "fumotris.h"
#include "fumocommon.h"
#include "term.h"
struct TetrMap {
size_t wid;
size_t hgt;

View file

@ -6,9 +6,10 @@
#include <stdlib.h>
#include <string.h>
#include "fumotris.h"
#include "fumocommon.h"
#include "term.h"
struct TetrMap {
size_t wid;
size_t hgt;

View file

@ -1,11 +0,0 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "fumotris.h"
typedef u64 Time;
Time TimeNow();
double TimeNowD();

BIN
test.exe

Binary file not shown.