Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

128 строки
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 <lol/engine-internal.h>
  20. #include "ui/d3d9-input.h"
  21. #include "ui/input.h"
  22. namespace lol
  23. {
  24. /*
  25. * D3d9 Input implementation class
  26. */
  27. class D3d9InputData
  28. {
  29. friend class D3d9Input;
  30. private:
  31. #if defined LOL_USE_XINPUT
  32. array<int, InputDevice*> m_joysticks;
  33. #endif // LOL_USE_XINPUT
  34. };
  35. /*
  36. * Public D3d9Input class
  37. */
  38. D3d9Input::D3d9Input()
  39. : m_data(new D3d9InputData())
  40. {
  41. #if defined LOL_USE_XINPUT
  42. for (int i = 0; i < XUSER_MAX_COUNT; i++)
  43. {
  44. XINPUT_STATE state;
  45. if (XInputGetState(i, &state) != ERROR_SUCCESS)
  46. continue;
  47. // TODO: we can put more friendly name here, such as LeftAxisX, ButtonX...
  48. InputDevice* stick = new InputDevice(g_name_joystick(i + 1));
  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 */
  66. while (m_data->m_joysticks.count())
  67. {
  68. delete m_data->m_joysticks[0].m2;
  69. m_data->m_joysticks.remove(0);
  70. }
  71. #endif
  72. delete m_data;
  73. }
  74. void D3d9Input::tick_game(float seconds)
  75. {
  76. Entity::tick_game(seconds);
  77. #if defined LOL_USE_XINPUT
  78. for (int i = 0; i < m_data->m_joysticks.count(); i++)
  79. {
  80. XINPUT_STATE state;
  81. if (XInputGetState(m_data->m_joysticks[i].m1, &state) != ERROR_SUCCESS)
  82. continue;
  83. m_data->m_joysticks[i].m2->internal_set_axis(0, (float)state.Gamepad.sThumbLX / 32768.f);
  84. m_data->m_joysticks[i].m2->internal_set_axis(1, -(float)state.Gamepad.sThumbLY / 32768.f);
  85. m_data->m_joysticks[i].m2->internal_set_axis(2, (float)state.Gamepad.sThumbRX / 32768.f);
  86. m_data->m_joysticks[i].m2->internal_set_axis(3, -(float)state.Gamepad.sThumbRY / 32768.f);
  87. m_data->m_joysticks[i].m2->internal_set_axis(4, (float)state.Gamepad.bLeftTrigger / 32768.f);
  88. m_data->m_joysticks[i].m2->internal_set_axis(5, (float)state.Gamepad.bRightTrigger / 32768.f);
  89. for (int b = 0; b < 16; b++)
  90. {
  91. // Reserved values
  92. if ((1 << b) > XINPUT_GAMEPAD_RIGHT_SHOULDER && (1 << b) < XINPUT_GAMEPAD_A)
  93. continue;
  94. int key_index = (1 << b) > XINPUT_GAMEPAD_RIGHT_SHOULDER ? b - 2 : b;
  95. m_data->m_joysticks[i].m2->internal_set_button((input::button)key_index, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1);
  96. }
  97. }
  98. #endif
  99. }
  100. void D3d9Input::tick_draw(float seconds, Scene &scene)
  101. {
  102. Entity::tick_draw(seconds, scene);
  103. }
  104. } /* namespace lol */