120 lines
2.3 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. #if defined USE_SDL
  14. # include <SDL.h>
  15. #endif
  16. #include "core.h"
  17. #include "sdlinput.h"
  18. namespace lol
  19. {
  20. /*
  21. * SDL Input implementation class
  22. */
  23. class SdlInputData
  24. {
  25. friend class SdlInput;
  26. private:
  27. static ivec2 GetMousePos();
  28. };
  29. /*
  30. * Public SdlInput class
  31. */
  32. SdlInput::SdlInput()
  33. : data(new SdlInputData())
  34. {
  35. #if defined USE_SDL
  36. SDL_Init(SDL_INIT_TIMER);
  37. m_gamegroup = GAMEGROUP_BEFORE;
  38. #endif
  39. }
  40. void SdlInput::TickGame(float deltams)
  41. {
  42. #if defined USE_SDL
  43. Entity::TickGame(deltams);
  44. /* Handle mouse input */
  45. ivec2 mouse = SdlInputData::GetMousePos();;
  46. Input::SetMousePos(mouse);
  47. /* Handle keyboard and WM events */
  48. SDL_Event event;
  49. while (SDL_PollEvent(&event))
  50. {
  51. switch (event.type)
  52. {
  53. case SDL_QUIT:
  54. Ticker::Shutdown();
  55. break;
  56. #if 0
  57. case SDL_KEYDOWN:
  58. Input::KeyPressed(event.key.keysym.sym, deltams);
  59. break;
  60. #endif
  61. case SDL_MOUSEBUTTONDOWN:
  62. case SDL_MOUSEBUTTONUP:
  63. {
  64. ivec2 newmouse = SdlInputData::GetMousePos();
  65. if (newmouse != mouse)
  66. Input::SetMousePos(mouse = newmouse);
  67. if (event.type == SDL_MOUSEBUTTONDOWN)
  68. Input::SetMouseButton(event.button.button - 1);
  69. else
  70. Input::UnsetMouseButton(event.button.button - 1);
  71. break;
  72. }
  73. }
  74. }
  75. /* Send the whole keyboard state to the input system */
  76. #if 0
  77. Uint8 *keystate = SDL_GetKeyState(NULL);
  78. for (int i = 0; i < 256; i++)
  79. if (keystate[i])
  80. Input::KeyPressed(i, deltams);
  81. #endif
  82. #endif
  83. }
  84. SdlInput::~SdlInput()
  85. {
  86. delete data;
  87. }
  88. ivec2 SdlInputData::GetMousePos()
  89. {
  90. ivec2 ret(-1, -1);
  91. #if defined USE_SDL
  92. if (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
  93. {
  94. SDL_GetMouseState(&ret.x, &ret.y);
  95. ret.y = Video::GetSize().y - 1 - ret.y;
  96. }
  97. #endif
  98. return ret;
  99. }
  100. } /* namespace lol */