CursorSoftware.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * CursorSoftware.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 "CursorSoftware.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../render/IScreenHandler.h"
  14. #include "../render/Colors.h"
  15. #include "../render/IImage.h"
  16. #include "../CMT.h"
  17. #include "SDL_Extensions.h"
  18. #include <SDL_render.h>
  19. #include <SDL_events.h>
  20. void CursorSoftware::render()
  21. {
  22. //texture must be updated in the main (renderer) thread, but changes to cursor type may come from other threads
  23. if (needUpdate)
  24. updateTexture();
  25. Point renderPos = pos - pivot;
  26. SDL_Rect destRect;
  27. destRect.x = renderPos.x;
  28. destRect.y = renderPos.y;
  29. destRect.w = cursorSurface->w;
  30. destRect.h = cursorSurface->h;
  31. SDL_RenderCopy(mainRenderer, cursorTexture, nullptr, &destRect);
  32. }
  33. void CursorSoftware::createTexture(const Point & dimensions)
  34. {
  35. if(cursorTexture)
  36. SDL_DestroyTexture(cursorTexture);
  37. if (cursorSurface)
  38. SDL_FreeSurface(cursorSurface);
  39. cursorSurface = CSDL_Ext::newSurface(dimensions);
  40. cursorTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, dimensions.x, dimensions.y);
  41. SDL_SetSurfaceBlendMode(cursorSurface, SDL_BLENDMODE_NONE);
  42. SDL_SetTextureBlendMode(cursorTexture, SDL_BLENDMODE_BLEND);
  43. }
  44. void CursorSoftware::updateTexture()
  45. {
  46. if (!cursorSurface)
  47. createTexture(cursorImage->dimensions() * GH.screenHandler().getScalingFactor());
  48. Point currentSize = Point(cursorSurface->w, cursorSurface->h);
  49. if (currentSize != cursorImage->dimensions() * GH.screenHandler().getScalingFactor())
  50. createTexture(cursorImage->dimensions() * GH.screenHandler().getScalingFactor());
  51. CSDL_Ext::fillSurface(cursorSurface, CSDL_Ext::toSDL(Colors::TRANSPARENCY));
  52. cursorImage->draw(cursorSurface, Point(0,0), nullptr, GH.screenHandler().getScalingFactor());
  53. SDL_UpdateTexture(cursorTexture, nullptr, cursorSurface->pixels, cursorSurface->pitch);
  54. needUpdate = false;
  55. }
  56. void CursorSoftware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  57. {
  58. assert(image != nullptr);
  59. cursorImage = image;
  60. pivot = pivotOffset;
  61. needUpdate = true;
  62. }
  63. void CursorSoftware::setCursorPosition( const Point & newPos )
  64. {
  65. pos = newPos;
  66. }
  67. void CursorSoftware::setVisible(bool on)
  68. {
  69. visible = on;
  70. }
  71. CursorSoftware::CursorSoftware():
  72. cursorTexture(nullptr),
  73. cursorSurface(nullptr),
  74. needUpdate(false),
  75. visible(false),
  76. pivot(0,0)
  77. {
  78. SDL_ShowCursor(SDL_DISABLE);
  79. }
  80. CursorSoftware::~CursorSoftware()
  81. {
  82. if(cursorTexture)
  83. SDL_DestroyTexture(cursorTexture);
  84. if (cursorSurface)
  85. SDL_FreeSurface(cursorSurface);
  86. }