2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
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.
 
 
 
 
 
 

273 lines
9.4 KiB

  1. #include "IZ_gamecontroller.h"
  2. static INI_ConfigItem game_controller_config_items[IZ_PLAYERS * (IZ_CONTROLS + IZ_GAME_CONTROLLER_DEFAULT_CONFIGS) + 1];
  3. bool IZ_GameControllerIsValidAxisThreshold(u16 value) {
  4. return (IZ_GAME_CONTROLLER_MIN_AXIS_THRESHOLD <= value && value <= IZ_GAME_CONTROLLER_MAX_AXIS_THRESHOLD);
  5. }
  6. void IZ_GameControllerHandleDeviceEvents(IZ_GameControllerState* state, SDL_Event e) {
  7. if (e.type == SDL_CONTROLLERDEVICEADDED) {
  8. u8 game_controllers_count = SDL_NumJoysticks();
  9. if (game_controllers_count <= IZ_PLAYERS && !state->device) {
  10. state->device = SDL_GameControllerOpen(e.cdevice.which);
  11. }
  12. return;
  13. }
  14. if (e.type == SDL_CONTROLLERDEVICEREMOVED) {
  15. if (
  16. state->device
  17. && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(state->device)) == e.cdevice.which
  18. ) {
  19. state->device = NULL;
  20. }
  21. }
  22. }
  23. void IZ_GameControllerHandleAxisEvents(IZ_GameControllerState* state, IZ_Action* action, SDL_Event e) {
  24. if (e.type != SDL_CONTROLLERAXISMOTION) {
  25. return;
  26. }
  27. u8 control_index;
  28. for (control_index = 0; control_index < IZ_CONTROLS; control_index += 1) {
  29. const char* current_axis_name = SDL_GameControllerGetStringForAxis(e.caxis.axis);
  30. if (!current_axis_name) {
  31. continue;
  32. }
  33. char current_positive_axis_full_name[IZ_GAME_CONTROLLER_MAX_CONTROL_NAME_LENGTH];
  34. sprintf(
  35. current_positive_axis_full_name,
  36. "axis:+%s",
  37. current_axis_name
  38. );
  39. char current_negative_axis_full_name[IZ_GAME_CONTROLLER_MAX_CONTROL_NAME_LENGTH];
  40. sprintf(
  41. current_negative_axis_full_name,
  42. "axis:-%s",
  43. current_axis_name
  44. );
  45. bool is_capture = false;
  46. const u16 bitflag = (0x1 << control_index);
  47. if (
  48. strstr(state->config.control_mapping[control_index], current_positive_axis_full_name)
  49. || strstr(state->config.control_mapping[control_index], current_negative_axis_full_name)
  50. ) {
  51. *action &= ~bitflag;
  52. // should we implement actions that do not cancel out across input controllers?
  53. // add extra byte for source of action:
  54. // 0x1 - keyboard
  55. // 0x2 - game controller dpad
  56. // 0x4 - game controller left stick
  57. // 0x8 - game controller right stick
  58. // 0x10 - other device
  59. }
  60. char current_axis_full_name[IZ_GAME_CONTROLLER_MAX_CONTROL_NAME_LENGTH];
  61. if (e.caxis.value > 0) {
  62. IZ_memcpy(
  63. current_axis_full_name,
  64. IZ_GAME_CONTROLLER_MAX_CONTROL_NAME_LENGTH,
  65. current_positive_axis_full_name,
  66. IZ_GAME_CONTROLLER_MAX_CONTROL_NAME_LENGTH
  67. );
  68. is_capture = e.caxis.value > state->config.axis_threshold;
  69. }
  70. if (e.caxis.value < 0) {
  71. IZ_memcpy(
  72. current_axis_full_name,
  73. IZ_GAME_CONTROLLER_MAX_CONTROL_NAME_LENGTH,
  74. current_negative_axis_full_name,
  75. IZ_GAME_CONTROLLER_MAX_CONTROL_NAME_LENGTH
  76. );
  77. is_capture = e.jaxis.value <= -state->config.axis_threshold;
  78. }
  79. if (!strstr(state->config.control_mapping[control_index], current_axis_full_name)) {
  80. continue;
  81. }
  82. if (!is_capture) {
  83. continue;
  84. }
  85. *action |= bitflag;
  86. }
  87. }
  88. void IZ_GameControllerHandleButtonEvents(IZ_GameControllerState* state, IZ_Action* action, SDL_Event e) {
  89. u8 control_index;
  90. for (control_index = 0; control_index < IZ_CONTROLS; control_index += 1) {
  91. const char* current_button_name = SDL_GameControllerGetStringForButton(e.cbutton.button);
  92. if (!current_button_name) {
  93. continue;
  94. }
  95. char current_button_full_name[IZ_GAME_CONTROLLER_MAX_CONTROL_NAME_LENGTH];
  96. sprintf(
  97. current_button_full_name,
  98. "button:%s",
  99. current_button_name
  100. );
  101. if (!strstr(state->config.control_mapping[control_index], current_button_full_name)) {
  102. continue;
  103. }
  104. const u16 bitflag = (0x1 << control_index);
  105. if (e.type == SDL_CONTROLLERBUTTONDOWN) {
  106. *action |= bitflag;
  107. return;
  108. }
  109. if (e.type == SDL_CONTROLLERBUTTONUP) {
  110. *action &= ~bitflag;
  111. return;
  112. }
  113. }
  114. }
  115. void IZ_GameControllerHandleEvents(IZ_GameControllerState(* state)[IZ_PLAYERS], IZ_Action(* action)[IZ_PLAYERS], SDL_Event e) {
  116. u8 player_index;
  117. for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  118. IZ_GameControllerHandleDeviceEvents(&(*state)[player_index], e);
  119. IZ_GameControllerHandleAxisEvents(&(*state)[player_index], &(*action)[player_index], e);
  120. IZ_GameControllerHandleButtonEvents(&(*state)[player_index], &(*action)[player_index], e);
  121. }
  122. }
  123. void IZ_GameControllerBindStateToConfig(IZ_GameControllerState(* state)[IZ_PLAYERS], INI_ConfigItem config_items[]) {
  124. u8 player_index;
  125. u8 control_index;
  126. for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  127. // The `+ 3` corresponds to the first three config items. Add as appropriate.
  128. u8 base_index = (player_index * (IZ_CONTROLS + IZ_GAME_CONTROLLER_DEFAULT_CONFIGS));
  129. config_items[base_index + 0].dest = &((*state)[player_index].config.axis_threshold);
  130. config_items[base_index + 1].dest = &((*state)[player_index].config.guid);
  131. for (control_index = 0; control_index < IZ_CONTROLS; control_index += 1) {
  132. config_items[base_index + IZ_GAME_CONTROLLER_DEFAULT_CONFIGS + control_index].dest = &((*state)[player_index].config.control_mapping[control_index]);
  133. }
  134. }
  135. }
  136. IZ_ProcedureResult IZ_GameControllerSaveConfig(IZ_GameControllerState(* state)[IZ_PLAYERS], const char* config_path) {
  137. IZ_GameControllerBindStateToConfig(state, game_controller_config_items);
  138. return INI_ConfigSave(game_controller_config_items, config_path);
  139. }
  140. void IZ_GameControllerInitializeConfigItems(INI_ConfigItem config_items[]) {
  141. u8 player_index;
  142. u8 control_index;
  143. char* main_section_name;
  144. char* control_mapping_section_name;
  145. for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  146. main_section_name = IZ_calloc(64, sizeof(char));
  147. sprintf(main_section_name, "GameController.%d", player_index);
  148. // The `+ 3` corresponds to the first three config items. Add as appropriate.
  149. u8 base_index = (player_index * (IZ_CONTROLS + IZ_GAME_CONTROLLER_DEFAULT_CONFIGS));
  150. config_items[base_index + 0] = (INI_ConfigItem) {
  151. INI_CONFIG_TYPE_U16,
  152. main_section_name,
  153. "AxisThreshold",
  154. NULL,
  155. &IZ_GAME_CONTROLLER_DEFAULT_STATE[player_index].config.axis_threshold,
  156. IZ_GameControllerIsValidAxisThreshold,
  157. INI_CONFIG_TRANSFORMER_NONE,
  158. NULL,
  159. };
  160. config_items[base_index + 1] = (INI_ConfigItem) {
  161. INI_CONFIG_TYPE_GUID,
  162. main_section_name,
  163. "GUID",
  164. NULL,
  165. &IZ_GAME_CONTROLLER_DEFAULT_STATE[player_index].config.guid,
  166. NULL,
  167. INI_CONFIG_TRANSFORMER_NONE,
  168. NULL,
  169. };
  170. control_mapping_section_name = IZ_calloc(64, sizeof(char));
  171. sprintf(control_mapping_section_name, "GameController.%d.ControlMapping", player_index);
  172. for (control_index = 0; control_index < IZ_CONTROLS; control_index += 1) {
  173. config_items[base_index + IZ_GAME_CONTROLLER_DEFAULT_CONFIGS + control_index] = (INI_ConfigItem) {
  174. INI_CONFIG_TYPE_STRING(IZ_GAME_CONTROLLER_MAX_CONTROL_NAME_LENGTH),
  175. control_mapping_section_name,
  176. IZ_ACTION_NAMES[control_index],
  177. NULL,
  178. &IZ_GAME_CONTROLLER_DEFAULT_STATE[player_index].config.control_mapping[control_index],
  179. NULL,
  180. INI_CONFIG_TRANSFORMER_NONE,
  181. NULL,
  182. };
  183. }
  184. }
  185. config_items[IZ_PLAYERS * (IZ_CONTROLS + IZ_GAME_CONTROLLER_DEFAULT_CONFIGS)] = INI_CONFIG_ITEM_NULL;
  186. }
  187. IZ_ProcedureResult IZ_GameControllerInitializeConfig(IZ_GameControllerState(* state)[IZ_PLAYERS], const char* config_path, u8 argc, const char* argv[]) {
  188. IZ_GameControllerInitializeConfigItems(game_controller_config_items);
  189. IZ_GameControllerBindStateToConfig(state, game_controller_config_items);
  190. if (INI_ConfigInitialize(game_controller_config_items, config_path, argc, argv) < 0) {
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. IZ_ProcedureResult IZ_GameControllerInitialize(IZ_GameControllerState(* state)[IZ_PLAYERS], const char* config_path, u8 argc, const char* argv[]) {
  196. IZ_memcpy(state, sizeof(IZ_GameControllerState) * IZ_PLAYERS, &IZ_GAME_CONTROLLER_DEFAULT_STATE, sizeof(IZ_GameControllerState) * IZ_PLAYERS);
  197. // TODO query this file from the internet?
  198. IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "input", "Loading game controller mappings file: %s", "assets/gamecontrollerdb.txt");
  199. const i16 loaded_mappings = SDL_GameControllerAddMappingsFromFile("assets/gamecontrollerdb.txt");
  200. if (loaded_mappings <= 0) {
  201. return -1;
  202. }
  203. IZ_LogInfo(IZ_LOG_CATEGORY_GENERIC, "input", "Mappings loaded: %d", loaded_mappings);
  204. u8 player_index;
  205. if (IZ_GameControllerInitializeConfig(state, config_path, argc, argv) < 0) {
  206. return -2;
  207. }
  208. u8 game_controllers_count = SDL_NumJoysticks();
  209. for (player_index = 0; player_index < game_controllers_count; player_index += 1) {
  210. if (player_index >= IZ_PLAYERS) {
  211. break;
  212. }
  213. (*state)[player_index].device = SDL_GameControllerOpen(state[player_index]->config.device_id);
  214. if (!(*state)[player_index].device) {
  215. break;
  216. }
  217. (*state)[player_index].config.device_id = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick((*state)[player_index].device));
  218. SDL_GUID game_controller_guid = SDL_JoystickGetGUID(SDL_GameControllerGetJoystick((*state)[player_index].device));
  219. IZ_memcpy(&(*state)[player_index].config.guid, sizeof(SDL_GUID), &game_controller_guid, sizeof(SDL_GUID));
  220. }
  221. // Post config (after game_controller GUIDs have been queried), this is unique to game_controllers since they can be plugged in any
  222. // time.
  223. INI_ConfigSaveResult post_config_save_result = IZ_GameControllerSaveConfig(state, config_path);
  224. if (post_config_save_result < 0) {
  225. return -3;
  226. }
  227. return 0;
  228. }
  229. void IZ_GameControllerTeardown(IZ_GameControllerState(* state)[IZ_PLAYERS]) {
  230. u8 player_index;
  231. for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  232. if (!(*state)[player_index].device) {
  233. continue;
  234. }
  235. SDL_GameControllerClose((*state)[player_index].device);
  236. }
  237. }