SDLImage.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <SDL_surface.h>
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class JsonNode;
  16. VCMI_LIB_NAMESPACE_END
  17. class CDefFile;
  18. struct SDL_Surface;
  19. struct SDL_Palette;
  20. /*
  21. * Wrapper around SDL_Surface
  22. */
  23. class SDLImageShared : public ISharedImage, public std::enable_shared_from_this<SDLImageShared>, boost::noncopyable
  24. {
  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. //pre scaled image
  33. int preScaleFactor;
  34. // Keep the original palette, in order to do color switching operation
  35. void savePalette();
  36. void optimizeSurface();
  37. public:
  38. //Load image from def file
  39. SDLImageShared(const CDefFile *data, size_t frame, size_t group=0, int preScaleFactor=1);
  40. //Load from bitmap file
  41. SDLImageShared(const ImagePath & filename, int preScaleFactor=1);
  42. //Create using existing surface, extraRef will increase refcount on SDL_Surface
  43. SDLImageShared(SDL_Surface * from, int preScaleFactor=1);
  44. ~SDLImageShared();
  45. void draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const override;
  46. void exportBitmap(const boost::filesystem::path & path, SDL_Palette * palette) const override;
  47. Point dimensions() const override;
  48. bool isTransparent(const Point & coords) const override;
  49. std::shared_ptr<IImage> createImageReference(EImageBlitMode mode) const override;
  50. std::shared_ptr<const ISharedImage> horizontalFlip() const override;
  51. std::shared_ptr<const ISharedImage> verticalFlip() const override;
  52. std::shared_ptr<const ISharedImage> scaleInteger(int factor, SDL_Palette * palette, EImageBlitMode blitMode) const override;
  53. std::shared_ptr<const ISharedImage> scaleTo(const Point & size, SDL_Palette * palette) const override;
  54. friend class SDLImageLoader;
  55. };
  56. class SDLEmptyImageShared : public SDLImageShared
  57. {
  58. public:
  59. SDLEmptyImageShared() : SDLImageShared(createDefaultSurface().get()) {}
  60. private:
  61. std::shared_ptr<SDL_Surface> createDefaultSurface()
  62. {
  63. auto surface = SDL_CreateRGBSurface(0, 32, 32, 32, 0, 0, 0, 0);
  64. SDL_FillRect(surface, nullptr, SDL_MapRGB(surface->format, 255, 0, 0));
  65. return std::shared_ptr<SDL_Surface>(surface, SDL_FreeSurface);
  66. }
  67. };
  68. class SDLImageBase : public IImage, boost::noncopyable
  69. {
  70. protected:
  71. std::shared_ptr<const ISharedImage> image;
  72. uint8_t alphaValue;
  73. EImageBlitMode blitMode;
  74. public:
  75. SDLImageBase(const std::shared_ptr<const ISharedImage> & image, EImageBlitMode mode);
  76. bool isTransparent(const Point & coords) const override;
  77. Point dimensions() const override;
  78. void setAlpha(uint8_t value) override;
  79. void setBlitMode(EImageBlitMode mode) override;
  80. std::shared_ptr<const ISharedImage> getSharedImage() const override;
  81. };
  82. class SDLImageIndexed final : public SDLImageBase
  83. {
  84. SDL_Palette * currentPalette = nullptr;
  85. SDL_Palette * originalPalette = nullptr;
  86. void setShadowTransparency(float factor);
  87. void preparePalette();
  88. public:
  89. SDLImageIndexed(const std::shared_ptr<const ISharedImage> & image, SDL_Palette * palette, EImageBlitMode mode);
  90. ~SDLImageIndexed();
  91. void draw(SDL_Surface * where, const Point & pos, const Rect * src) const override;
  92. void setOverlayColor(const ColorRGBA & color) override;
  93. void playerColored(PlayerColor player) override;
  94. void shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove) override;
  95. void adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask) override;
  96. void scaleInteger(int factor) override;
  97. void scaleTo(const Point & size) override;
  98. void exportBitmap(const boost::filesystem::path & path) const override;
  99. };
  100. class SDLImageRGB final : public SDLImageBase
  101. {
  102. public:
  103. using SDLImageBase::SDLImageBase;
  104. void draw(SDL_Surface * where, const Point & pos, const Rect * src) const override;
  105. void setOverlayColor(const ColorRGBA & color) override;
  106. void playerColored(PlayerColor player) override;
  107. void shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove) override;
  108. void adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask) override;
  109. void scaleInteger(int factor) override;
  110. void scaleTo(const Point & size) override;
  111. void exportBitmap(const boost::filesystem::path & path) const override;
  112. };