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.

50 lines
1.3 KiB

  1. #include "IZ_keyboard.h"
  2. void IZ_HandleKeyboardEvents(SDL_Event e, IZ_KeyboardState* state, IZ_Action* action) {
  3. for (uint8_t i = 0; i < CONTROLS; i += 1) {
  4. if (e.key.keysym.sym == state->config.control_mapping[i]) {
  5. const uint16_t bitflag = (0x1 << i);
  6. if (e.type == SDL_KEYDOWN) {
  7. *action |= bitflag;
  8. break;
  9. }
  10. if (e.type == SDL_KEYUP) {
  11. *action &= ~bitflag;
  12. break;
  13. }
  14. }
  15. }
  16. }
  17. void IZ_SaveKeyboardConfig(IZ_KeyboardConfig* config, uint8_t p) {
  18. char config_path[128];
  19. IZ_GetConfigPath(config_path, 128);
  20. char keyboard_section_name[] = "Keyboard.0.ControlMapping";
  21. keyboard_section_name[9] = (char) (48 + p);
  22. for (uint8_t i = 0; i < CONTROLS; i += 1) {
  23. ini_puts(keyboard_section_name, ACTION_NAMES[i], SDL_GetKeyName(config->control_mapping[i]), config_path);
  24. }
  25. }
  26. void IZ_LoadKeyboardConfig(IZ_KeyboardConfig* config, uint8_t p) {
  27. char config_path[128];
  28. IZ_GetConfigPath(config_path, 128);
  29. char buffer[128];
  30. char keyboard_section_name[] = "Keyboard.0.ControlMapping";
  31. keyboard_section_name[9] = (char) (48 + p);
  32. for (uint8_t i = 0; i < CONTROLS; i += 1) {
  33. ini_gets(
  34. keyboard_section_name,
  35. ACTION_NAMES[i],
  36. SDL_GetKeyName(IZ_DEFAULT_KEYBOARD_CONTROLS[p][i]),
  37. buffer,
  38. 128,
  39. config_path
  40. );
  41. config->control_mapping[i] = SDL_GetKeyFromName(buffer);
  42. }
  43. }