CursorHardware.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * CursorHardware.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CursorHardware.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../render/Colors.h"
  14. #include "../render/IImage.h"
  15. #include "SDL_Extensions.h"
  16. #include <SDL_render.h>
  17. #include <SDL_events.h>
  18. CursorHardware::CursorHardware():
  19. cursor(nullptr)
  20. {
  21. SDL_ShowCursor(SDL_DISABLE);
  22. }
  23. CursorHardware::~CursorHardware()
  24. {
  25. if(cursor)
  26. SDL_FreeCursor(cursor);
  27. }
  28. void CursorHardware::setVisible(bool on)
  29. {
  30. GH.dispatchMainThread([on]()
  31. {
  32. if (on)
  33. SDL_ShowCursor(SDL_ENABLE);
  34. else
  35. SDL_ShowCursor(SDL_DISABLE);
  36. });
  37. }
  38. void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  39. {
  40. auto cursorSurface = CSDL_Ext::newSurface(image->dimensions().x, image->dimensions().y);
  41. CSDL_Ext::fillSurface(cursorSurface, Colors::TRANSPARENCY);
  42. image->draw(cursorSurface);
  43. auto oldCursor = cursor;
  44. cursor = SDL_CreateColorCursor(cursorSurface, pivotOffset.x, pivotOffset.y);
  45. if (!cursor)
  46. logGlobal->error("Failed to set cursor! SDL says %s", SDL_GetError());
  47. SDL_FreeSurface(cursorSurface);
  48. GH.dispatchMainThread([this, oldCursor](){
  49. SDL_SetCursor(cursor);
  50. if (oldCursor)
  51. SDL_FreeCursor(oldCursor);
  52. });
  53. }
  54. void CursorHardware::setCursorPosition( const Point & newPos )
  55. {
  56. //no-op
  57. }
  58. void CursorHardware::render()
  59. {
  60. //no-op
  61. }