199 lines
4.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdlib>
  14. #include <cmath>
  15. #if defined USE_SDL
  16. # include <SDL.h>
  17. #endif
  18. #include "core.h"
  19. namespace lol
  20. {
  21. /*
  22. * Input implementation class
  23. */
  24. static class InputData
  25. {
  26. friend class Input;
  27. public:
  28. InputData()
  29. : mouse(-1),
  30. buttons(0),
  31. nentities(0),
  32. lastfocus(0)
  33. { }
  34. private:
  35. ivec2 mouse;
  36. ivec3 buttons;
  37. static int const MAX_ENTITIES = 100;
  38. WorldEntity *entities[MAX_ENTITIES];
  39. int nentities;
  40. WorldEntity *lastfocus;
  41. }
  42. inputdata;
  43. static InputData * const data = &inputdata;
  44. /*
  45. * Public Input class
  46. */
  47. vec2 Input::GetAxis(int axis)
  48. {
  49. vec2 ret;
  50. #if defined USE_SDL
  51. /* Simulate a joystick using the keyboard. This SDL call is free. */
  52. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
  53. Uint8 *keystate = SDL_GetKeyboardState(NULL);
  54. #else
  55. Uint8 *keystate = SDL_GetKeyState(NULL);
  56. #endif
  57. int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
  58. int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
  59. ret.x += left;
  60. ret.y += up;
  61. if (left && up)
  62. ret = ret * sqrtf(0.5f);
  63. #else
  64. ret = vec2(0, 0);
  65. #endif
  66. return ret;
  67. }
  68. ivec2 Input::GetMousePos()
  69. {
  70. return data->mouse;
  71. }
  72. ivec3 Input::GetMouseButtons()
  73. {
  74. return data->buttons;
  75. }
  76. //BH : Added this, is a v0.1 Alpha version.
  77. int Input::GetButtonState(int button)
  78. {
  79. #if defined USE_SDL
  80. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
  81. Uint8 *keystate = SDL_GetKeyboardState(NULL);
  82. #else
  83. Uint8 *keystate = SDL_GetKeyState(NULL);
  84. #endif
  85. return keystate[button];
  86. #else
  87. return 0;
  88. #endif
  89. }
  90. void Input::TrackMouse(WorldEntity *e)
  91. {
  92. if (data->nentities >= InputData::MAX_ENTITIES)
  93. return;
  94. data->entities[data->nentities] = e;
  95. data->nentities++;
  96. }
  97. void Input::UntrackMouse(WorldEntity *e)
  98. {
  99. for (int n = 0; n < data->nentities; n++)
  100. {
  101. if (data->entities[n] != e)
  102. continue;
  103. data->entities[n] = data->entities[data->nentities - 1];
  104. data->nentities--;
  105. n--;
  106. }
  107. }
  108. void Input::SetMousePos(ivec2 coord)
  109. {
  110. data->mouse = coord;
  111. WorldEntity *top = NULL;
  112. for (int n = 0; n < data->nentities; n++)
  113. {
  114. if (coord.x < data->entities[n]->m_bbox[0].x
  115. || coord.x >= data->entities[n]->m_bbox[1].x
  116. || coord.y < data->entities[n]->m_bbox[0].y
  117. || coord.y >= data->entities[n]->m_bbox[1].y)
  118. continue;
  119. if (!top || top->m_bbox[1].z < data->entities[n]->m_bbox[1].z)
  120. top = data->entities[n];
  121. }
  122. for (int n = 0; n < data->nentities; n++)
  123. {
  124. if (data->entities[n] == top)
  125. {
  126. data->entities[n]->m_mousepos = coord - (ivec2)top->m_bbox[0].xy;
  127. if (top != data->lastfocus)
  128. data->entities[n]->m_pressed = data->buttons;
  129. else
  130. data->entities[n]->m_clicked = ivec3(0);
  131. }
  132. else
  133. {
  134. data->entities[n]->m_mousepos = ivec2(-1);
  135. /* FIXME */
  136. data->entities[n]->m_released = ivec3(0);
  137. data->entities[n]->m_pressed = ivec3(0);
  138. data->entities[n]->m_clicked = ivec3(0);
  139. }
  140. }
  141. data->lastfocus = top;
  142. }
  143. void Input::SetMouseButton(int index)
  144. {
  145. data->buttons[index] = 1;
  146. if (data->lastfocus)
  147. {
  148. if (!data->lastfocus->m_pressed[index])
  149. data->lastfocus->m_clicked[index] = 1;
  150. data->lastfocus->m_pressed[index] = 1;
  151. data->lastfocus->m_released[index] = 0;
  152. }
  153. }
  154. void Input::UnsetMouseButton(int index)
  155. {
  156. data->buttons[index] = 0;
  157. if (data->lastfocus)
  158. {
  159. if (data->lastfocus->m_pressed[index])
  160. data->lastfocus->m_released[index] = 1;
  161. data->lastfocus->m_pressed[index] = 0;
  162. data->lastfocus->m_clicked[index] = 0;
  163. }
  164. }
  165. } /* namespace lol */