Canvas.cpp 6.9 KB

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