aa
This commit is contained in:
Julia 2024-05-09 11:59:33 -05:00
parent 7c58e46fc4
commit 41f57d5ba8
10 changed files with 164 additions and 114 deletions

View file

@ -59,5 +59,6 @@
"ringbuffer.h": "c", "ringbuffer.h": "c",
"fumoengine.h": "c", "fumoengine.h": "c",
"fumocommon.h": "c" "fumocommon.h": "c"
} },
"cmake.configureOnOpen": false
} }

37
.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,37 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:/mingw64/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "shell",
"label": "Fumo build script",
"command": "python build.py",
"group": {
"kind": "build",
"isDefault": true
},
}
],
"version": "2.0.0"
}

View file

@ -1,9 +1,12 @@
import os, sys import hashlib
import json, hashlib import json
import os
import subprocess
import sys
def walk_source(path): def walk_source_dir(path: str) -> tuple[list[str], list[str]]:
source_paths = [] source_paths : list[str] = []
subdirs = [] subdirs = []
for dirpath, dirnames, filenames in os.walk(path): for dirpath, dirnames, filenames in os.walk(path):
@ -55,8 +58,8 @@ def get_object_names(path):
return object_names return object_names
def build(source_path, obj_path, out_path, recompile = False): def build(source_path, obj_path, out_path, recompile):
source_paths, subdirs = walk_source(source_path) source_paths, subdirs = walk_source_dir(source_path)
if recompile: if recompile:
result = os.system(f"gcc {' '.join(source_paths)} -I {' -I '.join(subdirs)} -o {out_path} -pthread -Wall -std=c17 -pedantic") result = os.system(f"gcc {' '.join(source_paths)} -I {' -I '.join(subdirs)} -o {out_path} -pthread -Wall -std=c17 -pedantic")
@ -91,4 +94,4 @@ def build(source_path, obj_path, out_path, recompile = False):
print(os.system(f"gcc {obj_path}\\*.o -o {out_path} -pthread -Wall -std=c17 -pedantic -g")) print(os.system(f"gcc {obj_path}\\*.o -o {out_path} -pthread -Wall -std=c17 -pedantic -g"))
build(sys.argv[1], sys.argv[2], sys.argv[3], True) build("source/", "objects/", "debug", True)

View file

@ -32,44 +32,36 @@ void FreeController(struct Controller *ctrl)
FreeDictionary(&ctrl->binds); FreeDictionary(&ctrl->binds);
} }
u32 hash_bind(u16f bind, u16f type) u32 hash_bind(u16f code, u16f type)
{ {
return bind + (type << 16); return code | (type << 16);
} }
struct InputAxis *ControllerMap( bool ControllerBind(struct Controller *ctrl, u16 control, u16 code, u16 type)
struct Controller *ctrl, {
struct ControlMapping *map u32 hash = hash_bind(code, type);
) {
struct InputAxis *axis = &ctrl->axes[map->code];
u32 hash = hash_bind(map->bind, map->type); struct InputAxis *axis = &ctrl->axes[control];
struct InputAxis **bind = DictionarySet(BIND_T, &ctrl->binds, hash, axis); struct InputAxis **bind = DictionarySet(BIND_T, &ctrl->binds, hash, axis);
if (bind == nullptr) { if (bind == nullptr)
printf("whar");
exit(1);
}
*bind = axis;
axis->type = map->type;
return axis;
}
bool ControllerMapMulti(
struct Controller *ctrl,
usize n,
struct ControlMapping *maps,
struct InputAxis **binds
) {
for (usize i = 0; i < n; i++) {
struct InputAxis *axis = ControllerMap(ctrl, maps + i);
if (axis == nullptr)
return false; return false;
binds[i] = axis; *bind = axis;
return true;
}
bool ControllerBindMulti(
struct Controller *ctrl,
usize n,
u16 *controls,
u16 *codes,
u16 *types
) {
for (usize i = 0; i < n; i++) {
if (!ControllerBind(ctrl, controls[i], codes[i], types[i]))
return false;
} }
return true; return true;
@ -81,7 +73,7 @@ void dispatch_update(struct InputAxis *axis, struct InputRecord *rec)
axis->is_down = true; axis->is_down = true;
axis->is_held = true; axis->is_held = true;
axis->last_pressed = rec->time; axis->last_pressed = rec->time;
} else if (rec->is_up) { } else {
axis->is_up = true; axis->is_up = true;
axis->is_held = false; axis->is_held = false;
axis->last_released = rec->time; axis->last_released = rec->time;
@ -104,7 +96,7 @@ void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs)
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); u32 hash = hash_bind(rec->code, rec->type);
struct InputAxis *axis = DictionaryFind(BIND_T, &ctrl->binds, hash); struct InputAxis *axis = DictionaryFind(BIND_T, &ctrl->binds, hash);
if (axis == nullptr) if (axis == nullptr)

View file

@ -4,30 +4,6 @@
#include "input.h" #include "input.h"
struct ControlMapping {
u16 code;
u16 bind;
u16 type;
};
struct InputAxis {
nsec last_pressed;
nsec last_released;
union {
struct Button but;
struct Axis axis;
struct Joystick js;
union InputData data;
};
u16 type;
bool is_down;
bool is_held;
bool is_up;
};
struct Controller { struct Controller {
struct InputAxis *pending[IO_BUF_SIZE]; struct InputAxis *pending[IO_BUF_SIZE];
usize pending_len; usize pending_len;
@ -43,16 +19,14 @@ bool CreateController(struct Controller *ctrl);
void FreeController(struct Controller *ctrl); void FreeController(struct Controller *ctrl);
struct InputAxis *ControllerMap( bool ControllerBind(struct Controller *ctrl, u16 control, u16 code, u16 type);
struct Controller *ctrl,
struct ControlMapping *map
);
bool ControllerMapMulti( bool ControllerBindMulti(
struct Controller *ctrl, struct Controller *ctrl,
usize n, usize n,
struct ControlMapping *maps, u16 *controls,
struct InputAxis **axis_ptrs u16 *codes,
u16 *types
); );
void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs); void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs);

View file

@ -33,22 +33,28 @@ struct Joystick {
}; };
union InputData { union InputData {
struct Button input_but; struct Button but;
struct Axis input_axis; struct Axis axis;
struct Joystick input_js; struct Joystick js;
}; };
struct InputRecord { struct InputRecord {
nsec time; nsec time;
union {
struct Button but;
struct Axis axis;
struct Joystick js;
union InputData data; union InputData data;
u16 code;
u16 type;
bool is_down;
}; };
u16 bind; struct InputAxis {
nsec last_pressed;
nsec last_released;
union InputData data;
u16 type; u16 type;
bool is_down; bool is_down;

View file

@ -1,30 +1,29 @@
#include "parseinput.h" #include "parseinput.h"
void ReadButton(struct InputRecord *rec, u16f bind, bool is_down) void ReadButton(struct InputRecord *rec, u16f code, bool is_down)
{ {
rec->bind = bind; rec->code = code;
rec->type = BUTTON; rec->type = BUTTON;
rec->is_down = is_down; rec->is_down = is_down;
rec->is_up = !is_down;
} }
void ReadAxis(struct InputRecord *rec, u16f bind, u64 value) void ReadAxis(struct InputRecord *rec, u16f code, u64 value)
{ {
rec->bind = bind; rec->code = code;
rec->type = AXIS; rec->type = AXIS;
rec->axis.value = value; rec->data.axis.value = value;
} }
void ReadJoystick(struct InputRecord *rec, u16f bind, i32 x, i32 y) void ReadJoystick(struct InputRecord *rec, u16f code, i32 x, i32 y)
{ {
rec->bind = bind; rec->code = code;
rec->type = JOYSTICK; rec->type = JOYSTICK;
rec->js.x = x; rec->data.js.x = x;
rec->js.y = y; rec->data.js.y = y;
} }
size_t UCS2ToUTF8(char *buf, u16f ucs2) size_t UCS2ToUTF8(char *buf, u16f ucs2)
@ -40,8 +39,10 @@ size_t UCS2ToUTF8(char *buf, u16f ucs2)
return 2; return 2;
} }
else {
buf[0] = 0xE0 + (ucs2 >> 12); buf[0] = 0xE0 + (ucs2 >> 12);
buf[1] = 0x80 + ((ucs2 >> 6) & 0x3F); buf[1] = 0x80 + ((ucs2 >> 6) & 0x3F);
buf[2] = 0x80 + (ucs2 & 0x3F); buf[2] = 0x80 + (ucs2 & 0x3F);
return 3; return 3;
} }
}

View file

@ -2,9 +2,6 @@
struct Fumotris { struct Fumotris {
struct ControlMapping mappings[BINDS_N];
struct InputAxis *input[BINDS_N];
struct TetrMap board; struct TetrMap board;
struct TetrMap piece; struct TetrMap piece;
}; };
@ -15,7 +12,7 @@ void FumotrisStart(void *inst_arg, void *game_arg)
struct FumoInstance *inst = inst_arg; struct FumoInstance *inst = inst_arg;
struct Fumotris *game = game_arg; struct Fumotris *game = game_arg;
ControllerMapMulti(&inst->ctrl, BINDS_N, game->mappings, game->input); ControllerBindMulti(&inst->ctrl, BINDS_N, controls_g, codes_g, types_g);
CreateTetrMap(&game->board, 10, 10); CreateTetrMap(&game->board, 10, 10);
CreateTetrMap(&game->piece, 3, 3); CreateTetrMap(&game->piece, 3, 3);
@ -27,9 +24,10 @@ void FumotrisUpdate(void *inst_arg, void *game_arg)
struct FumoInstance *inst = inst_arg; struct FumoInstance *inst = inst_arg;
struct Fumotris *game = game_arg; struct Fumotris *game = game_arg;
if (game->input[LEFT]->is_down) if (inst->ctrl.axes[LEFT].is_down)
game->piece.x -= 1; game->piece.x -= 1;
if (game->input[RIGHT]->is_down)
if (inst->ctrl.axes[RIGHT].is_down)
game->piece.x += 1; game->piece.x += 1;
TetrMapDraw(&game->board, &inst->term); TetrMapDraw(&game->board, &inst->term);

View file

@ -5,7 +5,7 @@
#define BINDS_N 12 #define BINDS_N 12
enum FumotrisCode { enum FumotrisControls {
LEFT, LEFT,
RIGHT, RIGHT,
SOFT_DROP, SOFT_DROP,
@ -15,64 +15,102 @@ enum FumotrisCode {
ROTATE_180, ROTATE_180,
SWAP, SWAP,
ESC, ESC,
VSCROLL, VSCROLL,
HSCROLL, HSCROLL,
MOUSE MOUSE
}; };
struct ControlMapping mappings_global[BINDS_N] = { u16 controls_g[BINDS_N] = {
{ LEFT, 0x25, BUTTON }, LEFT,
{ RIGHT, 0x27, BUTTON }, RIGHT,
{ SOFT_DROP, 0x28, BUTTON }, SOFT_DROP,
{ HARD_DROP, 0x20, BUTTON }, HARD_DROP,
{ ROTATE_CCW, 'Z', BUTTON }, ROTATE_CCW,
{ ROTATE_CW, 'X', BUTTON }, ROTATE_CW,
{ ROTATE_180, 'A', BUTTON }, ROTATE_180,
{ SWAP, 'C', BUTTON }, SWAP,
{ ESC, 0x1B, BUTTON }, ESC,
{ VSCROLL, 0, AXIS },
{ HSCROLL, 1, AXIS }, VSCROLL,
{ MOUSE, 0, JOYSTICK } HSCROLL,
MOUSE,
}; };
const u8 I[16] = { u16 codes_g[BINDS_N] = {
0x25,
0x27,
0x28,
0x20,
'Z',
'X',
'A',
'C',
0x1B,
0,
1,
0
};
u16 types_g[BINDS_N] = {
BUTTON,
BUTTON,
BUTTON,
BUTTON,
BUTTON,
BUTTON,
BUTTON,
BUTTON,
BUTTON,
AXIS,
AXIS,
JOYSTICK
};
u8 I[16] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0 0, 0, 0, 0
}; };
const u8 O[4] = { u8 O[4] = {
1, 1, 1, 1,
1, 1 1, 1
}; };
const u8 T[9] = { u8 T[9] = {
0, 1, 0, 0, 1, 0,
1, 1, 1, 1, 1, 1,
0, 0, 0 0, 0, 0
}; };
const u8 S[9] = { u8 S[9] = {
0, 1, 1, 0, 1, 1,
1, 1, 0, 1, 1, 0,
0, 0, 0 0, 0, 0
}; };
const u8 Z[9] = { u8 Z[9] = {
1, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 1,
0, 0, 0 0, 0, 0
}; };
const u8 J[9] = { u8 J[9] = {
1, 0, 0, 1, 0, 0,
1, 1, 1, 1, 1, 1,
0, 0, 0 0, 0, 0
}; };
const u8 L[9] = { u8 L[9] = {
0, 0, 1, 0, 0, 1,
1, 1, 1, 1, 1, 1,
0, 0, 0 0, 0, 0

BIN
test.exe

Binary file not shown.