xboxinput.cpp 2.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include <lol/engine-internal.h>
  11. #if defined _XBOX
  12. # include <xtl.h>
  13. # include <xbdm.h>
  14. #endif
  15. #include "xboxinput.h"
  16. namespace lol
  17. {
  18. /*
  19. * Xbox Input implementation class
  20. */
  21. class XboxInputData
  22. {
  23. friend class XboxInput;
  24. private:
  25. #if defined _XBOX
  26. array<int, Stick *> m_joysticks;
  27. #endif
  28. };
  29. /*
  30. * Public XboxInput class
  31. */
  32. XboxInput::XboxInput()
  33. : m_data(new XboxInputData())
  34. {
  35. #if defined _XBOX
  36. for (int i = 0; i < XUSER_MAX_COUNT; i++)
  37. {
  38. XINPUT_STATE state;
  39. if (XInputGetState(i, &state) != ERROR_SUCCESS)
  40. continue;
  41. Stick *stick = Input::CreateStick();
  42. stick->SetAxisCount(4);
  43. stick->SetButtonCount(16);
  44. m_data->m_joysticks.Push(i, stick);
  45. }
  46. #endif
  47. m_gamegroup = GAMEGROUP_INPUT;
  48. }
  49. XboxInput::~XboxInput()
  50. {
  51. #if defined _XBOX
  52. /* Unregister all the joysticks we added */
  53. while (m_data->m_joysticks.Count())
  54. {
  55. Input::DestroyStick(m_data->m_joysticks[0].m2);
  56. m_data->m_joysticks.Remove(0);
  57. }
  58. #endif
  59. delete m_data;
  60. }
  61. void XboxInput::TickGame(float seconds)
  62. {
  63. Entity::TickGame(seconds);
  64. }
  65. void XboxInput::TickDraw(float seconds, Scene &scene)
  66. {
  67. Entity::TickDraw(seconds, scene);
  68. #if defined _XBOX
  69. for (int i = 0; i < m_data->m_joysticks.Count(); i++)
  70. {
  71. XINPUT_STATE state;
  72. if (XInputGetState(m_data->m_joysticks[i].m1, &state) != ERROR_SUCCESS)
  73. continue;
  74. m_data->m_joysticks[i].m2->SetAxis(0, (float)state.Gamepad.sThumbLX / 32768.f);
  75. m_data->m_joysticks[i].m2->SetAxis(1, -(float)state.Gamepad.sThumbLY / 32768.f);
  76. m_data->m_joysticks[i].m2->SetAxis(2, (float)state.Gamepad.sThumbRX / 32768.f);
  77. m_data->m_joysticks[i].m2->SetAxis(3, -(float)state.Gamepad.sThumbRY / 32768.f);
  78. for (int b = 0; b < 16; b++)
  79. m_data->m_joysticks[i].m2->SetButton(b, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1);
  80. }
  81. #endif
  82. }
  83. } /* namespace lol */