CursorHardware.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "../renderSDL/ScreenHandler.h"
  14. #include "../render/Colors.h"
  15. #include "../render/IImage.h"
  16. #include "SDL_Extensions.h"
  17. #include <SDL_render.h>
  18. #include <SDL_events.h>
  19. CursorHardware::CursorHardware():
  20. cursor(nullptr)
  21. {
  22. SDL_ShowCursor(SDL_DISABLE);
  23. }
  24. CursorHardware::~CursorHardware()
  25. {
  26. if(cursor)
  27. SDL_FreeCursor(cursor);
  28. }
  29. void CursorHardware::setVisible(bool on)
  30. {
  31. GH.dispatchMainThread([on]()
  32. {
  33. if (on)
  34. SDL_ShowCursor(SDL_ENABLE);
  35. else
  36. SDL_ShowCursor(SDL_DISABLE);
  37. });
  38. }
  39. void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  40. {
  41. auto cursorSurface = CSDL_Ext::newSurface(image->dimensions().x, image->dimensions().y);
  42. CSDL_Ext::fillSurface(cursorSurface, CSDL_Ext::toSDL(Colors::TRANSPARENCY));
  43. image->draw(cursorSurface, Point(0,0));
  44. auto oldCursor = cursor;
  45. cursor = SDL_CreateColorCursor(cursorSurface, pivotOffset.x, pivotOffset.y);
  46. if (!cursor)
  47. logGlobal->error("Failed to set cursor! SDL says %s", SDL_GetError());
  48. SDL_FreeSurface(cursorSurface);
  49. GH.dispatchMainThread([this, oldCursor](){
  50. SDL_SetCursor(cursor);
  51. if (oldCursor)
  52. SDL_FreeCursor(oldCursor);
  53. });
  54. }
  55. void CursorHardware::setCursorPosition( const Point & newPos )
  56. {
  57. //no-op
  58. }
  59. void CursorHardware::render()
  60. {
  61. //no-op
  62. }