Canvas.cpp 7.3 KB

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