CursorSoftware.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if (!cursorSurface || Point(cursorSurface->w, cursorSurface->h) != cursorImage->dimensions())
  45. createTexture(cursorImage->dimensions());
  46. CSDL_Ext::fillSurface(cursorSurface, CSDL_Ext::toSDL(Colors::TRANSPARENCY));
  47. cursorImage->draw(cursorSurface, Point(0,0));
  48. SDL_UpdateTexture(cursorTexture, nullptr, cursorSurface->pixels, cursorSurface->pitch);
  49. needUpdate = false;
  50. }
  51. void CursorSoftware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  52. {
  53. assert(image != nullptr);
  54. cursorImage = image;
  55. pivot = pivotOffset;
  56. needUpdate = true;
  57. }
  58. void CursorSoftware::setCursorPosition( const Point & newPos )
  59. {
  60. pos = newPos;
  61. }
  62. void CursorSoftware::setVisible(bool on)
  63. {
  64. visible = on;
  65. }
  66. CursorSoftware::CursorSoftware():
  67. cursorTexture(nullptr),
  68. cursorSurface(nullptr),
  69. needUpdate(false),
  70. visible(false),
  71. pivot(0,0)
  72. {
  73. SDL_ShowCursor(SDL_DISABLE);
  74. }
  75. CursorSoftware::~CursorSoftware()
  76. {
  77. if(cursorTexture)
  78. SDL_DestroyTexture(cursorTexture);
  79. if (cursorSurface)
  80. SDL_FreeSurface(cursorSurface);
  81. }