SDLImage.h 3.4 KB

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