From 3c3c971726974d314166bda8eacf1d4fb1dd0c19 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sat, 28 May 2022 07:47:18 +0800 Subject: [PATCH] Optimize config loading Determine config path only once. --- src/packages/game/IZ_app.c | 15 ++++++---- src/packages/game/input/IZ_joystick.c | 42 +++++++++++++++++---------- src/packages/game/input/IZ_joystick.h | 5 ++-- src/packages/game/input/IZ_keyboard.c | 31 +++++++++++--------- src/packages/game/input/IZ_keyboard.h | 5 ++-- src/packages/game/output/IZ_video.c | 10 ++----- src/packages/game/output/IZ_video.h | 4 +-- 7 files changed, 61 insertions(+), 51 deletions(-) diff --git a/src/packages/game/IZ_app.c b/src/packages/game/IZ_app.c index 5d7fead..e51721e 100644 --- a/src/packages/game/IZ_app.c +++ b/src/packages/game/IZ_app.c @@ -1,16 +1,19 @@ #include "IZ_app.h" int IZ_InitializeApp(IZ_App* app) { - IZ_LoadVideoConfig(&app->video_config); - IZ_SaveVideoConfig(&app->video_config); + char config_path[128]; + IZ_GetConfigPath(config_path, 128); + + IZ_LoadVideoConfig(config_path, &app->video_config); + IZ_SaveVideoConfig(config_path, &app->video_config); app->video_update_at = 0u; for (uint8_t p = 0; p < PLAYERS; p += 1) { - IZ_LoadKeyboardConfig(&app->keyboard_state->config, p); - IZ_SaveKeyboardConfig(&app->keyboard_state->config, p); + IZ_LoadKeyboardConfig(config_path, &app->keyboard_state->config, p); + IZ_SaveKeyboardConfig(config_path, &app->keyboard_state->config, p); - IZ_LoadJoystickConfig(&app->joystick_state->config, p); - IZ_SaveJoystickConfig(&app->joystick_state->config, p); + IZ_LoadJoystickConfig(config_path, &app->joystick_state->config, p); + IZ_SaveJoystickConfig(config_path, &app->joystick_state->config, p); app->actions[p] = 0; } diff --git a/src/packages/game/input/IZ_joystick.c b/src/packages/game/input/IZ_joystick.c index ee00cfe..ae84d49 100644 --- a/src/packages/game/input/IZ_joystick.c +++ b/src/packages/game/input/IZ_joystick.c @@ -89,32 +89,44 @@ void IZ_HandleJoystickEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* ac IZ_HandleJoystickButtonEvents(e, state, action); } -void IZ_LoadJoystickConfig(IZ_JoystickConfig* config, uint8_t p) { - char config_path[128]; - IZ_GetConfigPath(config_path, 128); +void IZ_LoadJoystickConfig(const char* config_path, IZ_JoystickConfig* config, uint8_t player_index) { + char joystick_control_mapping_section_name[26]; + sprintf_s(joystick_control_mapping_section_name, 26, "Joystick.%d.ControlMapping", player_index); - char joystick_control_mapping_section_name[] = "Joystick.0.ControlMapping"; - joystick_control_mapping_section_name[9] = (char) (48 + p); for (uint8_t i = 4; i < CONTROLS; i += 1) { - config->control_mapping[i] = ini_getl(joystick_control_mapping_section_name, ACTION_NAMES[i], IZ_DEFAULT_JOYSTICK_CONTROLS[p][i], config_path); + config->control_mapping[i] = ini_getl(joystick_control_mapping_section_name, ACTION_NAMES[i], IZ_DEFAULT_JOYSTICK_CONTROLS[player_index][i], config_path); } char joystick_section_name[] = "Joystick.0"; - joystick_section_name[9] = (char) (48 + p); + joystick_section_name[9] = (char) (48 + player_index); config->axis_threshold = ini_getl(joystick_section_name, "AxisThreshold", 8000, config_path); } -void IZ_SaveJoystickConfig(IZ_JoystickConfig* config, uint8_t p) { - char config_path[128]; - IZ_GetConfigPath(config_path, 128); +IZ_ProcedureResult IZ_SaveJoystickConfig(const char* config_path, IZ_JoystickConfig* config, uint8_t player_index) { + char joystick_control_mapping_section_name[26]; + sprintf_s(joystick_control_mapping_section_name, 26, "Joystick.%d.ControlMapping", player_index); - char joystick_control_mapping_section_name[] = "Joystick.0.ControlMapping"; - joystick_control_mapping_section_name[9] = (char) (48 + p); for (uint8_t i = 4; i < CONTROLS; i += 1) { - ini_putl(joystick_control_mapping_section_name, ACTION_NAMES[i], config->control_mapping[i], config_path); + if (!ini_putl( + joystick_control_mapping_section_name, + ACTION_NAMES[i], + config->control_mapping[i], + config_path) + ) { + return 1; + } } char joystick_section_name[] = "Joystick.0"; - joystick_section_name[9] = (char) (48 + p); - ini_putl(joystick_section_name, "AxisThreshold", config->axis_threshold, config_path); + joystick_section_name[9] = (char) (48 + player_index); + if (!ini_putl( + joystick_section_name, + "AxisThreshold", + config->axis_threshold, + config_path) + ) { + return 1; + } + + return 0; } diff --git a/src/packages/game/input/IZ_joystick.h b/src/packages/game/input/IZ_joystick.h index 8342fb0..642abd0 100644 --- a/src/packages/game/input/IZ_joystick.h +++ b/src/packages/game/input/IZ_joystick.h @@ -5,7 +5,6 @@ #include #include #include "../IZ_action.h" -#include "../IZ_config.h" typedef uint8_t IZ_PadButton; @@ -48,9 +47,9 @@ static IZ_PadButton IZ_DEFAULT_JOYSTICK_CONTROLS[PLAYERS][CONTROLS] = { }, }; -void IZ_LoadJoystickConfig(IZ_JoystickConfig*, uint8_t); +void IZ_LoadJoystickConfig(const char*, IZ_JoystickConfig*, uint8_t); -void IZ_SaveJoystickConfig(IZ_JoystickConfig*, uint8_t); +IZ_ProcedureResult IZ_SaveJoystickConfig(const char*, IZ_JoystickConfig*, uint8_t); void IZ_HandleJoystickEvents(SDL_Event, IZ_JoystickState*, IZ_Action*); diff --git a/src/packages/game/input/IZ_keyboard.c b/src/packages/game/input/IZ_keyboard.c index bfdaf33..fbb9122 100644 --- a/src/packages/game/input/IZ_keyboard.c +++ b/src/packages/game/input/IZ_keyboard.c @@ -16,29 +16,32 @@ void IZ_HandleKeyboardEvents(SDL_Event e, IZ_KeyboardState* state, IZ_Action* ac } } -void IZ_SaveKeyboardConfig(IZ_KeyboardConfig* config, uint8_t p) { - char config_path[128]; - IZ_GetConfigPath(config_path, 128); - - char keyboard_section_name[] = "Keyboard.0.ControlMapping"; - keyboard_section_name[9] = (char) (48 + p); +IZ_ProcedureResult IZ_SaveKeyboardConfig(const char* config_path, IZ_KeyboardConfig* config, uint8_t player_index) { + char keyboard_section_name[26]; + sprintf_s(keyboard_section_name, 26, "Keyboard.%d.ControlMapping", player_index); for (uint8_t i = 0; i < CONTROLS; i += 1) { - ini_puts(keyboard_section_name, ACTION_NAMES[i], SDL_GetKeyName(config->control_mapping[i]), config_path); + if (!ini_puts( + keyboard_section_name, + ACTION_NAMES[i], + SDL_GetKeyName(config->control_mapping[i]), + config_path) + ) { + return 1; + } } -} -void IZ_LoadKeyboardConfig(IZ_KeyboardConfig* config, uint8_t p) { - char config_path[128]; - IZ_GetConfigPath(config_path, 128); + return 0; +} +void IZ_LoadKeyboardConfig(const char* config_path, IZ_KeyboardConfig* config, uint8_t player_index) { char buffer[128]; - char keyboard_section_name[] = "Keyboard.0.ControlMapping"; - keyboard_section_name[9] = (char) (48 + p); + char keyboard_section_name[26]; + sprintf_s(keyboard_section_name, 26, "Keyboard.%d.ControlMapping", player_index); for (uint8_t i = 0; i < CONTROLS; i += 1) { ini_gets( keyboard_section_name, ACTION_NAMES[i], - SDL_GetKeyName(IZ_DEFAULT_KEYBOARD_CONTROLS[p][i]), + SDL_GetKeyName(IZ_DEFAULT_KEYBOARD_CONTROLS[player_index][i]), buffer, 128, config_path diff --git a/src/packages/game/input/IZ_keyboard.h b/src/packages/game/input/IZ_keyboard.h index e023b73..3c4243a 100644 --- a/src/packages/game/input/IZ_keyboard.h +++ b/src/packages/game/input/IZ_keyboard.h @@ -5,7 +5,6 @@ #include #include #include "../IZ_action.h" -#include "../IZ_config.h" typedef struct { SDL_KeyCode control_mapping[CONTROLS]; @@ -36,9 +35,9 @@ static const SDL_KeyCode IZ_DEFAULT_KEYBOARD_CONTROLS[PLAYERS][CONTROLS] = { }, }; -void IZ_LoadKeyboardConfig(IZ_KeyboardConfig*, uint8_t); +void IZ_LoadKeyboardConfig(const char*, IZ_KeyboardConfig*, uint8_t); -void IZ_SaveKeyboardConfig(IZ_KeyboardConfig*, uint8_t); +IZ_ProcedureResult IZ_SaveKeyboardConfig(const char*, IZ_KeyboardConfig*, uint8_t); void IZ_HandleKeyboardEvents(SDL_Event, IZ_KeyboardState*, IZ_Action*); diff --git a/src/packages/game/output/IZ_video.c b/src/packages/game/output/IZ_video.c index 6a54230..b938201 100644 --- a/src/packages/game/output/IZ_video.c +++ b/src/packages/game/output/IZ_video.c @@ -1,9 +1,6 @@ #include "IZ_video.h" -IZ_ProcedureResult IZ_SaveVideoConfig(IZ_VideoConfig* config) { - char config_path[128]; - IZ_GetConfigPath(config_path, 128); - +IZ_ProcedureResult IZ_SaveVideoConfig(const char* config_path, IZ_VideoConfig* config) { if (!ini_putl("Video", "Width", config->width, config_path)) { return 1; } @@ -17,10 +14,7 @@ IZ_ProcedureResult IZ_SaveVideoConfig(IZ_VideoConfig* config) { return 0; } -void IZ_LoadVideoConfig(IZ_VideoConfig* config) { - char config_path[128]; - IZ_GetConfigPath(config_path, 128); - +void IZ_LoadVideoConfig(const char* config_path, IZ_VideoConfig* config) { config->width = ini_getl("Video", "Width", 640l, config_path); config->height = ini_getl("Video", "Height", 480l, config_path); config->max_fps = ini_getl("Video", "MaxFps", 30, config_path); diff --git a/src/packages/game/output/IZ_video.h b/src/packages/game/output/IZ_video.h index b2fb3fb..e9f56e7 100644 --- a/src/packages/game/output/IZ_video.h +++ b/src/packages/game/output/IZ_video.h @@ -12,8 +12,8 @@ typedef struct { uint8_t max_fps; } IZ_VideoConfig; -IZ_ProcedureResult IZ_SaveVideoConfig(IZ_VideoConfig* config); +IZ_ProcedureResult IZ_SaveVideoConfig(const char*, IZ_VideoConfig* config); -void IZ_LoadVideoConfig(IZ_VideoConfig* config); +void IZ_LoadVideoConfig(const char*, IZ_VideoConfig* config); #endif