2
0

CursorSoftware.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "../GameEngine.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::update()
  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. }
  26. void CursorSoftware::render()
  27. {
  28. Point renderPos = pos - pivot;
  29. SDL_Rect destRect;
  30. destRect.x = renderPos.x;
  31. destRect.y = renderPos.y;
  32. destRect.w = cursorSurface->w;
  33. destRect.h = cursorSurface->h;
  34. SDL_RenderCopy(mainRenderer, cursorTexture, nullptr, &destRect);
  35. }
  36. void CursorSoftware::createTexture(const Point & dimensions)
  37. {
  38. if(cursorTexture)
  39. SDL_DestroyTexture(cursorTexture);
  40. if (cursorSurface)
  41. SDL_FreeSurface(cursorSurface);
  42. cursorSurface = CSDL_Ext::newSurface(dimensions);
  43. cursorTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, dimensions.x, dimensions.y);
  44. SDL_SetSurfaceBlendMode(cursorSurface, SDL_BLENDMODE_NONE);
  45. SDL_SetTextureBlendMode(cursorTexture, SDL_BLENDMODE_BLEND);
  46. }
  47. void CursorSoftware::updateTexture()
  48. {
  49. if (!cursorSurface)
  50. createTexture(cursorImage->dimensions() * ENGINE->screenHandler().getScalingFactor());
  51. Point currentSize = Point(cursorSurface->w, cursorSurface->h);
  52. if (currentSize != cursorImage->dimensions() * ENGINE->screenHandler().getScalingFactor())
  53. createTexture(cursorImage->dimensions() * ENGINE->screenHandler().getScalingFactor());
  54. CSDL_Ext::fillSurface(cursorSurface, CSDL_Ext::toSDL(Colors::TRANSPARENCY));
  55. cursorImage->draw(cursorSurface, Point(0,0), nullptr, ENGINE->screenHandler().getScalingFactor());
  56. SDL_UpdateTexture(cursorTexture, nullptr, cursorSurface->pixels, cursorSurface->pitch);
  57. needUpdate = false;
  58. }
  59. void CursorSoftware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  60. {
  61. assert(image != nullptr);
  62. cursorImage = image;
  63. pivot = pivotOffset;
  64. needUpdate = true;
  65. }
  66. void CursorSoftware::setCursorPosition( const Point & newPos )
  67. {
  68. pos = newPos;
  69. }
  70. void CursorSoftware::setVisible(bool on)
  71. {
  72. visible = on;
  73. }
  74. CursorSoftware::CursorSoftware():
  75. cursorTexture(nullptr),
  76. cursorSurface(nullptr),
  77. needUpdate(false),
  78. visible(false),
  79. pivot(0,0)
  80. {
  81. SDL_ShowCursor(SDL_DISABLE);
  82. }
  83. CursorSoftware::~CursorSoftware()
  84. {
  85. if(cursorTexture)
  86. SDL_DestroyTexture(cursorTexture);
  87. if (cursorSurface)
  88. SDL_FreeSurface(cursorSurface);
  89. }