CanvasImage.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * CanvasImage.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 "CanvasImage.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "../render/IScreenHandler.h"
  14. #include "../renderSDL/SDL_Extensions.h"
  15. #include <SDL_image.h>
  16. #include <SDL_surface.h>
  17. CanvasImage::CanvasImage(const Point & size, CanvasScalingPolicy scalingPolicy)
  18. : surface(CSDL_Ext::newSurface(scalingPolicy == CanvasScalingPolicy::IGNORE ? size : (size * GH.screenHandler().getScalingFactor())))
  19. , scalingPolicy(scalingPolicy)
  20. {
  21. }
  22. void CanvasImage::draw(SDL_Surface * where, const Point & pos, const Rect * src, int scalingFactor) const
  23. {
  24. if(src)
  25. CSDL_Ext::blitSurface(surface, *src, where, pos);
  26. else
  27. CSDL_Ext::blitSurface(surface, where, pos);
  28. }
  29. void CanvasImage::scaleTo(const Point & size, EScalingAlgorithm algorithm)
  30. {
  31. Point scaledSize = size * GH.screenHandler().getScalingFactor();
  32. auto newSurface = CSDL_Ext::scaleSurface(surface, scaledSize.x, scaledSize.y, algorithm);
  33. SDL_FreeSurface(surface);
  34. surface = newSurface;
  35. }
  36. void CanvasImage::exportBitmap(const boost::filesystem::path & path) const
  37. {
  38. IMG_SavePNG(surface, path.string().c_str());
  39. }
  40. Canvas CanvasImage::getCanvas()
  41. {
  42. return Canvas::createFromSurface(surface, scalingPolicy);
  43. }
  44. Rect CanvasImage::contentRect() const
  45. {
  46. return Rect(Point(0, 0), dimensions());
  47. }
  48. Point CanvasImage::dimensions() const
  49. {
  50. return {surface->w, surface->h};
  51. }