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.
 
 
 
 
 
 

101 lines
3.5 KiB

  1. #include "IZ_keyboard.h"
  2. static INI_ConfigItem keyboard_config_items[IZ_PLAYERS * IZ_CONTROLS + 1];
  3. void IZ_KeyboardSerializeControl(i32 value, char control[128], void* _unused) {
  4. const char* serialized = SDL_GetKeyName(value);
  5. IZ_memcpy(control, 128, serialized, 128);
  6. }
  7. i32 IZ_KeyboardDeserializeControl(const char* control, void* _unused) {
  8. return SDL_GetKeyFromName(control);
  9. }
  10. void IZ_KeyboardHandleKeyUpDownEvents(IZ_KeyboardState* state, IZ_Action* action, SDL_Event e) {
  11. u8 control_index;
  12. for (control_index = 0; control_index < IZ_CONTROLS; control_index += 1) {
  13. if (e.key.keysym.sym == state->config.control_mapping[control_index]) {
  14. const u16 bitflag = (0x1 << control_index);
  15. if (e.type == SDL_KEYDOWN) {
  16. *action |= bitflag;
  17. return;
  18. }
  19. if (e.type == SDL_KEYUP) {
  20. *action &= ~bitflag;
  21. return;
  22. }
  23. }
  24. }
  25. }
  26. void IZ_KeyboardHandleEvents(IZ_KeyboardState(* state)[IZ_PLAYERS], IZ_Action(* action)[IZ_PLAYERS], SDL_Event e) {
  27. for (u8 player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  28. IZ_KeyboardHandleKeyUpDownEvents(&(*state)[player_index], &(*action)[player_index], e);
  29. }
  30. }
  31. void IZ_KeyboardBindStateToConfig(IZ_KeyboardState(* state)[IZ_PLAYERS], INI_ConfigItem config_items[]) {
  32. u8 player_index;
  33. u8 control_index;
  34. for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  35. for (control_index = 0; control_index < IZ_CONTROLS; control_index += 1) {
  36. config_items[player_index * IZ_CONTROLS + control_index].dest = &((*state)[player_index].config.control_mapping[control_index]);
  37. }
  38. }
  39. }
  40. IZ_ProcedureResult IZ_KeyboardSaveConfig(IZ_KeyboardState(* state)[IZ_PLAYERS], const char* config_path) {
  41. IZ_KeyboardBindStateToConfig(state, keyboard_config_items);
  42. return INI_ConfigSave(keyboard_config_items, config_path);
  43. }
  44. void IZ_KeyboardInitializeConfigItems(INI_ConfigItem config_items[]) {
  45. u8 player_index;
  46. u8 control_index;
  47. char* control_mapping_section_name;
  48. for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  49. control_mapping_section_name = IZ_calloc(64, sizeof(char));
  50. sprintf(control_mapping_section_name, "Keyboard.%d.ControlMapping", player_index);
  51. for (control_index = 0; control_index < IZ_CONTROLS; control_index += 1) {
  52. config_items[player_index * IZ_CONTROLS + control_index] = (INI_ConfigItem) {
  53. INI_CONFIG_TYPE_I32,
  54. control_mapping_section_name,
  55. IZ_ACTION_NAMES[control_index],
  56. NULL,
  57. &IZ_KEYBOARD_DEFAULT_STATE[player_index].config.control_mapping[control_index],
  58. NULL,
  59. {
  60. .serialize = IZ_KeyboardSerializeControl,
  61. .deserialize = IZ_KeyboardDeserializeControl,
  62. },
  63. NULL,
  64. };
  65. }
  66. }
  67. config_items[IZ_PLAYERS * IZ_CONTROLS] = INI_CONFIG_ITEM_NULL;
  68. }
  69. IZ_ProcedureResult IZ_KeyboardInitializeConfig(IZ_KeyboardState(* state)[IZ_PLAYERS], const char* config_path, u8 argc, const char* argv[]) {
  70. IZ_KeyboardInitializeConfigItems(keyboard_config_items);
  71. IZ_KeyboardBindStateToConfig(state, keyboard_config_items);
  72. if (INI_ConfigInitialize(keyboard_config_items, config_path, argc, argv) < 0) {
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. IZ_ProcedureResult IZ_KeyboardInitialize(IZ_KeyboardState(* state)[IZ_PLAYERS], const char* config_path, u8 argc, const char* argv[]) {
  78. IZ_memcpy(state, sizeof(IZ_KeyboardState) * IZ_PLAYERS, &IZ_KEYBOARD_DEFAULT_STATE, sizeof(IZ_KeyboardState) * IZ_PLAYERS);
  79. if (IZ_KeyboardInitializeConfig(state, config_path, argc, argv) < 0) {
  80. return -2;
  81. }
  82. INI_ConfigSaveResult post_config_save_result = IZ_KeyboardSaveConfig(state, config_path);
  83. if (post_config_save_result < 0) {
  84. return -3;
  85. }
  86. return 0;
  87. }