Canvas.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <SDL_surface.h>
  17. Canvas::Canvas(SDL_Surface * surface):
  18. surface(surface),
  19. renderArea(0,0, surface->w, surface->h)
  20. {
  21. surface->refcount++;
  22. }
  23. Canvas::Canvas(const Canvas & other):
  24. surface(other.surface),
  25. renderArea(other.renderArea)
  26. {
  27. surface->refcount++;
  28. }
  29. Canvas::Canvas(Canvas && other):
  30. surface(other.surface),
  31. renderArea(other.renderArea)
  32. {
  33. surface->refcount++;
  34. }
  35. Canvas::Canvas(const Canvas & other, const Rect & newClipRect):
  36. Canvas(other)
  37. {
  38. renderArea = other.renderArea.intersect(newClipRect + other.renderArea.topLeft());
  39. }
  40. Canvas::Canvas(const Point & size):
  41. renderArea(Point(0,0), size),
  42. surface(CSDL_Ext::newSurface(size.x, size.y))
  43. {
  44. CSDL_Ext::fillSurface(surface, Colors::TRANSPARENCY );
  45. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
  46. }
  47. Canvas Canvas::createFromSurface(SDL_Surface * surface)
  48. {
  49. return Canvas(surface);
  50. }
  51. void Canvas::applyTransparency(bool on)
  52. {
  53. if (on)
  54. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  55. else
  56. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
  57. }
  58. void Canvas::applyGrayscale()
  59. {
  60. CSDL_Ext::convertToGrayscale(surface, renderArea);
  61. }
  62. Canvas::~Canvas()
  63. {
  64. SDL_FreeSurface(surface);
  65. }
  66. void Canvas::draw(const std::shared_ptr<IImage>& image, const Point & pos)
  67. {
  68. assert(image);
  69. if (image)
  70. image->draw(surface, renderArea.x + pos.x, renderArea.y + pos.y);
  71. }
  72. void Canvas::draw(const std::shared_ptr<IImage>& image, const Point & pos, const Rect & sourceRect)
  73. {
  74. assert(image);
  75. if (image)
  76. image->draw(surface, renderArea.x + pos.x, renderArea.y + pos.y, &sourceRect);
  77. }
  78. void Canvas::draw(const Canvas & image, const Point & pos)
  79. {
  80. CSDL_Ext::blitSurface(image.surface, image.renderArea, surface, renderArea.topLeft() + pos);
  81. }
  82. void Canvas::drawTransparent(const Canvas & image, const Point & pos, double transparency)
  83. {
  84. SDL_BlendMode oldMode;
  85. SDL_GetSurfaceBlendMode(image.surface, &oldMode);
  86. SDL_SetSurfaceBlendMode(image.surface, SDL_BLENDMODE_BLEND);
  87. SDL_SetSurfaceAlphaMod(image.surface, 255 * transparency);
  88. CSDL_Ext::blitSurface(image.surface, image.renderArea, surface, renderArea.topLeft() + pos);
  89. SDL_SetSurfaceAlphaMod(image.surface, 255);
  90. SDL_SetSurfaceBlendMode(image.surface, oldMode);
  91. }
  92. void Canvas::drawScaled(const Canvas & image, const Point & pos, const Point & targetSize)
  93. {
  94. SDL_Rect targetRect = CSDL_Ext::toSDL(Rect(pos + renderArea.topLeft(), targetSize));
  95. SDL_BlitScaled(image.surface, nullptr, surface, &targetRect);
  96. }
  97. void Canvas::drawPoint(const Point & dest, const ColorRGBA & color)
  98. {
  99. CSDL_Ext::putPixelWithoutRefreshIfInSurf(surface, dest.x, dest.y, color.r, color.g, color.b, color.a);
  100. }
  101. void Canvas::drawLine(const Point & from, const Point & dest, const ColorRGBA & colorFrom, const ColorRGBA & colorDest)
  102. {
  103. CSDL_Ext::drawLine(surface, renderArea.topLeft() + from, renderArea.topLeft() + dest, CSDL_Ext::toSDL(colorFrom), CSDL_Ext::toSDL(colorDest));
  104. }
  105. void Canvas::drawLineDashed(const Point & from, const Point & dest, const ColorRGBA & color)
  106. {
  107. CSDL_Ext::drawLineDashed(surface, renderArea.topLeft() + from, renderArea.topLeft() + dest, CSDL_Ext::toSDL(color));
  108. }
  109. void Canvas::drawBorder(const Rect & target, const SDL_Color & color, int width)
  110. {
  111. Rect realTarget = target + renderArea.topLeft();
  112. CSDL_Ext::drawBorder(surface, realTarget.x, realTarget.y, realTarget.w, realTarget.h, color, width);
  113. }
  114. void Canvas::drawBorderDashed(const Rect & target, const ColorRGBA & color)
  115. {
  116. Rect realTarget = target + renderArea.topLeft();
  117. CSDL_Ext::drawLineDashed(surface, realTarget.topLeft(), realTarget.topRight(), CSDL_Ext::toSDL(color));
  118. CSDL_Ext::drawLineDashed(surface, realTarget.bottomLeft(), realTarget.bottomRight(), CSDL_Ext::toSDL(color));
  119. CSDL_Ext::drawLineDashed(surface, realTarget.topLeft(), realTarget.bottomLeft(), CSDL_Ext::toSDL(color));
  120. CSDL_Ext::drawLineDashed(surface, realTarget.topRight(), realTarget.bottomRight(), CSDL_Ext::toSDL(color));
  121. }
  122. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text )
  123. {
  124. switch (alignment)
  125. {
  126. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLeft (surface, text, colorDest, renderArea.topLeft() + position);
  127. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, renderArea.topLeft() + position);
  128. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextRight (surface, text, colorDest, renderArea.topLeft() + position);
  129. }
  130. }
  131. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
  132. {
  133. switch (alignment)
  134. {
  135. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLinesLeft (surface, text, colorDest, renderArea.topLeft() + position);
  136. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, renderArea.topLeft() + position);
  137. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextLinesRight (surface, text, colorDest, renderArea.topLeft() + position);
  138. }
  139. }
  140. void Canvas::drawColor(const Rect & target, const SDL_Color & color)
  141. {
  142. Rect realTarget = target + renderArea.topLeft();
  143. CSDL_Ext::fillRect(surface, realTarget, color);
  144. }
  145. SDL_Surface * Canvas::getInternalSurface()
  146. {
  147. return surface;
  148. }