CursorHardware.cpp 1.7 KB

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