Canvas.cpp 6.6 KB

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