2
0

CursorHardware.cpp 1.7 KB

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