SDLImage.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 SDLImageConst final : public IConstImage, public std::enable_shared_from_this<SDLImageConst>, boost::noncopyable
  23. {
  24. static constexpr int DEFAULT_PALETTE_COLORS = 256;
  25. //Surface without empty borders
  26. SDL_Surface * surf;
  27. SDL_Palette * originalPalette;
  28. //size of left and top borders
  29. Point margins;
  30. //total size including borders
  31. Point fullSize;
  32. // Keep the original palette, in order to do color switching operation
  33. void savePalette();
  34. public:
  35. //Load image from def file
  36. SDLImageConst(CDefFile *data, size_t frame, size_t group=0);
  37. //Load from bitmap file
  38. SDLImageConst(const ImagePath & filename, EImageBlitMode blitMode);
  39. //Create using existing surface, extraRef will increase refcount on SDL_Surface
  40. SDLImageConst(SDL_Surface * from, EImageBlitMode blitMode);
  41. ~SDLImageConst();
  42. void draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, uint8_t alpha, EImageBlitMode mode) const;
  43. void exportBitmap(const boost::filesystem::path & path) const override;
  44. Point dimensions() const override;
  45. bool isTransparent(const Point & coords) const override;
  46. std::shared_ptr<IImage> createImageReference() override;
  47. std::shared_ptr<SDLImageConst> horizontalFlip() const;
  48. std::shared_ptr<SDLImageConst> verticalFlip() const;
  49. std::shared_ptr<SDLImageConst> scaleFast(const Point & size) const;
  50. const SDL_Palette * getPalette() const;
  51. friend class SDLImageLoader;
  52. };
  53. class SDLImageBase : public IImage, boost::noncopyable
  54. {
  55. protected:
  56. std::shared_ptr<SDLImageConst> image;
  57. uint8_t alphaValue;
  58. EImageBlitMode blitMode;
  59. public:
  60. SDLImageBase(const std::shared_ptr<SDLImageConst> & image);
  61. void scaleFast(const Point & size) override;
  62. void exportBitmap(const boost::filesystem::path & path) const override;
  63. bool isTransparent(const Point & coords) const override;
  64. Point dimensions() const override;
  65. void setAlpha(uint8_t value) override;
  66. void setBlitMode(EImageBlitMode mode) override;
  67. void horizontalFlip() override;
  68. void verticalFlip() override;
  69. };
  70. class SDLImageIndexed final : public SDLImageBase
  71. {
  72. SDL_Palette * currentPalette = nullptr;
  73. public:
  74. SDLImageIndexed(const std::shared_ptr<SDLImageConst> & image);
  75. ~SDLImageIndexed();
  76. void draw(SDL_Surface * where, const Point & pos, const Rect * src) const override;
  77. void setSpecialPallete(const SpecialPalette & SpecialPalette, uint32_t colorsToSkipMask) override;
  78. void playerColored(PlayerColor player) override;
  79. void setFlagColor(PlayerColor player) override;
  80. void shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove) override;
  81. void adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask) override;
  82. };
  83. class SDLImageRGB final : public SDLImageBase
  84. {
  85. public:
  86. using SDLImageBase::SDLImageBase;
  87. void draw(SDL_Surface * where, const Point & pos, const Rect * src) const override;
  88. void playerColored(PlayerColor player) override;
  89. void setFlagColor(PlayerColor player) override;
  90. void shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove) override;
  91. void adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask) override;
  92. };