From 2432443ea618be72f1844c64dee2f057f1ff14f6 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Fri, 17 Jul 2026 09:11:19 -0500 Subject: [PATCH] Cleaned settings a bit --- src/dusk/engine/engine.c | 3 +- src/dusk/input/input.c | 65 -------------------- src/dusk/input/input.h | 17 ----- src/dusk/input/inputaction.h | 10 --- src/dusk/save/CMakeLists.txt | 1 + src/dusk/save/savesettings.c | 46 ++++++++++++++ src/dusk/save/savesettings.h | 29 +++++++++ src/dusk/ui/frame/settings/uisettingsinput.c | 3 +- 8 files changed, 80 insertions(+), 94 deletions(-) create mode 100644 src/dusk/save/savesettings.c create mode 100644 src/dusk/save/savesettings.h diff --git a/src/dusk/engine/engine.c b/src/dusk/engine/engine.c index ed3188b8..d3a32567 100644 --- a/src/dusk/engine/engine.c +++ b/src/dusk/engine/engine.c @@ -21,6 +21,7 @@ #include "system/system.h" #include "console/console.h" #include "save/save.h" +#include "save/savesettings.h" engine_t ENGINE; @@ -39,7 +40,7 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) { errorChain(inputInit()); errorChain(assetInit()); errorChain(saveInit()); - errorChain(inputLoadSettings()); + errorChain(saveSettingsLoad()); errorChain(localeManagerInit()); errorChain(displayInit()); errorChain(uiInit()); diff --git a/src/dusk/input/input.c b/src/dusk/input/input.c index 4d23c37b..d019f4fc 100644 --- a/src/dusk/input/input.c +++ b/src/dusk/input/input.c @@ -11,8 +11,6 @@ #include "util/string.h" #include "util/math.h" #include "time/time.h" -#include "event/event.h" -#include "save/save.h" input_t INPUT; @@ -24,20 +22,6 @@ errorret_t inputInit(void) { INPUT.actions[i].action = (inputaction_t)i; INPUT.actions[i].lastValue = 0.0f; INPUT.actions[i].currentValue = 0.0f; - - eventInit( - &INPUT.actions[i].onPressed, - INPUT.actions[i].onPressedCallbacks, - INPUT.actions[i].onPressedUsers, - INPUT_ACTION_CALLBACK_COUNT_MAX - ); - - eventInit( - &INPUT.actions[i].onReleased, - INPUT.actions[i].onReleasedCallbacks, - INPUT.actions[i].onReleasedUsers, - INPUT_ACTION_CALLBACK_COUNT_MAX - ); } #ifdef inputInitPlatform @@ -47,43 +31,6 @@ errorret_t inputInit(void) { errorOk(); } -errorret_t inputSaveSettings(void) { - yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); - yyjson_mut_val *root = yyjson_mut_obj(doc); - yyjson_mut_doc_set_root(doc, root); - - yyjson_mut_val *settings = yyjson_mut_obj(doc); - yyjson_mut_obj_add_val(doc, root, "settings", settings); - - yyjson_mut_val *inputSettings = yyjson_mut_obj(doc); - yyjson_mut_obj_add_val(doc, settings, "input", inputSettings); - - yyjson_mut_obj_add_real(doc, inputSettings, "deadzone", INPUT.deadzone); - - errorret_t ret = saveWrite(SAVE_SETTINGS_SLOT, doc); - yyjson_mut_doc_free(doc); - - if(errorIsNotOk(ret)) return ret; - errorOk(); -} - -errorret_t inputLoadSettings(void) { - yyjson_doc *doc = NULL; - errorChain(saveLoad(SAVE_SETTINGS_SLOT, &doc)); - - if(!doc) errorOk(); - - yyjson_val *root = yyjson_doc_get_root(doc); - yyjson_val *settings = yyjson_obj_get(root, "settings"); - yyjson_val *inputSettings = yyjson_obj_get(settings, "input"); - yyjson_val *deadzone = yyjson_obj_get(inputSettings, "deadzone"); - - if(deadzone) INPUT.deadzone = (float_t)yyjson_get_num(deadzone); - - yyjson_doc_free(doc); - errorOk(); -} - void inputUpdate(void) { #ifdef inputUpdatePlatform inputUpdatePlatform(); @@ -136,18 +83,6 @@ void inputUpdate(void) { cur++; } - - #ifdef DUSK_TIME_DYNAMIC - if(TIME.dynamicUpdate) return; - #endif - - for(uint8_t i = INPUT_ACTION_NULL + 1; i < INPUT_ACTION_COUNT; i++) { - inputactiondata_t *act = &INPUT.actions[i]; - bool_t isDown = act->currentValue > 0.0f; - bool_t wasDown = act->lastValue > 0.0f; - if(isDown && !wasDown) eventInvoke(&act->onPressed, act); - if(!isDown && wasDown) eventInvoke(&act->onReleased, act); - } } float_t inputGetCurrentValue(const inputaction_t action) { diff --git a/src/dusk/input/input.h b/src/dusk/input/input.h index 29fbfe10..06093eef 100644 --- a/src/dusk/input/input.h +++ b/src/dusk/input/input.h @@ -37,23 +37,6 @@ errorret_t inputInit(void); */ void inputUpdate(void); -/** - * Persists the current input settings (currently just the gamepad - * deadzone) to the save system as JSON, under settings.input. - * - * @return An error code if serialization or the write fails. - */ -errorret_t inputSaveSettings(void); - -/** - * Loads input settings (currently just the gamepad deadzone) from the - * save system's JSON payload under settings.input and applies them to - * INPUT. Leaves INPUT unchanged if no settings file exists yet. - * - * @return An error code if the load or parse fails. - */ -errorret_t inputLoadSettings(void); - /** * Gets the current value of a specific input action. * diff --git a/src/dusk/input/inputaction.h b/src/dusk/input/inputaction.h index f094a42a..5398465f 100644 --- a/src/dusk/input/inputaction.h +++ b/src/dusk/input/inputaction.h @@ -8,9 +8,6 @@ #pragma once #include "time/time.h" #include "input/inputactiondefs.h" -#include "event/event.h" - -#define INPUT_ACTION_CALLBACK_COUNT_MAX 4 typedef struct { inputaction_t action; @@ -21,13 +18,6 @@ typedef struct { float_t lastDynamicValue; float_t currentDynamicValue; #endif - - eventcallback_t onPressedCallbacks[INPUT_ACTION_CALLBACK_COUNT_MAX]; - void *onPressedUsers[INPUT_ACTION_CALLBACK_COUNT_MAX]; - event_t onPressed; - eventcallback_t onReleasedCallbacks[INPUT_ACTION_CALLBACK_COUNT_MAX]; - void *onReleasedUsers[INPUT_ACTION_CALLBACK_COUNT_MAX]; - event_t onReleased; } inputactiondata_t; /** diff --git a/src/dusk/save/CMakeLists.txt b/src/dusk/save/CMakeLists.txt index 55b35252..a79d665a 100644 --- a/src/dusk/save/CMakeLists.txt +++ b/src/dusk/save/CMakeLists.txt @@ -8,4 +8,5 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME} PUBLIC save.c savestream.c + savesettings.c ) diff --git a/src/dusk/save/savesettings.c b/src/dusk/save/savesettings.c new file mode 100644 index 00000000..8cd39a4b --- /dev/null +++ b/src/dusk/save/savesettings.c @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "save/savesettings.h" +#include "save/save.h" +#include "input/input.h" + +errorret_t saveSettingsWrite(void) { + yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); + yyjson_mut_val *root = yyjson_mut_obj(doc); + yyjson_mut_doc_set_root(doc, root); + + yyjson_mut_val *settings = yyjson_mut_obj(doc); + yyjson_mut_obj_add_val(doc, root, "settings", settings); + + yyjson_mut_val *input = yyjson_mut_obj(doc); + yyjson_mut_obj_add_val(doc, settings, "input", input); + yyjson_mut_obj_add_real(doc, input, "deadzone", INPUT.deadzone); + + errorret_t ret = saveWrite(SAVE_SETTINGS_SLOT, doc); + yyjson_mut_doc_free(doc); + + if(errorIsNotOk(ret)) return ret; + errorOk(); +} + +errorret_t saveSettingsLoad(void) { + yyjson_doc *doc = NULL; + errorChain(saveLoad(SAVE_SETTINGS_SLOT, &doc)); + + if(!doc) errorOk(); + + yyjson_val *root = yyjson_doc_get_root(doc); + yyjson_val *settings = yyjson_obj_get(root, "settings"); + yyjson_val *input = yyjson_obj_get(settings, "input"); + yyjson_val *deadzone = yyjson_obj_get(input, "deadzone"); + + if(deadzone) INPUT.deadzone = (float_t)yyjson_get_num(deadzone); + + yyjson_doc_free(doc); + errorOk(); +} diff --git a/src/dusk/save/savesettings.h b/src/dusk/save/savesettings.h new file mode 100644 index 00000000..cb363d5c --- /dev/null +++ b/src/dusk/save/savesettings.h @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "error/error.h" + +/** + * Gathers all game settings (currently just the input gamepad deadzone) + * from their owning runtime state and persists them to the save system + * as JSON, under settings.. Nothing is retained after this call + * returns - the JSON is built and written fresh every time. + * + * @return An error code if serialization or the write fails. + */ +errorret_t saveSettingsWrite(void); + +/** + * Loads all game settings from the save system's JSON payload and + * applies them directly to their owning runtime state (e.g. + * INPUT.deadzone). Leaves runtime state unchanged if no settings file + * exists yet. Nothing is retained after this call returns. + * + * @return An error code if the load or parse fails. + */ +errorret_t saveSettingsLoad(void); diff --git a/src/dusk/ui/frame/settings/uisettingsinput.c b/src/dusk/ui/frame/settings/uisettingsinput.c index f0a83693..b22c7976 100644 --- a/src/dusk/ui/frame/settings/uisettingsinput.c +++ b/src/dusk/ui/frame/settings/uisettingsinput.c @@ -12,6 +12,7 @@ #include "locale/localemanager.h" #include "asset/loader/locale/assetlocaleloader.h" #include "input/input.h" +#include "save/savesettings.h" void uiSettingsInputSelected( const uimenu_t *menu, @@ -66,7 +67,7 @@ void uiSettingsInputApply(void) { &UI_SETTINGS.data.input.items[UI_SETTINGS_INPUT_INDEX_DEADZONE].slider ); #endif - errorCatch(errorPrint(inputSaveSettings())); + errorCatch(errorPrint(saveSettingsWrite())); uiMenuClose(&UI_SETTINGS.data.input.menu); }