CursorSoftware.cpp 2.4 KB

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