Browse Source

ISharedImage is now always const, remove creation of image copy when
upscaling to same factor

Ivan Savenko 11 months ago
parent
commit
7f0cb6ce6a

+ 6 - 6
client/render/IImage.h

@@ -78,7 +78,7 @@ public:
 	virtual void setShadowEnabled(bool on) = 0;
 	virtual void setBodyEnabled(bool on) = 0;
 	virtual void setOverlayEnabled(bool on) = 0;
-	virtual std::shared_ptr<ISharedImage> getSharedImage() const = 0;
+	virtual std::shared_ptr<const ISharedImage> getSharedImage() const = 0;
 
 	virtual ~IImage() = default;
 };
@@ -94,12 +94,12 @@ public:
 	virtual bool isTransparent(const Point & coords) const = 0;
 	virtual void draw(SDL_Surface * where, SDL_Palette * palette, const Point & dest, const Rect * src, const ColorRGBA & colorMultiplier, uint8_t alpha, EImageBlitMode mode) const = 0;
 
-	virtual std::shared_ptr<IImage> createImageReference(EImageBlitMode mode) = 0;
+	virtual std::shared_ptr<IImage> createImageReference(EImageBlitMode mode) const = 0;
 
-	virtual std::shared_ptr<ISharedImage> horizontalFlip() const = 0;
-	virtual std::shared_ptr<ISharedImage> verticalFlip() const = 0;
-	virtual std::shared_ptr<ISharedImage> scaleInteger(int factor, SDL_Palette * palette) const = 0;
-	virtual std::shared_ptr<ISharedImage> scaleTo(const Point & size, SDL_Palette * palette) const = 0;
+	virtual std::shared_ptr<const ISharedImage> horizontalFlip() const = 0;
+	virtual std::shared_ptr<const ISharedImage> verticalFlip() const = 0;
+	virtual std::shared_ptr<const ISharedImage> scaleInteger(int factor, SDL_Palette * palette) const = 0;
+	virtual std::shared_ptr<const ISharedImage> scaleTo(const Point & size, SDL_Palette * palette) const = 0;
 
 
 	virtual ~ISharedImage() = default;

+ 2 - 2
client/renderSDL/ImageScaled.cpp

@@ -21,7 +21,7 @@
 
 #include <SDL_surface.h>
 
-ImageScaled::ImageScaled(const ImageLocator & inputLocator, const std::shared_ptr<ISharedImage> & source, EImageBlitMode mode)
+ImageScaled::ImageScaled(const ImageLocator & inputLocator, const std::shared_ptr<const ISharedImage> & source, EImageBlitMode mode)
 	: source(source)
 	, locator(inputLocator)
 	, colorMultiplier(Colors::WHITE_TRUE)
@@ -33,7 +33,7 @@ ImageScaled::ImageScaled(const ImageLocator & inputLocator, const std::shared_pt
 		setShadowEnabled(true);
 }
 
-std::shared_ptr<ISharedImage> ImageScaled::getSharedImage() const
+std::shared_ptr<const ISharedImage> ImageScaled::getSharedImage() const
 {
 	return body;
 }

+ 6 - 6
client/renderSDL/ImageScaled.h

@@ -25,16 +25,16 @@ class ImageScaled final : public IImage
 private:
 
 	/// Original unscaled image
-	std::shared_ptr<ISharedImage> source;
+	std::shared_ptr<const ISharedImage> source;
 
 	/// Upscaled shadow of our image, may be null
-	std::shared_ptr<ISharedImage> shadow;
+	std::shared_ptr<const ISharedImage> shadow;
 
 	/// Upscaled main part of our image, may be null
-	std::shared_ptr<ISharedImage> body;
+	std::shared_ptr<const ISharedImage> body;
 
 	/// Upscaled overlay (player color, selection highlight) of our image, may be null
-	std::shared_ptr<ISharedImage> overlay;
+	std::shared_ptr<const ISharedImage> overlay;
 
 	ImageLocator locator;
 
@@ -45,7 +45,7 @@ private:
 	EImageBlitMode blitMode;
 
 public:
-	ImageScaled(const ImageLocator & locator, const std::shared_ptr<ISharedImage> & source, EImageBlitMode mode);
+	ImageScaled(const ImageLocator & locator, const std::shared_ptr<const ISharedImage> & source, EImageBlitMode mode);
 
 	void scaleInteger(int factor) override;
 	void scaleTo(const Point & size) override;
@@ -63,5 +63,5 @@ public:
 	void setShadowEnabled(bool on) override;
 	void setBodyEnabled(bool on) override;
 	void setOverlayEnabled(bool on) override;
-	std::shared_ptr<ISharedImage> getSharedImage() const override;
+	std::shared_ptr<const ISharedImage> getSharedImage() const override;
 };

+ 6 - 6
client/renderSDL/RenderHandler.cpp

@@ -212,7 +212,7 @@ ImageLocator RenderHandler::getLocatorForAnimationFrame(const AnimationPath & pa
 	return ImageLocator(path, frame, group);
 }
 
-std::shared_ptr<ISharedImage> RenderHandler::loadImageImpl(const ImageLocator & locator)
+std::shared_ptr<const ISharedImage> RenderHandler::loadImageImpl(const ImageLocator & locator)
 {
 	auto it = imageFiles.find(locator);
 	if (it != imageFiles.end())
@@ -231,7 +231,7 @@ std::shared_ptr<ISharedImage> RenderHandler::loadImageImpl(const ImageLocator &
 	return scaledImage;
 }
 
-std::shared_ptr<ISharedImage> RenderHandler::loadImageFromFileUncached(const ImageLocator & locator)
+std::shared_ptr<const ISharedImage> RenderHandler::loadImageFromFileUncached(const ImageLocator & locator)
 {
 	if (locator.image)
 	{
@@ -258,7 +258,7 @@ std::shared_ptr<ISharedImage> RenderHandler::loadImageFromFileUncached(const Ima
 	throw std::runtime_error("Invalid image locator received!");
 }
 
-void RenderHandler::storeCachedImage(const ImageLocator & locator, std::shared_ptr<ISharedImage> image)
+void RenderHandler::storeCachedImage(const ImageLocator & locator, std::shared_ptr<const ISharedImage> image)
 {
 	imageFiles[locator] = image;
 
@@ -271,7 +271,7 @@ void RenderHandler::storeCachedImage(const ImageLocator & locator, std::shared_p
 #endif
 }
 
-std::shared_ptr<ISharedImage> RenderHandler::loadImageFromFile(const ImageLocator & locator)
+std::shared_ptr<const ISharedImage> RenderHandler::loadImageFromFile(const ImageLocator & locator)
 {
 	if (imageFiles.count(locator))
 		return imageFiles.at(locator);
@@ -281,7 +281,7 @@ std::shared_ptr<ISharedImage> RenderHandler::loadImageFromFile(const ImageLocato
 	return result;
 }
 
-std::shared_ptr<ISharedImage> RenderHandler::transformImage(const ImageLocator & locator, std::shared_ptr<ISharedImage> image)
+std::shared_ptr<const ISharedImage> RenderHandler::transformImage(const ImageLocator & locator, std::shared_ptr<const ISharedImage> image)
 {
 	if (imageFiles.count(locator))
 		return imageFiles.at(locator);
@@ -298,7 +298,7 @@ std::shared_ptr<ISharedImage> RenderHandler::transformImage(const ImageLocator &
 	return result;
 }
 
-std::shared_ptr<ISharedImage> RenderHandler::scaleImage(const ImageLocator & locator, std::shared_ptr<ISharedImage> image)
+std::shared_ptr<const ISharedImage> RenderHandler::scaleImage(const ImageLocator & locator, std::shared_ptr<const ISharedImage> image)
 {
 	if (imageFiles.count(locator))
 		return imageFiles.at(locator);

+ 7 - 7
client/renderSDL/RenderHandler.h

@@ -25,7 +25,7 @@ class RenderHandler : public IRenderHandler
 
 	std::map<AnimationPath, std::shared_ptr<CDefFile>> animationFiles;
 	std::map<AnimationPath, AnimationLayoutMap> animationLayouts;
-	std::map<ImageLocator, std::shared_ptr<ISharedImage>> imageFiles;
+	std::map<ImageLocator, std::shared_ptr<const ISharedImage>> imageFiles;
 	std::map<EFonts, std::shared_ptr<const IFont>> fonts;
 
 	std::shared_ptr<CDefFile> getAnimationFile(const AnimationPath & path);
@@ -36,15 +36,15 @@ class RenderHandler : public IRenderHandler
 
 	void addImageListEntry(size_t index, size_t group, const std::string & listName, const std::string & imageName);
 	void addImageListEntries(const EntityService * service);
-	void storeCachedImage(const ImageLocator & locator, std::shared_ptr<ISharedImage> image);
+	void storeCachedImage(const ImageLocator & locator, std::shared_ptr<const ISharedImage> image);
 
-	std::shared_ptr<ISharedImage> loadImageImpl(const ImageLocator & config);
+	std::shared_ptr<const ISharedImage> loadImageImpl(const ImageLocator & config);
 
-	std::shared_ptr<ISharedImage> loadImageFromFileUncached(const ImageLocator & locator);
-	std::shared_ptr<ISharedImage> loadImageFromFile(const ImageLocator & locator);
+	std::shared_ptr<const ISharedImage> loadImageFromFileUncached(const ImageLocator & locator);
+	std::shared_ptr<const ISharedImage> loadImageFromFile(const ImageLocator & locator);
 
-	std::shared_ptr<ISharedImage> transformImage(const ImageLocator & locator, std::shared_ptr<ISharedImage> image);
-	std::shared_ptr<ISharedImage> scaleImage(const ImageLocator & locator, std::shared_ptr<ISharedImage> image);
+	std::shared_ptr<const ISharedImage> transformImage(const ImageLocator & locator, std::shared_ptr<const ISharedImage> image);
+	std::shared_ptr<const ISharedImage> scaleImage(const ImageLocator & locator, std::shared_ptr<const ISharedImage> image);
 
 	ImageLocator getLocatorForAnimationFrame(const AnimationPath & path, int frame, int group);
 

+ 9 - 12
client/renderSDL/SDLImage.cpp

@@ -269,7 +269,7 @@ void SDLImageShared::optimizeSurface()
 	}
 }
 
-std::shared_ptr<ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palette * palette) const
+std::shared_ptr<const ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palette * palette) const
 {
 	if (factor <= 0)
 		throw std::runtime_error("Unable to scale by integer value of " + std::to_string(factor));
@@ -279,10 +279,7 @@ std::shared_ptr<ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palet
 
 	SDL_Surface * scaled = nullptr;
 	if(preScaleFactor == factor)
-	{
-		scaled = CSDL_Ext::newSurface(Point(surf->w, surf->h), surf);
-		SDL_BlitSurface(surf, nullptr, scaled, nullptr);
-	}
+		return shared_from_this();
 	else if(preScaleFactor == 1)
 		scaled = CSDL_Ext::scaleSurfaceIntegerFactor(surf, factor, EScalingAlgorithm::XBRZ);
 	else
@@ -306,7 +303,7 @@ std::shared_ptr<ISharedImage> SDLImageShared::scaleInteger(int factor, SDL_Palet
 	return ret;
 }
 
-std::shared_ptr<ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const
+std::shared_ptr<const ISharedImage> SDLImageShared::scaleTo(const Point & size, SDL_Palette * palette) const
 {
 	float scaleX = float(size.x) / fullSize.x;
 	float scaleY = float(size.y) / fullSize.y;
@@ -370,7 +367,7 @@ Point SDLImageShared::dimensions() const
 	return fullSize / preScaleFactor;
 }
 
-std::shared_ptr<IImage> SDLImageShared::createImageReference(EImageBlitMode mode)
+std::shared_ptr<IImage> SDLImageShared::createImageReference(EImageBlitMode mode) const
 {
 	if (surf && surf->format->palette)
 		return std::make_shared<SDLImageIndexed>(shared_from_this(), originalPalette, mode);
@@ -378,7 +375,7 @@ std::shared_ptr<IImage> SDLImageShared::createImageReference(EImageBlitMode mode
 		return std::make_shared<SDLImageRGB>(shared_from_this(), mode);
 }
 
-std::shared_ptr<ISharedImage> SDLImageShared::horizontalFlip() const
+std::shared_ptr<const ISharedImage> SDLImageShared::horizontalFlip() const
 {
 	SDL_Surface * flipped = CSDL_Ext::horizontalFlip(surf);
 	auto ret = std::make_shared<SDLImageShared>(flipped, preScaleFactor);
@@ -390,7 +387,7 @@ std::shared_ptr<ISharedImage> SDLImageShared::horizontalFlip() const
 	return ret;
 }
 
-std::shared_ptr<ISharedImage> SDLImageShared::verticalFlip() const
+std::shared_ptr<const ISharedImage> SDLImageShared::verticalFlip() const
 {
 	SDL_Surface * flipped = CSDL_Ext::verticalFlip(surf);
 	auto ret = std::make_shared<SDLImageShared>(flipped, preScaleFactor);
@@ -444,7 +441,7 @@ void SDLImageIndexed::adjustPalette(const ColorFilter & shifter, uint32_t colors
 	}
 }
 
-SDLImageIndexed::SDLImageIndexed(const std::shared_ptr<ISharedImage> & image, SDL_Palette * originalPalette, EImageBlitMode mode)
+SDLImageIndexed::SDLImageIndexed(const std::shared_ptr<const ISharedImage> & image, SDL_Palette * originalPalette, EImageBlitMode mode)
 	:SDLImageBase::SDLImageBase(image, mode)
 	,originalPalette(originalPalette)
 {
@@ -541,13 +538,13 @@ SDLImageShared::~SDLImageShared()
 	SDL_FreePalette(originalPalette);
 }
 
-SDLImageBase::SDLImageBase(const std::shared_ptr<ISharedImage> & image, EImageBlitMode mode)
+SDLImageBase::SDLImageBase(const std::shared_ptr<const ISharedImage> & image, EImageBlitMode mode)
 	:image(image)
 	, alphaValue(SDL_ALPHA_OPAQUE)
 	, blitMode(mode)
 {}
 
-std::shared_ptr<ISharedImage> SDLImageBase::getSharedImage() const
+std::shared_ptr<const ISharedImage> SDLImageBase::getSharedImage() const
 {
 	return image;
 }

+ 9 - 9
client/renderSDL/SDLImage.h

@@ -57,11 +57,11 @@ public:
 	void exportBitmap(const boost::filesystem::path & path, SDL_Palette * palette) const override;
 	Point dimensions() const override;
 	bool isTransparent(const Point & coords) const override;
-	std::shared_ptr<IImage> createImageReference(EImageBlitMode mode) override;
-	std::shared_ptr<ISharedImage> horizontalFlip() const override;
-	std::shared_ptr<ISharedImage> verticalFlip() const override;
-	std::shared_ptr<ISharedImage> scaleInteger(int factor, SDL_Palette * palette) const override;
-	std::shared_ptr<ISharedImage> scaleTo(const Point & size, SDL_Palette * palette) const override;
+	std::shared_ptr<IImage> createImageReference(EImageBlitMode mode) const override;
+	std::shared_ptr<const ISharedImage> horizontalFlip() const override;
+	std::shared_ptr<const ISharedImage> verticalFlip() const override;
+	std::shared_ptr<const ISharedImage> scaleInteger(int factor, SDL_Palette * palette) const override;
+	std::shared_ptr<const ISharedImage> scaleTo(const Point & size, SDL_Palette * palette) const override;
 
 	friend class SDLImageLoader;
 };
@@ -69,19 +69,19 @@ public:
 class SDLImageBase : public IImage, boost::noncopyable
 {
 protected:
-	std::shared_ptr<ISharedImage> image;
+	std::shared_ptr<const ISharedImage> image;
 
 	uint8_t alphaValue;
 	EImageBlitMode blitMode;
 
 public:
-	SDLImageBase(const std::shared_ptr<ISharedImage> & image, EImageBlitMode mode);
+	SDLImageBase(const std::shared_ptr<const ISharedImage> & image, EImageBlitMode mode);
 
 	bool isTransparent(const Point & coords) const override;
 	Point dimensions() const override;
 	void setAlpha(uint8_t value) override;
 	void setBlitMode(EImageBlitMode mode) override;
-	std::shared_ptr<ISharedImage> getSharedImage() const override;
+	std::shared_ptr<const ISharedImage> getSharedImage() const override;
 };
 
 class SDLImageIndexed final : public SDLImageBase
@@ -95,7 +95,7 @@ class SDLImageIndexed final : public SDLImageBase
 
 	void setShadowTransparency(float factor);
 public:
-	SDLImageIndexed(const std::shared_ptr<ISharedImage> & image, SDL_Palette * palette, EImageBlitMode mode);
+	SDLImageIndexed(const std::shared_ptr<const ISharedImage> & image, SDL_Palette * palette, EImageBlitMode mode);
 	~SDLImageIndexed();
 
 	void draw(SDL_Surface * where, const Point & pos, const Rect * src) const override;