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.

73 lines
2.5 KiB

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