CursorHardware.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "../gui/CGuiHandler.h"
  14. #include "../render/IScreenHandler.h"
  15. #include "../render/Colors.h"
  16. #include "../render/IImage.h"
  17. #include "../../lib/CConfigHandler.h"
  18. #include <SDL_render.h>
  19. #include <SDL_events.h>
  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. GH.dispatchMainThread([on]()
  33. {
  34. if (on)
  35. SDL_ShowCursor(SDL_ENABLE);
  36. else
  37. SDL_ShowCursor(SDL_DISABLE);
  38. });
  39. }
  40. void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  41. {
  42. int cursorScalingPercent = settings["video"]["resolution"]["scaling"].Integer();
  43. Point cursorDimensions = image->dimensions() * GH.screenHandler().getScalingFactor();
  44. Point cursorDimensionsScaled = image->dimensions() * cursorScalingPercent / 100;
  45. Point pivotOffsetScaled = pivotOffset * cursorScalingPercent / 100 / GH.screenHandler().getScalingFactor();
  46. auto cursorSurface = CSDL_Ext::newSurface(cursorDimensions);
  47. CSDL_Ext::fillSurface(cursorSurface, CSDL_Ext::toSDL(Colors::TRANSPARENCY));
  48. image->draw(cursorSurface, Point(0,0));
  49. auto cursorSurfaceScaled = CSDL_Ext::scaleSurface(cursorSurface, cursorDimensionsScaled.x, cursorDimensionsScaled.y );
  50. auto oldCursor = cursor;
  51. cursor = SDL_CreateColorCursor(cursorSurfaceScaled, pivotOffsetScaled.x, pivotOffsetScaled.y);
  52. if (!cursor)
  53. logGlobal->error("Failed to set cursor! SDL says %s", SDL_GetError());
  54. SDL_FreeSurface(cursorSurface);
  55. SDL_FreeSurface(cursorSurfaceScaled);
  56. GH.dispatchMainThread([this, oldCursor](){
  57. SDL_SetCursor(cursor);
  58. if (oldCursor)
  59. SDL_FreeCursor(oldCursor);
  60. });
  61. }
  62. void CursorHardware::setCursorPosition( const Point & newPos )
  63. {
  64. //no-op
  65. }
  66. void CursorHardware::render()
  67. {
  68. //no-op
  69. }