CursorHardware.cpp 2.4 KB

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