Bläddra i källkod

Optimize config loading

Determine config path only once.
feature/data-structs
TheoryOfNekomata 2 år sedan
förälder
incheckning
3c3c971726
7 ändrade filer med 61 tillägg och 51 borttagningar
  1. +9
    -6
      src/packages/game/IZ_app.c
  2. +27
    -15
      src/packages/game/input/IZ_joystick.c
  3. +2
    -3
      src/packages/game/input/IZ_joystick.h
  4. +17
    -14
      src/packages/game/input/IZ_keyboard.c
  5. +2
    -3
      src/packages/game/input/IZ_keyboard.h
  6. +2
    -8
      src/packages/game/output/IZ_video.c
  7. +2
    -2
      src/packages/game/output/IZ_video.h

+ 9
- 6
src/packages/game/IZ_app.c Visa fil

@@ -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;
}


+ 27
- 15
src/packages/game/input/IZ_joystick.c Visa fil

@@ -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;
}

+ 2
- 3
src/packages/game/input/IZ_joystick.h Visa fil

@@ -5,7 +5,6 @@
#include <SDL_events.h>
#include <minIni.h>
#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*);



+ 17
- 14
src/packages/game/input/IZ_keyboard.c Visa fil

@@ -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


+ 2
- 3
src/packages/game/input/IZ_keyboard.h Visa fil

@@ -5,7 +5,6 @@
#include <SDL_events.h>
#include <minIni.h>
#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*);



+ 2
- 8
src/packages/game/output/IZ_video.c Visa fil

@@ -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);


+ 2
- 2
src/packages/game/output/IZ_video.h Visa fil

@@ -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

Laddar…
Avbryt
Spara