77 lines
1.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. #include <SDL.h>
  14. #include <cstdio>
  15. #include <cstdlib>
  16. #include <cmath>
  17. #include "core.h"
  18. /*
  19. * Input implementation class
  20. */
  21. static class InputData
  22. {
  23. friend class Input;
  24. public:
  25. InputData()
  26. {
  27. mouse.x = mouse.y = -1;
  28. }
  29. Int2 mouse;
  30. }
  31. inputdata;
  32. static InputData * const data = &inputdata;
  33. /*
  34. * Public Input class
  35. */
  36. Float2 Input::GetAxis(int axis)
  37. {
  38. float invsqrt2 = sqrtf(0.5f);
  39. Float2 f;
  40. /* Simulate a joystick using the keyboard. This SDL call is free. */
  41. Uint8 *keystate = SDL_GetKeyState(NULL);
  42. int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
  43. int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
  44. f.x += left;
  45. f.y += up;
  46. if (left && up)
  47. {
  48. f.x *= invsqrt2;
  49. f.y *= invsqrt2;
  50. }
  51. return f;
  52. }
  53. void Input::SetMousePos(Int2 coord)
  54. {
  55. data->mouse = coord;
  56. }
  57. Int2 Input::GetMousePos()
  58. {
  59. return data->mouse;
  60. }