CursorHardware.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "CursorHandler.h"
  12. #include "SDL_Extensions.h"
  13. #include "CGuiHandler.h"
  14. #include "CAnimation.h"
  15. #include "../../lib/CConfigHandler.h"
  16. #include <SDL_render.h>
  17. #include <SDL_events.h>
  18. #ifdef VCMI_APPLE
  19. #include <dispatch/dispatch.h>
  20. #endif
  21. CursorHardware::CursorHardware():
  22. cursor(nullptr)
  23. {
  24. SDL_ShowCursor(SDL_DISABLE);
  25. }
  26. CursorHardware::~CursorHardware()
  27. {
  28. if(cursor)
  29. SDL_FreeCursor(cursor);
  30. }
  31. void CursorHardware::setVisible(bool on)
  32. {
  33. #ifdef VCMI_APPLE
  34. dispatch_async(dispatch_get_main_queue(), ^{
  35. #endif
  36. if (on)
  37. SDL_ShowCursor(SDL_ENABLE);
  38. else
  39. SDL_ShowCursor(SDL_DISABLE);
  40. #ifdef VCMI_APPLE
  41. });
  42. #endif
  43. }
  44. void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  45. {
  46. auto cursorSurface = CSDL_Ext::newSurface(image->dimensions().x, image->dimensions().y);
  47. CSDL_Ext::fillSurface(cursorSurface, Colors::TRANSPARENCY);
  48. image->draw(cursorSurface);
  49. auto oldCursor = cursor;
  50. cursor = SDL_CreateColorCursor(cursorSurface, pivotOffset.x, pivotOffset.y);
  51. if (!cursor)
  52. logGlobal->error("Failed to set cursor! SDL says %s", SDL_GetError());
  53. SDL_FreeSurface(cursorSurface);
  54. #ifdef VCMI_APPLE
  55. dispatch_async(dispatch_get_main_queue(), ^{
  56. #endif
  57. SDL_SetCursor(cursor);
  58. if (oldCursor)
  59. SDL_FreeCursor(oldCursor);
  60. #ifdef VCMI_APPLE
  61. });
  62. #endif
  63. }
  64. void CursorHardware::setCursorPosition( const Point & newPos )
  65. {
  66. //no-op
  67. }
  68. void CursorHardware::render()
  69. {
  70. //no-op
  71. }