Re-implementation of Izanami game engine
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.

153 lines
4.7 KiB

  1. #include "IZ_config.h"
  2. #ifdef _WIN64
  3. void realpath(const char* filename, char* resolved_path) {
  4. TCHAR** lppPart = { NULL };
  5. GetFullPathNameA(filename, 256, resolved_path, lppPart);
  6. }
  7. #endif
  8. /**
  9. * Writes the configuration to a file.
  10. * @param config The pointer to the configuration data.
  11. * @param filename The filename of the destination file.
  12. * @return Error message, or NULL if there are no errors.
  13. */
  14. unsigned int IZ_ConfigSave(IZ_Config* config, const char* filename) {
  15. char resolved_path[256];
  16. realpath(filename, resolved_path);
  17. IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "Persisting configuration to: %s", resolved_path);
  18. FILE* output = fopen(filename, "w");
  19. if (output == NULL) {
  20. IZ_LogError("Could not persist config.");
  21. return 1;
  22. }
  23. fprintf(output, "[Window]\n");
  24. fprintf(output, "Width = %d\n", config->window.width);
  25. fprintf(output, "Height = %d\n", config->window.height);
  26. fprintf(output, "\n[Pool]\n");
  27. fprintf(output, "Characters = %d\n", config->pool.characters);
  28. unsigned int p;
  29. unsigned int i;
  30. for (p = 0; p < IZ_MAX_PLAYERS; p += 1) {
  31. fprintf(output, "\n[Mapping(%d).Keyboard]\n", p);
  32. for (i = 0; i < 16; i += 1) {
  33. fprintf(output, "%s = %d\n", IZ_ACTION_NAMES[i], config->mapping[p].keyboard[i]);
  34. }
  35. fprintf(output, "\n[Mapping(%d).Joystick]\n", p);
  36. for (i = 0; i < 12; i += 1) {
  37. fprintf(output, "%s = %d\n", IZ_ACTION_NAMES[i + 4], config->mapping[p].joystick[i]);
  38. }
  39. }
  40. fclose(output);
  41. IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "Config saved successfully.");
  42. return 0;
  43. }
  44. /**
  45. * Reads the configuration from a file.
  46. * @param config The pointer to the configuration data.
  47. * @param filename The filename of the destination file.
  48. * @return Error message, or NULL if there are no errors.
  49. */
  50. unsigned int IZ_ConfigLoad(IZ_Config* config, const char* filename) {
  51. char resolved_path[256];
  52. realpath(filename, resolved_path);
  53. IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "Loading configuration file: %s", resolved_path);
  54. FILE* input = fopen(filename, "r");
  55. unsigned int p;
  56. unsigned int i;
  57. if (input == NULL) {
  58. IZ_LogWarn(false, "Config file not found! Loading defaults.");
  59. *config = (IZ_Config) {
  60. .window = { .width = 320, .height = 240 },
  61. .pool = {
  62. .characters = 64
  63. },
  64. .mapping = {
  65. // player 1
  66. {
  67. .keyboard = {
  68. IZ_SCANCODE_D,
  69. IZ_SCANCODE_S,
  70. IZ_SCANCODE_A,
  71. IZ_SCANCODE_W,
  72. IZ_SCANCODE_RETURN,
  73. IZ_SCANCODE_BACKSPACE,
  74. IZ_SCANCODE_M,
  75. IZ_SCANCODE_COMMA,
  76. IZ_SCANCODE_PERIOD,
  77. IZ_SCANCODE_SLASH,
  78. IZ_SCANCODE_J,
  79. IZ_SCANCODE_K,
  80. IZ_SCANCODE_L,
  81. IZ_SCANCODE_SEMICOLON,
  82. IZ_SCANCODE_I,
  83. IZ_SCANCODE_O,
  84. },
  85. .joystick = {
  86. IZ_JOYSTICK_CODE_0,
  87. IZ_JOYSTICK_CODE_1,
  88. IZ_JOYSTICK_CODE_2,
  89. IZ_JOYSTICK_CODE_3,
  90. IZ_JOYSTICK_CODE_4,
  91. IZ_JOYSTICK_CODE_5,
  92. IZ_JOYSTICK_CODE_6,
  93. IZ_JOYSTICK_CODE_7,
  94. IZ_JOYSTICK_CODE_8,
  95. IZ_JOYSTICK_CODE_9,
  96. IZ_JOYSTICK_CODE_10,
  97. IZ_JOYSTICK_CODE_11,
  98. }
  99. }
  100. }
  101. };
  102. unsigned int persist_status = IZ_ConfigSave(config, filename);
  103. if (persist_status != 0) {
  104. return persist_status;
  105. }
  106. IZ_Log("Window.Width=%d", config->window.width);
  107. IZ_Log("Window.Height=%d", config->window.height);
  108. for (p = 0; p < IZ_MAX_PLAYERS; p += 1) {
  109. for (i = 0; i < 16; i += 1) {
  110. IZ_Log("Mapping(%d).Keyboard.%s=%02x", p, IZ_ACTION_NAMES[i], config->mapping[p].keyboard[i]);
  111. }
  112. for (i = 0; i < 12; i += 1) {
  113. IZ_Log("Mapping(%d).Joystick.%s=%02x", p, IZ_ACTION_NAMES[i + 4], config->mapping[p].joystick[i]);
  114. }
  115. }
  116. } else {
  117. fscanf(input, "[Window]\n");
  118. fscanf(input, "Width = %d\n", &config->window.width);
  119. IZ_Log("Window.Width=%d", config->window.width);
  120. fscanf(input, "Height = %d\n", &config->window.height);
  121. IZ_Log("Window.Height=%d", config->window.height);
  122. fscanf(input, "\n[Pool]\n");
  123. fscanf(input, "Characters = %d\n", &config->pool.characters);
  124. IZ_Log("Pool.Characters=%d", config->pool.characters);
  125. static char action_name[7];
  126. int player_index;
  127. for (p = 0; p < IZ_MAX_PLAYERS; p += 1) {
  128. fscanf(input, "\n[Mapping(%d).Keyboard]\n", &player_index);
  129. for (i = 0; i < 16; i += 1) {
  130. fscanf(input, "%s = %d\n", action_name, &config->mapping[player_index].keyboard[i]);
  131. IZ_Log("Mapping(%d).Keyboard.%s=%02x", player_index, action_name, config->mapping[player_index].keyboard[i]);
  132. }
  133. fscanf(input, "\n[Mapping(%d).Joystick]\n", &player_index);
  134. for (i = 0; i < 12; i += 1) {
  135. fscanf(input, "%s = %d\n", action_name, &config->mapping[player_index].joystick[i]);
  136. IZ_Log("Mapping(%d).Joystick.%s=%02x", player_index, action_name, config->mapping[player_index].joystick[i]);
  137. }
  138. }
  139. }
  140. fclose(input);
  141. IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC,"%s", "Config loaded successfully.");
  142. return 0;
  143. }