input_manager.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "input_manager.h"
  6. #include "input_device.h"
  7. #include "memory.h"
  8. #include "vector3.h"
  9. #include <string.h> // strlen, strcpy, memset
  10. namespace crown
  11. {
  12. InputManager::InputManager()
  13. : _keyboard(NULL)
  14. , _mouse(NULL)
  15. , _touch(NULL)
  16. {
  17. _keyboard = create_input_device("Keyboard", KeyboardButton::COUNT, 0);
  18. _mouse = create_input_device("Mouse", MouseButton::COUNT, 2);
  19. _touch = create_input_device("Touch", TouchButton::COUNT, TouchButton::COUNT);
  20. for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
  21. _joypad[i] = create_input_device("Joypad", JoypadButton::COUNT, JoypadAxis::COUNT);
  22. _keyboard->set_connected(true);
  23. _mouse->set_connected(true);
  24. _touch->set_connected(true);
  25. }
  26. InputManager::~InputManager()
  27. {
  28. for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
  29. default_allocator().deallocate(_joypad[i]);
  30. default_allocator().deallocate(_touch);
  31. default_allocator().deallocate(_mouse);
  32. default_allocator().deallocate(_keyboard);
  33. }
  34. InputDevice* InputManager::create_input_device(const char* name, uint8_t num_buttons, uint8_t num_axes)
  35. {
  36. const uint32_t size = 0
  37. + sizeof(InputDevice)
  38. + sizeof(uint8_t)*num_buttons*2
  39. + sizeof(Vector3)*num_axes
  40. + strlen(name) + 1;
  41. InputDevice* id = (InputDevice*)default_allocator().allocate(size);
  42. id->_connected = false;
  43. id->_num_buttons = num_buttons;
  44. id->_num_axes = num_axes;
  45. id->_last_button = 0;
  46. id->_last_state = (uint8_t*)&id[1];
  47. id->_current_state = (uint8_t*)(id->_last_state + num_buttons);
  48. id->_axis = (Vector3*)(id->_current_state + num_buttons);
  49. id->_name = (char*)(id->_axis + num_axes);
  50. memset(id->_last_state, 0, sizeof(uint8_t)*num_buttons);
  51. memset(id->_current_state, 0, sizeof(uint8_t)*num_buttons);
  52. memset(id->_axis, 0, sizeof(Vector3)*num_axes);
  53. strcpy(id->_name, name);
  54. return id;
  55. }
  56. InputDevice* InputManager::keyboard()
  57. {
  58. return _keyboard;
  59. }
  60. InputDevice* InputManager::mouse()
  61. {
  62. return _mouse;
  63. }
  64. InputDevice* InputManager::touch()
  65. {
  66. return _touch;
  67. }
  68. InputDevice* InputManager::joypad(uint8_t i)
  69. {
  70. CE_ASSERT(i < CROWN_MAX_JOYPADS, "Index out of bounds");
  71. return _joypad[i];
  72. }
  73. void InputManager::update()
  74. {
  75. _keyboard->update();
  76. _mouse->update();
  77. _touch->update();
  78. for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
  79. _joypad[i]->update();
  80. }
  81. } // namespace crown