diff --git a/.vscode/settings.json b/.vscode/settings.json index 94cdde1..8b5a32d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -59,5 +59,6 @@ "ringbuffer.h": "c", "fumoengine.h": "c", "fumocommon.h": "c" - } + }, + "cmake.configureOnOpen": false } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..a785b23 --- /dev/null +++ b/.vscode/tasks.json @@ -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" +} \ No newline at end of file diff --git a/build.py b/build.py index 23b9c28..8e11286 100644 --- a/build.py +++ b/build.py @@ -1,9 +1,12 @@ -import os, sys -import json, hashlib +import hashlib +import json +import os +import subprocess +import sys -def walk_source(path): - source_paths = [] +def walk_source_dir(path: str) -> tuple[list[str], list[str]]: + source_paths : list[str] = [] subdirs = [] for dirpath, dirnames, filenames in os.walk(path): @@ -55,8 +58,8 @@ def get_object_names(path): return object_names -def build(source_path, obj_path, out_path, recompile = False): - source_paths, subdirs = walk_source(source_path) +def build(source_path, obj_path, out_path, recompile): + source_paths, subdirs = walk_source_dir(source_path) if recompile: 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")) -build(sys.argv[1], sys.argv[2], sys.argv[3], True) \ No newline at end of file +build("source/", "objects/", "debug", True) \ No newline at end of file diff --git a/source/fumoengine/input/ctrl.c b/source/fumoengine/input/ctrl.c index b7f994a..deba581 100644 --- a/source/fumoengine/input/ctrl.c +++ b/source/fumoengine/input/ctrl.c @@ -32,44 +32,36 @@ void FreeController(struct Controller *ctrl) 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( - struct Controller *ctrl, - struct ControlMapping *map -) { - struct InputAxis *axis = &ctrl->axes[map->code]; +bool ControllerBind(struct Controller *ctrl, u16 control, u16 code, u16 type) +{ + u32 hash = hash_bind(code, type); - u32 hash = hash_bind(map->bind, map->type); + struct InputAxis *axis = &ctrl->axes[control]; struct InputAxis **bind = DictionarySet(BIND_T, &ctrl->binds, hash, axis); - if (bind == nullptr) { - printf("whar"); - exit(1); - } + if (bind == nullptr) + return false; *bind = axis; - axis->type = map->type; - return axis; + return true; } -bool ControllerMapMulti( +bool ControllerBindMulti( struct Controller *ctrl, usize n, - struct ControlMapping *maps, - struct InputAxis **binds + u16 *controls, + u16 *codes, + u16 *types ) { for (usize i = 0; i < n; i++) { - struct InputAxis *axis = ControllerMap(ctrl, maps + i); - - if (axis == nullptr) + if (!ControllerBind(ctrl, controls[i], codes[i], types[i])) return false; - - binds[i] = axis; } return true; @@ -81,7 +73,7 @@ void dispatch_update(struct InputAxis *axis, struct InputRecord *rec) axis->is_down = true; axis->is_held = true; axis->last_pressed = rec->time; - } else if (rec->is_up) { + } else { axis->is_up = true; axis->is_held = false; 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++) { 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); if (axis == nullptr) diff --git a/source/fumoengine/input/ctrl.h b/source/fumoengine/input/ctrl.h index 24030c9..fce02e0 100644 --- a/source/fumoengine/input/ctrl.h +++ b/source/fumoengine/input/ctrl.h @@ -4,30 +4,6 @@ #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 InputAxis *pending[IO_BUF_SIZE]; usize pending_len; @@ -43,16 +19,14 @@ bool CreateController(struct Controller *ctrl); void FreeController(struct Controller *ctrl); -struct InputAxis *ControllerMap( - struct Controller *ctrl, - struct ControlMapping *map -); +bool ControllerBind(struct Controller *ctrl, u16 control, u16 code, u16 type); -bool ControllerMapMulti( +bool ControllerBindMulti( struct Controller *ctrl, usize n, - struct ControlMapping *maps, - struct InputAxis **axis_ptrs + u16 *controls, + u16 *codes, + u16 *types ); void ControllerPoll(struct Controller *ctrl, struct RecordBuffer *recs); diff --git a/source/fumoengine/input/input.h b/source/fumoengine/input/input.h index ae4e158..e7f91ce 100644 --- a/source/fumoengine/input/input.h +++ b/source/fumoengine/input/input.h @@ -33,24 +33,30 @@ struct Joystick { }; union InputData { - struct Button input_but; - struct Axis input_axis; - struct Joystick input_js; + struct Button but; + struct Axis axis; + struct Joystick js; }; struct InputRecord { nsec time; - union { - struct Button but; - struct Axis axis; - struct Joystick js; - union InputData data; - }; + union InputData data; - u16 bind; + u16 code; u16 type; + bool is_down; +}; + +struct InputAxis { + nsec last_pressed; + nsec last_released; + + union InputData data; + + u16 type; + bool is_down; bool is_held; bool is_up; diff --git a/source/fumoengine/input/platforms/parseinput.c b/source/fumoengine/input/platforms/parseinput.c index b92717e..d6e6bb8 100644 --- a/source/fumoengine/input/platforms/parseinput.c +++ b/source/fumoengine/input/platforms/parseinput.c @@ -1,30 +1,29 @@ #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->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->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->js.x = x; - rec->js.y = y; + rec->data.js.x = x; + rec->data.js.y = y; } size_t UCS2ToUTF8(char *buf, u16f ucs2) @@ -40,8 +39,10 @@ size_t UCS2ToUTF8(char *buf, u16f ucs2) return 2; } - buf[0] = 0xE0 + (ucs2 >> 12); - buf[1] = 0x80 + ((ucs2 >> 6) & 0x3F); - buf[2] = 0x80 + (ucs2 & 0x3F); - return 3; + else { + buf[0] = 0xE0 + (ucs2 >> 12); + buf[1] = 0x80 + ((ucs2 >> 6) & 0x3F); + buf[2] = 0x80 + (ucs2 & 0x3F); + return 3; + } } \ No newline at end of file diff --git a/source/fumotris/fumotris.c b/source/fumotris/fumotris.c index 155efd1..ef7cfcc 100644 --- a/source/fumotris/fumotris.c +++ b/source/fumotris/fumotris.c @@ -2,9 +2,6 @@ struct Fumotris { - struct ControlMapping mappings[BINDS_N]; - struct InputAxis *input[BINDS_N]; - struct TetrMap board; struct TetrMap piece; }; @@ -15,7 +12,7 @@ void FumotrisStart(void *inst_arg, void *game_arg) struct FumoInstance *inst = inst_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->piece, 3, 3); @@ -27,9 +24,10 @@ void FumotrisUpdate(void *inst_arg, void *game_arg) struct FumoInstance *inst = inst_arg; struct Fumotris *game = game_arg; - if (game->input[LEFT]->is_down) + if (inst->ctrl.axes[LEFT].is_down) game->piece.x -= 1; - if (game->input[RIGHT]->is_down) + + if (inst->ctrl.axes[RIGHT].is_down) game->piece.x += 1; TetrMapDraw(&game->board, &inst->term); diff --git a/source/fumotris/fumotris.h b/source/fumotris/fumotris.h index 27cdfaa..de2d6f8 100644 --- a/source/fumotris/fumotris.h +++ b/source/fumotris/fumotris.h @@ -5,7 +5,7 @@ #define BINDS_N 12 -enum FumotrisCode { +enum FumotrisControls { LEFT, RIGHT, SOFT_DROP, @@ -15,64 +15,102 @@ enum FumotrisCode { ROTATE_180, SWAP, ESC, + VSCROLL, HSCROLL, + MOUSE }; -struct ControlMapping mappings_global[BINDS_N] = { - { 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 } +u16 controls_g[BINDS_N] = { + LEFT, + RIGHT, + SOFT_DROP, + HARD_DROP, + ROTATE_CCW, + ROTATE_CW, + ROTATE_180, + SWAP, + ESC, + + VSCROLL, + 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, 1, 1, 1, 1, 0, 0, 0, 0 }; -const u8 O[4] = { +u8 O[4] = { 1, 1, 1, 1 }; -const u8 T[9] = { +u8 T[9] = { 0, 1, 0, 1, 1, 1, 0, 0, 0 }; -const u8 S[9] = { +u8 S[9] = { 0, 1, 1, 1, 1, 0, 0, 0, 0 }; -const u8 Z[9] = { +u8 Z[9] = { 1, 1, 0, 0, 1, 1, 0, 0, 0 }; -const u8 J[9] = { +u8 J[9] = { 1, 0, 0, 1, 1, 1, 0, 0, 0 }; -const u8 L[9] = { +u8 L[9] = { 0, 0, 1, 1, 1, 1, 0, 0, 0 diff --git a/test.exe b/test.exe index 4ff9e84..cd52a73 100644 Binary files a/test.exe and b/test.exe differ