Canvas.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. {
  19. surface->refcount++;
  20. }
  21. Canvas::Canvas(Canvas & other):
  22. surface(other.surface)
  23. {
  24. surface->refcount++;
  25. }
  26. Canvas::Canvas(const Point & size)
  27. {
  28. surface = CSDL_Ext::newSurface(size.x, size.y);
  29. }
  30. Canvas::~Canvas()
  31. {
  32. SDL_FreeSurface(surface);
  33. }
  34. void Canvas::draw(std::shared_ptr<IImage> image, const Point & pos)
  35. {
  36. assert(image);
  37. if (image)
  38. image->draw(surface, pos.x, pos.y);
  39. }
  40. void Canvas::draw(std::shared_ptr<IImage> image, const Point & pos, const Rect & sourceRect)
  41. {
  42. assert(image);
  43. if (image)
  44. image->draw(surface, pos.x, pos.y, &sourceRect);
  45. }
  46. void Canvas::draw(std::shared_ptr<IImage> image, const Point & pos, const Rect & sourceRect, uint8_t alpha)
  47. {
  48. assert(image);
  49. if (image)
  50. image->draw(surface, pos.x, pos.y, &sourceRect, alpha);
  51. }
  52. void Canvas::draw(Canvas & image, const Point & pos)
  53. {
  54. blitAt(image.surface, pos.x, pos.y, surface);
  55. }
  56. void Canvas::drawLine(const Point & from, const Point & dest, const SDL_Color & colorFrom, const SDL_Color & colorDest)
  57. {
  58. CSDL_Ext::drawLine(surface, from.x, from.y, dest.x, dest.y, colorFrom, colorDest);
  59. }
  60. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text )
  61. {
  62. switch (alignment)
  63. {
  64. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLeft (surface, text, colorDest, position);
  65. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, position);
  66. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextRight (surface, text, colorDest, position);
  67. }
  68. }
  69. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
  70. {
  71. switch (alignment)
  72. {
  73. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLinesLeft (surface, text, colorDest, position);
  74. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, position);
  75. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextLinesRight (surface, text, colorDest, position);
  76. }
  77. }