Canvas.cpp 5.1 KB

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