No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

130 líneas
3.6 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #if defined LOL_USE_XINPUT
  16. # include <d3d9.h>
  17. # include <xinput.h>
  18. #endif
  19. #include <memory>
  20. #include <lol/engine-internal.h>
  21. #include "ui/d3d9-input.h"
  22. #include "ui/input.h"
  23. namespace lol
  24. {
  25. /*
  26. * D3d9 Input implementation class
  27. */
  28. class D3d9InputData
  29. {
  30. friend class D3d9Input;
  31. private:
  32. #if defined LOL_USE_XINPUT
  33. array<int, std::shared_ptr<input::device::joystick>> m_joysticks;
  34. #endif // LOL_USE_XINPUT
  35. };
  36. /*
  37. * Public D3d9Input class
  38. */
  39. D3d9Input::D3d9Input()
  40. : m_data(new D3d9InputData())
  41. {
  42. #if defined LOL_USE_XINPUT
  43. for (int i = 0; i < XUSER_MAX_COUNT; i++)
  44. {
  45. XINPUT_STATE state;
  46. if (XInputGetState(i, &state) != ERROR_SUCCESS)
  47. continue;
  48. auto stick = input::joystick(i);
  49. stick->internal_add_axis(input::axis::LeftX, "LeftX");
  50. stick->internal_add_axis(input::axis::LeftY, "LeftY");
  51. stick->internal_add_axis(input::axis::RightX, "RightX");
  52. stick->internal_add_axis(input::axis::RightY, "RightY");
  53. stick->internal_add_axis(input::axis::LeftShoulder, "LeftShoulder");
  54. stick->internal_add_axis(input::axis::RightShoulder, "RightShoulder");
  55. #define _BTN(id, name) stick->internal_add_button(input::button::BTN_##name, #name);
  56. #include "ui/buttons.inc" // FIXME: ignore mouse buttons here
  57. m_data->m_joysticks.push(i, stick);
  58. }
  59. #endif
  60. m_gamegroup = tickable::group::game::input;
  61. }
  62. D3d9Input::~D3d9Input()
  63. {
  64. #if defined LOL_USE_XINPUT
  65. /* Unregister all the joysticks we added (FIXME: this code seems unnecessary) */
  66. while (m_data->m_joysticks.count())
  67. m_data->m_joysticks.remove(0);
  68. #endif
  69. delete m_data;
  70. }
  71. void D3d9Input::tick_game(float seconds)
  72. {
  73. entity::tick_game(seconds);
  74. #if defined LOL_USE_XINPUT
  75. for (int i = 0; i < m_data->m_joysticks.count(); i++)
  76. {
  77. XINPUT_STATE state;
  78. if (XInputGetState(m_data->m_joysticks[i].m1, &state) != ERROR_SUCCESS)
  79. continue;
  80. auto stick = m_data->m_joysticks[i].m2;
  81. stick->internal_begin_frame();
  82. stick->internal_set_axis(input::axis::LeftX, state.Gamepad.sThumbLX / 32768.f);
  83. stick->internal_set_axis(input::axis::LeftY, -state.Gamepad.sThumbLY / 32768.f);
  84. stick->internal_set_axis(input::axis::RightX, state.Gamepad.sThumbRX / 32768.f);
  85. stick->internal_set_axis(input::axis::RightY, -state.Gamepad.sThumbRY / 32768.f);
  86. stick->internal_set_axis(input::axis::LeftShoulder, state.Gamepad.bLeftTrigger / 32768.f);
  87. stick->internal_set_axis(input::axis::RightShoulder, state.Gamepad.bRightTrigger / 32768.f);
  88. for (int b = 0; b < 16; b++)
  89. {
  90. // Reserved values
  91. if ((1 << b) > XINPUT_GAMEPAD_RIGHT_SHOULDER && (1 << b) < XINPUT_GAMEPAD_A)
  92. continue;
  93. int key_index = (1 << b) > XINPUT_GAMEPAD_RIGHT_SHOULDER ? b - 2 : b;
  94. m_data->m_joysticks[i].m2->internal_set_button((input::button)key_index, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1);
  95. }
  96. }
  97. #endif
  98. }
  99. void D3d9Input::tick_draw(float seconds, Scene &scene)
  100. {
  101. entity::tick_draw(seconds, scene);
  102. }
  103. } /* namespace lol */