Canvas.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "IImage.h"
  14. #include "Graphics.h"
  15. #include <SDL_surface.h>
  16. Canvas::Canvas(SDL_Surface * surface):
  17. surface(surface),
  18. renderOffset(0,0)
  19. {
  20. surface->refcount++;
  21. }
  22. Canvas::Canvas(const Canvas & other):
  23. surface(other.surface),
  24. renderOffset(other.renderOffset)
  25. {
  26. surface->refcount++;
  27. }
  28. Canvas::Canvas(const Canvas & other, const Rect & newClipRect):
  29. Canvas(other)
  30. {
  31. clipRect.emplace();
  32. CSDL_Ext::getClipRect(surface, clipRect.get());
  33. Rect currClipRect = newClipRect + renderOffset;
  34. CSDL_Ext::setClipRect(surface, currClipRect);
  35. renderOffset += newClipRect.topLeft();
  36. }
  37. Canvas::Canvas(const Point & size):
  38. renderOffset(0,0),
  39. surface(CSDL_Ext::newSurface(size.x, size.y))
  40. {
  41. }
  42. Canvas::~Canvas()
  43. {
  44. if (clipRect)
  45. CSDL_Ext::setClipRect(surface, clipRect.get());
  46. SDL_FreeSurface(surface);
  47. }
  48. void Canvas::draw(std::shared_ptr<IImage> image, const Point & pos)
  49. {
  50. assert(image);
  51. if (image)
  52. image->draw(surface, renderOffset.x + pos.x, renderOffset.y + pos.y);
  53. }
  54. void Canvas::draw(std::shared_ptr<IImage> image, const Point & pos, const Rect & sourceRect)
  55. {
  56. assert(image);
  57. if (image)
  58. image->draw(surface, renderOffset.x + pos.x, renderOffset.y + pos.y, &sourceRect);
  59. }
  60. void Canvas::draw(Canvas & image, const Point & pos)
  61. {
  62. CSDL_Ext::blitAt(image.surface, renderOffset.x + pos.x, renderOffset.y + pos.y, surface);
  63. }
  64. void Canvas::draw(Canvas & image, const Point & pos, const Point & targetSize)
  65. {
  66. SDL_Rect targetRect = CSDL_Ext::toSDL(Rect(pos, targetSize));
  67. SDL_BlitScaled(image.surface, nullptr, surface, &targetRect );
  68. }
  69. void Canvas::drawPoint(const Point & dest, const ColorRGBA & color)
  70. {
  71. CSDL_Ext::putPixelWithoutRefreshIfInSurf(surface, dest.x, dest.y, color.r, color.g, color.b, color.a);
  72. }
  73. void Canvas::drawLine(const Point & from, const Point & dest, const ColorRGBA & colorFrom, const ColorRGBA & colorDest)
  74. {
  75. CSDL_Ext::drawLine(surface, renderOffset + from, renderOffset + dest, CSDL_Ext::toSDL(colorFrom), CSDL_Ext::toSDL(colorDest));
  76. }
  77. void Canvas::drawLineDashed(const Point & from, const Point & dest, const ColorRGBA & color)
  78. {
  79. CSDL_Ext::drawLineDashed(surface, renderOffset + from, renderOffset + dest, CSDL_Ext::toSDL(color));
  80. }
  81. void Canvas::drawBorderDashed(const Rect & target, const ColorRGBA & color)
  82. {
  83. Rect realTarget = target + renderOffset;
  84. CSDL_Ext::drawLineDashed(surface, realTarget.topLeft(), realTarget.topRight(), CSDL_Ext::toSDL(color));
  85. CSDL_Ext::drawLineDashed(surface, realTarget.bottomLeft(), realTarget.bottomRight(), CSDL_Ext::toSDL(color));
  86. CSDL_Ext::drawLineDashed(surface, realTarget.topLeft(), realTarget.bottomLeft(), CSDL_Ext::toSDL(color));
  87. CSDL_Ext::drawLineDashed(surface, realTarget.topRight(), realTarget.bottomRight(), CSDL_Ext::toSDL(color));
  88. }
  89. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text )
  90. {
  91. switch (alignment)
  92. {
  93. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLeft (surface, text, colorDest, renderOffset + position);
  94. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, renderOffset + position);
  95. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextRight (surface, text, colorDest, renderOffset + position);
  96. }
  97. }
  98. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
  99. {
  100. switch (alignment)
  101. {
  102. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLinesLeft (surface, text, colorDest, renderOffset + position);
  103. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, renderOffset + position);
  104. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextLinesRight (surface, text, colorDest, renderOffset + position);
  105. }
  106. }