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.

132 lines
3.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #if defined USE_XINPUT
  14. # include <d3d9.h>
  15. # include <xinput.h>
  16. #endif
  17. #include <lol/engine-internal.h>
  18. #include "d3d9input.h"
  19. #include "input/input_internal.h"
  20. namespace lol
  21. {
  22. /*
  23. * D3d9 Input implementation class
  24. */
  25. class D3d9InputData
  26. {
  27. friend class D3d9Input;
  28. private:
  29. #if defined USE_XINPUT
  30. array<int, InputDeviceInternal*> m_joysticks;
  31. #endif // USE_XINPUT
  32. };
  33. /*
  34. * Public D3d9Input class
  35. */
  36. D3d9Input::D3d9Input()
  37. : m_data(new D3d9InputData())
  38. {
  39. #if defined USE_XINPUT
  40. for (int i = 0; i < XUSER_MAX_COUNT; i++)
  41. {
  42. XINPUT_STATE state;
  43. if (XInputGetState(i, &state) != ERROR_SUCCESS)
  44. continue;
  45. // TODO: we can put more friendly name here, such as LeftAxisX, ButtonX...
  46. InputDeviceInternal* stick = new InputDeviceInternal(String::Printf("Joystick%d", i+1).C());
  47. for (int j = 0; j < 4; ++j)
  48. stick->AddAxis(String::Printf("Axis%d", j+1).C());
  49. stick->AddKey("DPadUp");
  50. stick->AddKey("DPadDown");
  51. stick->AddKey("DPadLeft");
  52. stick->AddKey("DPadRight");
  53. stick->AddKey("Start");
  54. stick->AddKey("Back");
  55. stick->AddKey("LeftThumb");
  56. stick->AddKey("RightThumb");
  57. stick->AddKey("LeftShoulder");
  58. stick->AddKey("RightShoulder");
  59. stick->AddKey("A");
  60. stick->AddKey("B");
  61. stick->AddKey("X");
  62. stick->AddKey("Y");
  63. m_data->m_joysticks.Push(i, stick);
  64. }
  65. #endif
  66. m_gamegroup = GAMEGROUP_BEFORE;
  67. }
  68. D3d9Input::~D3d9Input()
  69. {
  70. #if defined USE_XINPUT
  71. /* Unregister all the joysticks we added */
  72. while (m_data->m_joysticks.Count())
  73. {
  74. delete m_data->m_joysticks[0].m2;
  75. m_data->m_joysticks.Remove(0);
  76. }
  77. #endif
  78. delete m_data;
  79. }
  80. void D3d9Input::TickGame(float seconds)
  81. {
  82. Entity::TickGame(seconds);
  83. #if defined USE_XINPUT
  84. for (int i = 0; i < m_data->m_joysticks.Count(); i++)
  85. {
  86. XINPUT_STATE state;
  87. if (XInputGetState(m_data->m_joysticks[i].m1, &state) != ERROR_SUCCESS)
  88. continue;
  89. m_data->m_joysticks[i].m2->SetAxis(0, (float)state.Gamepad.sThumbLX / 32768.f);
  90. m_data->m_joysticks[i].m2->SetAxis(1, -(float)state.Gamepad.sThumbLY / 32768.f);
  91. m_data->m_joysticks[i].m2->SetAxis(2, (float)state.Gamepad.sThumbRX / 32768.f);
  92. m_data->m_joysticks[i].m2->SetAxis(3, -(float)state.Gamepad.sThumbRY / 32768.f);
  93. for (int b = 0; b < 16; b++)
  94. {
  95. // Reserved values
  96. if ((1 << b) > XINPUT_GAMEPAD_RIGHT_SHOULDER && (1 << b) < XINPUT_GAMEPAD_A)
  97. continue;
  98. int key_index = (1 << b) > XINPUT_GAMEPAD_RIGHT_SHOULDER ? b - 2 : b;
  99. m_data->m_joysticks[i].m2->SetKey(key_index, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1);
  100. }
  101. }
  102. #endif
  103. }
  104. void D3d9Input::TickDraw(float seconds, Scene &scene)
  105. {
  106. Entity::TickDraw(seconds, scene);
  107. }
  108. } /* namespace lol */