Starter project for SDL2.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.3 KiB

  1. #include "IZ_config.h"
  2. void IZ_GetConfigPath(char* config_path) {
  3. //const char* config_path_dir = SDL_GetPrefPath("Modal Studios", APP_NAME);
  4. const char* config_path_dir = SDL_GetBasePath();
  5. memcpy_s(config_path, 128, config_path_dir, 128);
  6. strcat_s(config_path, 128, "config.ini");
  7. }
  8. void IZ_SaveConfig(IZ_Config* config) {
  9. char config_path[128];
  10. IZ_GetConfigPath(config_path);
  11. FILE* fp;
  12. fopen_s(&fp, config_path, "w");
  13. fprintf_s(fp, "[Video]\n");
  14. fprintf_s(fp, "Width=%u\n", config->video.width);
  15. fprintf_s(fp, "Height=%u\n", config->video.height);
  16. fprintf_s(fp, "MaxFps=%u\n", config->video.max_fps);
  17. fprintf_s(fp, "\n");
  18. fprintf_s(fp, "[Input]\n");
  19. fprintf_s(fp, "GamepadAxisThreshold=%u\n", config->input.gamepad_axis_threshold);
  20. fprintf_s(fp, "\n");
  21. for (uint8_t p = 0; p < PLAYERS; p += 1) {
  22. fprintf_s(fp, "[Controls.%u.Keyboard]\n", p);
  23. for (uint8_t i = 0; i < CONTROLS; i += 1) {
  24. fprintf_s(fp, "%s=%s\n", ACTION_NAMES[i], SDL_GetKeyName(config->controls[p].keyboard[i]));
  25. }
  26. fprintf_s(fp, "\n");
  27. fprintf_s(fp, "[Controls.%u.Joystick]\n", p);
  28. for (uint8_t i = 4; i < CONTROLS; i += 1) {
  29. fprintf_s(fp, "%s=%d\n", ACTION_NAMES[i], config->controls[p].gamepad[i]);
  30. }
  31. }
  32. }
  33. void IZ_LoadConfig(IZ_Config* config) {
  34. char config_path[128];
  35. IZ_GetConfigPath(config_path);
  36. config->video.width = ini_getl("Video", "Width", 640l, config_path);
  37. config->video.height = ini_getl("Video", "Height", 480l, config_path);
  38. config->video.max_fps = ini_getl("Video", "MaxFps", 30, config_path);
  39. config->input.gamepad_axis_threshold = ini_getl("Input", "GamepadAxisThreshold", 8000, config_path);
  40. char buffer[128];
  41. char keyboard_section_name[20] = "Controls.0.Keyboard";
  42. for (uint8_t p = 0; p < PLAYERS; p += 1) {
  43. keyboard_section_name[9] = (char) (48 + p);
  44. for (uint8_t i = 0; i < CONTROLS; i += 1) {
  45. ini_gets(keyboard_section_name, ACTION_NAMES[i], SDL_GetKeyName(IZ_DEFAULT_KEYBOARD_CONTROLS[p][i]), buffer, 128,
  46. config_path);
  47. config->controls[p].keyboard[i] = SDL_GetKeyFromName(buffer);
  48. }
  49. }
  50. char joystick_section_name[20] = "Controls.0.Joystick";
  51. for (uint8_t p = 0; p < PLAYERS; p += 1) {
  52. joystick_section_name[9] = (char) (48 + p);
  53. for (uint8_t i = 4; i < CONTROLS; i += 1) {
  54. config->controls[p].gamepad[i] = ini_getl(joystick_section_name, ACTION_NAMES[i], IZ_DEFAULT_JOYSTICK_CONTROLS[p][i], config_path);
  55. }
  56. }
  57. }