Canvas.cpp 7.3 KB

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