Canvas.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "SDL_Extensions.h"
  13. #include "Geometries.h"
  14. #include "CAnimation.h"
  15. #include "../Graphics.h"
  16. Canvas::Canvas(SDL_Surface * surface):
  17. surface(surface),
  18. renderOffset(0,0)
  19. {
  20. surface->refcount++;
  21. }
  22. Canvas::Canvas(Canvas & other):
  23. surface(other.surface),
  24. renderOffset(other.renderOffset)
  25. {
  26. surface->refcount++;
  27. }
  28. Canvas::Canvas(Canvas & other, const Rect & newClipRect):
  29. Canvas(other)
  30. {
  31. clipRect.emplace();
  32. SDL_GetClipRect(surface, clipRect.get_ptr());
  33. Rect currClipRect = newClipRect + renderOffset;
  34. SDL_SetClipRect(surface, &currClipRect);
  35. renderOffset += newClipRect.topLeft();
  36. }
  37. Canvas::Canvas(const Point & size):
  38. renderOffset(0,0)
  39. {
  40. surface = CSDL_Ext::newSurface(size.x, size.y);
  41. }
  42. Canvas::~Canvas()
  43. {
  44. if (clipRect)
  45. SDL_SetClipRect(surface, clipRect.get_ptr());
  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. blitAt(image.surface, renderOffset.x + pos.x, renderOffset.y + pos.y, surface);
  63. }
  64. void Canvas::drawLine(const Point & from, const Point & dest, const SDL_Color & colorFrom, const SDL_Color & colorDest)
  65. {
  66. CSDL_Ext::drawLine(surface, renderOffset.x + from.x, renderOffset.y + from.y, renderOffset.x + dest.x, renderOffset.y + dest.y, colorFrom, colorDest);
  67. }
  68. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text )
  69. {
  70. switch (alignment)
  71. {
  72. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLeft (surface, text, colorDest, renderOffset + position);
  73. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, renderOffset + position);
  74. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextRight (surface, text, colorDest, renderOffset + position);
  75. }
  76. }
  77. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
  78. {
  79. switch (alignment)
  80. {
  81. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLinesLeft (surface, text, colorDest, renderOffset + position);
  82. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, renderOffset + position);
  83. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextLinesRight (surface, text, colorDest, renderOffset + position);
  84. }
  85. }