Canvas.cpp 7.3 KB

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