2
0

CursorSoftware.cpp 2.4 KB

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