Canvas.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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(Canvas & image, const Point & pos)
  47. {
  48. blitAt(image.surface, pos.x, pos.y, surface);
  49. }
  50. void Canvas::drawLine(const Point & from, const Point & dest, const SDL_Color & colorFrom, const SDL_Color & colorDest)
  51. {
  52. CSDL_Ext::drawLine(surface, from.x, from.y, dest.x, dest.y, colorFrom, colorDest);
  53. }
  54. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text )
  55. {
  56. switch (alignment)
  57. {
  58. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLeft (surface, text, colorDest, position);
  59. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, position);
  60. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextRight (surface, text, colorDest, position);
  61. }
  62. }
  63. void Canvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
  64. {
  65. switch (alignment)
  66. {
  67. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLinesLeft (surface, text, colorDest, position);
  68. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, position);
  69. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextLinesRight (surface, text, colorDest, position);
  70. }
  71. }