CursorSoftware.cpp 2.5 KB

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