CCanvas.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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<CCanvas> image, const Point & pos)
  34. {
  35. blitAt(image->surface, pos.x, pos.y, surface);
  36. }
  37. void CCanvas::drawLine(const Point & from, const Point & dest, const SDL_Color & colorFrom, const SDL_Color & colorDest)
  38. {
  39. CSDL_Ext::drawLine(surface, from.x, from.y, dest.x, dest.y, colorFrom, colorDest);
  40. }
  41. void CCanvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::string & text )
  42. {
  43. switch (alignment)
  44. {
  45. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLeft (surface, text, colorDest, position);
  46. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextCenter(surface, text, colorDest, position);
  47. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextRight (surface, text, colorDest, position);
  48. }
  49. }
  50. void CCanvas::drawText(const Point & position, const EFonts & font, const SDL_Color & colorDest, ETextAlignment alignment, const std::vector<std::string> & text )
  51. {
  52. switch (alignment)
  53. {
  54. case ETextAlignment::TOPLEFT: return graphics->fonts[font]->renderTextLinesLeft (surface, text, colorDest, position);
  55. case ETextAlignment::CENTER: return graphics->fonts[font]->renderTextLinesCenter(surface, text, colorDest, position);
  56. case ETextAlignment::BOTTOMRIGHT: return graphics->fonts[font]->renderTextLinesRight (surface, text, colorDest, position);
  57. }
  58. }
  59. SDL_Surface * CCanvas::getSurface()
  60. {
  61. return surface;
  62. }