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.
 
 
 
 
 
 

77 lines
2.5 KiB

  1. #include "IZ_keyboard.h"
  2. void IZ_KeyboardHandleKeyUpDownEvents(IZ_KeyboardState* state, IZ_Action* action, SDL_Event e) {
  3. u8 control_index;
  4. for (control_index = 0; control_index < CONTROLS; control_index += 1) {
  5. if (e.key.keysym.sym == state->config.control_mapping[control_index]) {
  6. const u16 bitflag = (0x1 << control_index);
  7. if (e.type == SDL_KEYDOWN) {
  8. *action |= bitflag;
  9. return;
  10. }
  11. if (e.type == SDL_KEYUP) {
  12. *action &= ~bitflag;
  13. return;
  14. }
  15. }
  16. }
  17. }
  18. void IZ_KeyboardHandleEvents(IZ_KeyboardState(* state)[IZ_PLAYERS], IZ_Action(* action)[IZ_PLAYERS], SDL_Event e) {
  19. for (u8 player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  20. IZ_KeyboardHandleKeyUpDownEvents(&(*state)[player_index], &(*action)[player_index], e);
  21. }
  22. }
  23. IZ_ProcedureResult IZ_KeyboardSaveConfig(IZ_KeyboardState(* state)[IZ_PLAYERS], const char* config_path) {
  24. u8 problem = 0;
  25. char control_mapping_section_name[26];
  26. u8 player_index;
  27. u8 control_index;
  28. for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  29. sprintf_s(control_mapping_section_name, 26, "Keyboard.%d.ControlMapping", player_index);
  30. for (control_index = 0; control_index < CONTROLS; control_index += 1) {
  31. if (!ini_puts(
  32. control_mapping_section_name,
  33. ACTION_NAMES[control_index],
  34. SDL_GetKeyName((*state)[player_index].config.control_mapping[control_index]),
  35. config_path
  36. )) {
  37. problem |= (1 << player_index);
  38. }
  39. }
  40. }
  41. return problem;
  42. }
  43. void IZ_KeyboardLoadConfig(IZ_KeyboardState(* state)[IZ_PLAYERS], const char* config_path) {
  44. char buffer[128];
  45. char keyboard_section_name[26];
  46. u8 player_index;
  47. u8 control_index;
  48. for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) {
  49. sprintf_s(keyboard_section_name, 26, "Keyboard.%d.ControlMapping", player_index);
  50. for (control_index = 0; control_index < CONTROLS; control_index += 1) {
  51. ini_gets(
  52. keyboard_section_name,
  53. ACTION_NAMES[control_index],
  54. SDL_GetKeyName(IZ_KEYBOARD_DEFAULT_STATE[player_index].config.control_mapping[control_index]),
  55. buffer,
  56. 128,
  57. config_path
  58. );
  59. (*state)[player_index].config.control_mapping[control_index] = SDL_GetKeyFromName(buffer);
  60. }
  61. }
  62. }
  63. IZ_ProcedureResult IZ_KeyboardInitialize(IZ_KeyboardState(* state)[IZ_PLAYERS], const char* config_path, u8 argc, const char* argv[]) {
  64. SDL_memcpy(state, &IZ_KEYBOARD_DEFAULT_STATE, sizeof(IZ_KeyboardState));
  65. IZ_KeyboardLoadConfig(state, config_path);
  66. return IZ_KeyboardSaveConfig(state, config_path);
  67. }