Canvas.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Canvas.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 "Canvas.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../media/IVideoPlayer.h"
  14. #include "../render/IRenderHandler.h"
  15. #include "../render/IScreenHandler.h"
  16. #include "../renderSDL/SDL_Extensions.h"
  17. #include "Colors.h"
  18. #include "IImage.h"
  19. #include "Graphics.h"
  20. #include "IFont.h"
  21. #include <SDL_surface.h>
  22. #include <SDL_pixels.h>
  23. Canvas::Canvas(SDL_Surface * surface, CanvasScalingPolicy scalingPolicy):
  24. scalingPolicy(scalingPolicy),
  25. surface(surface),
  26. renderArea(0,0, surface->w, surface->h)
  27. {
  28. surface->refcount++;
  29. }
  30. Canvas::Canvas(const Canvas & other):
  31. scalingPolicy(other.scalingPolicy),
  32. surface(other.surface),
  33. renderArea(other.renderArea)
  34. {
  35. surface->refcount++;
  36. }
  37. Canvas::Canvas(Canvas && other):
  38. scalingPolicy(other.scalingPolicy),
  39. surface(other.surface),
  40. renderArea(other.renderArea)
  41. {
  42. surface->refcount++;
  43. }
  44. Canvas::Canvas(const Canvas & other, const Rect & newClipRect):
  45. Canvas(other)
  46. {
  47. Rect scaledClipRect( transformPos(newClipRect.topLeft()), transformPos(newClipRect.dimensions()));
  48. renderArea = other.renderArea.intersect(scaledClipRect + other.renderArea.topLeft());
  49. }
  50. Canvas::Canvas(const Point & size, CanvasScalingPolicy scalingPolicy):
  51. scalingPolicy(scalingPolicy),
  52. surface(CSDL_Ext::newSurface(size * getScalingFactor())),
  53. renderArea(Point(0,0), size * getScalingFactor())
  54. {
  55. CSDL_Ext::fillSurface(surface, CSDL_Ext::toSDL(Colors::TRANSPARENCY) );
  56. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
  57. }
  58. int Canvas::getScalingFactor() const
  59. {
  60. if (scalingPolicy == CanvasScalingPolicy::IGNORE)
  61. return 1;
  62. return GH.screenHandler().getScalingFactor();
  63. }
  64. Point Canvas::transformPos(const Point & input)
  65. {
  66. return renderArea.topLeft() + input * getScalingFactor();
  67. }
  68. Point Canvas::transformSize(const Point & input)
  69. {
  70. return input * getScalingFactor();
  71. }
  72. Canvas Canvas::createFromSurface(SDL_Surface * surface, CanvasScalingPolicy scalingPolicy)
  73. {
  74. return Canvas(surface, scalingPolicy);
  75. }
  76. void Canvas::applyTransparency(bool on)
  77. {
  78. if (on)
  79. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  80. else
  81. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
  82. }
  83. void Canvas::applyGrayscale()
  84. {
  85. CSDL_Ext::convertToGrayscale(surface, renderArea);
  86. }
  87. Canvas::~Canvas()
  88. {
  89. SDL_FreeSurface(surface);
  90. }
  91. void Canvas::draw(IVideoInstance & video, const Point & pos)
  92. {
  93. video.show(pos, surface);
  94. }
  95. void Canvas::draw(const IImage& image, const Point & pos)
  96. {
  97. image.draw(surface, transformPos(pos), nullptr, getScalingFactor());
  98. }
  99. void Canvas::draw(const std::shared_ptr<IImage>& image, const Point & pos)
  100. {
  101. assert(image);
  102. if (image)
  103. image->draw(surface, transformPos(pos), nullptr, getScalingFactor());
  104. }
  105. void Canvas::draw(const std::shared_ptr<IImage>& image, const Point & pos, const Rect & sourceRect)
  106. {
  107. Rect realSourceRect = sourceRect * getScalingFactor();
  108. assert(image);
  109. if (image)
  110. image->draw(surface, transformPos(pos), &realSourceRect, getScalingFactor());
  111. }
  112. void Canvas::draw(const Canvas & image, const Point & pos)
  113. {
  114. CSDL_Ext::blitSurface(image.surface, image.renderArea, surface, transformPos(pos));
  115. }
  116. void Canvas::drawTransparent(const Canvas & image, const Point & pos, double transparency)
  117. {
  118. SDL_BlendMode oldMode;
  119. SDL_GetSurfaceBlendMode(image.surface, &oldMode);
  120. SDL_SetSurfaceBlendMode(image.surface, SDL_BLENDMODE_BLEND);
  121. SDL_SetSurfaceAlphaMod(image.surface, 255 * transparency);
  122. CSDL_Ext::blitSurface(image.surface, image.renderArea, surface, transformPos(pos));
  123. SDL_SetSurfaceAlphaMod(image.surface, 255);
  124. SDL_SetSurfaceBlendMode(image.surface, oldMode);
  125. }
  126. void Canvas::drawScaled(const Canvas & image, const Point & pos, const Point & targetSize)
  127. {
  128. SDL_Rect targetRect = CSDL_Ext::toSDL(Rect(transformPos(pos), transformSize(targetSize)));
  129. SDL_BlitScaled(image.surface, nullptr, surface, &targetRect);
  130. }
  131. void Canvas::drawPoint(const Point & dest, const ColorRGBA & color)
  132. {
  133. Point point = transformPos(dest);
  134. CSDL_Ext::putPixelWithoutRefreshIfInSurf(surface, point.x, point.y, color.r, color.g, color.b, color.a);
  135. }
  136. void Canvas::drawLine(const Point & from, const Point & dest, const ColorRGBA & colorFrom, const ColorRGBA & colorDest)
  137. {
  138. CSDL_Ext::drawLine(surface, transformPos(from), transformPos(dest), CSDL_Ext::toSDL(colorFrom), CSDL_Ext::toSDL(colorDest), getScalingFactor());
  139. }
  140. void Canvas::drawBorder(const Rect & target, const ColorRGBA & color, int width)
  141. {
  142. Rect realTarget = target * getScalingFactor() + renderArea.topLeft();
  143. CSDL_Ext::drawBorder(surface, realTarget.x, realTarget.y, realTarget.w, realTarget.h, CSDL_Ext::toSDL(color), width * getScalingFactor());
  144. }
  145. void Canvas::drawBorderDashed(const Rect & target, const ColorRGBA & color)
  146. {
  147. Rect realTarget = target * getScalingFactor() + renderArea.topLeft();
  148. CSDL_Ext::drawLineDashed(surface, realTarget.topLeft(), realTarget.topRight(), CSDL_Ext::toSDL(color));
  149. CSDL_Ext::drawLineDashed(surface, realTarget.bottomLeft(), realTarget.bottomRight(), CSDL_Ext::toSDL(color));
  150. CSDL_Ext::drawLineDashed(surface, realTarget.topLeft(), realTarget.bottomLeft(), CSDL_Ext::toSDL(color));
  151. CSDL_Ext::drawLineDashed(surface, realTarget.topRight(), realTarget.bottomRight(), CSDL_Ext::toSDL(color));
  152. }
  153. void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::string & text )
  154. {
  155. const auto & fontPtr = GH.renderHandler().loadFont(font);
  156. switch (alignment)
  157. {
  158. case ETextAlignment::TOPLEFT: return fontPtr->renderTextLeft (surface, text, colorDest, transformPos(position));
  159. case ETextAlignment::TOPCENTER: return fontPtr->renderTextCenter(surface, text, colorDest, transformPos(position));
  160. case ETextAlignment::CENTER: return fontPtr->renderTextCenter(surface, text, colorDest, transformPos(position));
  161. case ETextAlignment::BOTTOMRIGHT: return fontPtr->renderTextRight (surface, text, colorDest, transformPos(position));
  162. }
  163. }
  164. void Canvas::drawText(const Point & position, const EFonts & font, const ColorRGBA & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
  165. {
  166. const auto & fontPtr = GH.renderHandler().loadFont(font);
  167. switch (alignment)
  168. {
  169. case ETextAlignment::TOPLEFT: return fontPtr->renderTextLinesLeft (surface, text, colorDest, transformPos(position));
  170. case ETextAlignment::TOPCENTER: return fontPtr->renderTextLinesCenter(surface, text, colorDest, transformPos(position));
  171. case ETextAlignment::CENTER: return fontPtr->renderTextLinesCenter(surface, text, colorDest, transformPos(position));
  172. case ETextAlignment::BOTTOMRIGHT: return fontPtr->renderTextLinesRight (surface, text, colorDest, transformPos(position));
  173. }
  174. }
  175. void Canvas::drawColor(const Rect & target, const ColorRGBA & color)
  176. {
  177. Rect realTarget = target * getScalingFactor() + renderArea.topLeft();
  178. CSDL_Ext::fillRect(surface, realTarget, CSDL_Ext::toSDL(color));
  179. }
  180. void Canvas::drawColorBlended(const Rect & target, const ColorRGBA & color)
  181. {
  182. Rect realTarget = target * getScalingFactor() + renderArea.topLeft();
  183. CSDL_Ext::fillRectBlended(surface, realTarget, CSDL_Ext::toSDL(color));
  184. }
  185. void Canvas::fillTexture(const std::shared_ptr<IImage>& image)
  186. {
  187. assert(image);
  188. if (!image)
  189. return;
  190. Rect imageArea(Point(0, 0), image->dimensions());
  191. for (int y=0; y < surface->h; y+= imageArea.h)
  192. {
  193. for (int x=0; x < surface->w; x+= imageArea.w)
  194. image->draw(surface, Point(renderArea.x + x * getScalingFactor(), renderArea.y + y * getScalingFactor()), nullptr, getScalingFactor());
  195. }
  196. }
  197. Rect Canvas::getRenderArea() const
  198. {
  199. return renderArea;
  200. }
  201. CanvasClipRectGuard::CanvasClipRectGuard(Canvas & canvas, const Rect & rect): surf(canvas.surface)
  202. {
  203. CSDL_Ext::getClipRect(surf, oldRect);
  204. CSDL_Ext::setClipRect(surf, rect * GH.screenHandler().getScalingFactor());
  205. }
  206. CanvasClipRectGuard::~CanvasClipRectGuard()
  207. {
  208. CSDL_Ext::setClipRect(surf, oldRect);
  209. }