CCanvas.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * CCanvas.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 "CCanvas.h"
  12. #include "SDL_Extensions.h"
  13. #include "Geometries.h"
  14. #include "CAnimation.h"
  15. #include "../Graphics.h"
  16. CCanvas::CCanvas(SDL_Surface * surface):
  17. surface(surface)
  18. {
  19. surface->refcount++;
  20. }
  21. CCanvas::CCanvas(const Point & size)
  22. {
  23. surface = CSDL_Ext::newSurface(size.x, size.y);
  24. }
  25. CCanvas::~CCanvas()
  26. {
  27. SDL_FreeSurface(surface);
  28. }
  29. void CCanvas::draw(std::shared_ptr<IImage> image, const Point & pos)
  30. {
  31. image->draw(surface, pos.x, pos.y);
  32. }
  33. void CCanvas::draw(std::shared_ptr<IImage> image, const Point & pos, const Rect & sourceRect)
  34. {
  35. image->draw(surface, pos.x, pos.y, &sourceRect);
  36. }
  37. void CCanvas::draw(std::shared_ptr<CCanvas> image, const Point & pos)
  38. {
  39. blitAt(image->surface, pos.x, pos.y, surface);
  40. }
  41. void CCanvas::drawLine(const Point & from, const Point & dest, const SDL_Color & colorFrom, const SDL_Color & colorDest)
  42. {
  43. CSDL_Ext::drawLine(surface, from.x, from.y, dest.x, dest.y, colorFrom, colorDest);
  44. }
  45. void CCanvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text )
  46. {
  47. switch (alignment)
  48. {
  49. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLeft (surface, text, colorDest, position);
  50. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, position);
  51. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextRight (surface, text, colorDest, position);
  52. }
  53. }
  54. void CCanvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
  55. {
  56. switch (alignment)
  57. {
  58. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLinesLeft (surface, text, colorDest, position);
  59. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, position);
  60. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextLinesRight (surface, text, colorDest, position);
  61. }
  62. }
  63. SDL_Surface * CCanvas::getSurface()
  64. {
  65. return surface;
  66. }