SDLImage.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * SDLImage.h, 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. #pragma once
  11. #include "../render/IImage.h"
  12. #include "../../lib/Point.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class JsonNode;
  15. VCMI_LIB_NAMESPACE_END
  16. class CDefFile;
  17. struct SDL_Surface;
  18. struct SDL_Palette;
  19. /*
  20. * Wrapper around SDL_Surface
  21. */
  22. class SDLImageShared final : public ISharedImage, public std::enable_shared_from_this<SDLImageShared>, boost::noncopyable
  23. {
  24. //Surface without empty borders
  25. SDL_Surface * surf = nullptr;
  26. SDL_Palette * originalPalette = nullptr;
  27. //size of left and top borders
  28. Point margins;
  29. //total size including borders
  30. Point fullSize;
  31. std::atomic_bool upscalingInProgress = false;
  32. bool asyncUpscale = true;
  33. // Keep the original palette, in order to do color switching operation
  34. void savePalette();
  35. void optimizeSurface();
  36. public:
  37. //Load image from def file
  38. SDLImageShared(const CDefFile *data, size_t frame, size_t group=0);
  39. //Load from bitmap file
  40. SDLImageShared(const ImagePath & filename, bool optimizeImage=true);
  41. //Create using existing surface, extraRef will increase refcount on SDL_Surface
  42. SDLImageShared(SDL_Surface * from);
  43. ~SDLImageShared();
  44. /// Creates image at specified scaling factor from source image
  45. static std::shared_ptr<SDLImageShared> createScaled(const SDLImageShared * from, int integerScaleFactor, EScalingAlgorithm algorithm);
  46. void scaledDraw(SDL_Surface * where, SDL_Palette * palette, const Point & scaling, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const override;
  47. void draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const override;
  48. void exportBitmap(const boost::filesystem::path & path, SDL_Palette * palette) const override;
  49. Point dimensions() const override;
  50. bool isTransparent(const Point & coords) const override;
  51. Rect contentRect() const override;
  52. bool isLoading() const override;
  53. void setAsyncUpscale(bool on) override;
  54. bool getAsyncUpscale() const override;
  55. const SDL_Palette * getPalette() const override;
  56. [[nodiscard]] std::shared_ptr<const ISharedImage> horizontalFlip() const override;
  57. [[nodiscard]] std::shared_ptr<const ISharedImage> verticalFlip() const override;
  58. [[nodiscard]] std::shared_ptr<const ISharedImage> scaleInteger(int factor, SDL_Palette * palette, EImageBlitMode blitMode) const override;
  59. [[nodiscard]] std::shared_ptr<const ISharedImage> scaleTo(const Point & size, SDL_Palette * palette) const override;
  60. std::shared_ptr<SDLImageShared> drawShadow(bool doSheer) const;
  61. std::shared_ptr<SDLImageShared> drawOutline(const ColorRGBA & color, int thickness) const;
  62. friend class SDLImageLoader;
  63. };