File: ScalableImage.h

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (125 lines) | stat: -rw-r--r-- 4,551 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * ScalableImage.h, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#pragma once

#include "../render/IImage.h"
#include "../render/ImageLocator.h"
#include "../render/Colors.h"

#include "../../lib/Color.h"

struct SDL_Palette;

class ScalableImageInstance;
class CanvasImage;

struct ScalableImageParameters : boost::noncopyable
{
	SDL_Palette * palette = nullptr;

	ColorRGBA colorMultiplier = Colors::WHITE_TRUE;
	ColorRGBA ovelayColorMultiplier = Colors::WHITE_TRUE;

	PlayerColor player = PlayerColor::CANNOT_DETERMINE;
	uint8_t alphaValue = 255;

	bool flipVertical = false;
	bool flipHorizontal = false;

	ScalableImageParameters(const SDL_Palette * originalPalette, EImageBlitMode blitMode);
	~ScalableImageParameters();

	void setShadowTransparency(const SDL_Palette * originalPalette, float factor);
	void shiftPalette(const SDL_Palette * originalPalette, uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove);
	void playerColored(PlayerColor player);
	void setOverlayColor(const SDL_Palette * originalPalette, const ColorRGBA & color);
	void preparePalette(const SDL_Palette * originalPalette, EImageBlitMode blitMode);
	void adjustPalette(const SDL_Palette * originalPalette, EImageBlitMode blitMode, const ColorFilter & shifter, uint32_t colorsToSkipMask);
};

class ScalableImageShared final : public std::enable_shared_from_this<ScalableImageShared>, boost::noncopyable
{
	static constexpr int scalingSize = 5; // 0-4 range. TODO: switch to 1-4 since there is no '0' scaling
	static constexpr int maxFlips = 4;

	using ImageType = std::shared_ptr<const ISharedImage>;
	using FlippedImages = std::array<ImageType, maxFlips>;
	using PlayerColoredImages = std::array<ImageType, PlayerColor::PLAYER_LIMIT_I + 1>; // all valid colors+neutral

	struct ScaledImage
	{
		/// Upscaled shadow of our image, may be null
		FlippedImages shadow;

		/// Upscaled main part of our image, may be null
		FlippedImages body;

		/// Upscaled overlay (player color, selection highlight) of our image, may be null
		FlippedImages overlay;

		/// player-colored images of this particular scale, mostly for UI. These are never flipped in h3
		PlayerColoredImages playerColored;
	};

	/// 1x-4x images. body for 1x scaling is guaranteed to be loaded
	std::array<ScaledImage, scalingSize> scaled;

	/// Locator of this image, for loading additional (e.g. upscaled) images
	const SharedImageLocator locator;

	std::shared_ptr<const ISharedImage> loadOrGenerateImage(EImageBlitMode mode, int8_t scalingFactor, PlayerColor color, ImageType upscalingSource) const;

	void loadScaledImages(int8_t scalingFactor, PlayerColor color);

public:
	ScalableImageShared(const SharedImageLocator & locator, const std::shared_ptr<const ISharedImage> & baseImage);

	Point dimensions() const;
	void exportBitmap(const boost::filesystem::path & path, const ScalableImageParameters & parameters) const;
	bool isTransparent(const Point & coords) const;
	Rect contentRect() const;
	void draw(SDL_Surface * where, const Point & dest, const Rect * src, const ScalableImageParameters & parameters, int scalingFactor);

	const SDL_Palette * getPalette() const;

	std::shared_ptr<ScalableImageInstance> createImageReference();

	void preparePlayerColoredImage(PlayerColor color);
};

class ScalableImageInstance final : public IImage
{
	friend class ScalableImageShared;

	std::shared_ptr<ScalableImageShared> image;
	std::shared_ptr<CanvasImage> scaledImage;

	ScalableImageParameters parameters;
	EImageBlitMode blitMode;

public:
	ScalableImageInstance(const std::shared_ptr<ScalableImageShared> & image, EImageBlitMode blitMode);

	void scaleTo(const Point & size, EScalingAlgorithm algorithm) override;
	void exportBitmap(const boost::filesystem::path & path) const override;
	bool isTransparent(const Point & coords) const override;
	Rect contentRect() const override;
	Point dimensions() const override;
	void setAlpha(uint8_t value) override;
	void draw(SDL_Surface * where, const Point & pos, const Rect * src, int scalingFactor) const override;
	void setOverlayColor(const ColorRGBA & color) override;
	void playerColored(const PlayerColor & player) override;
	void shiftPalette(uint32_t firstColorID, uint32_t colorsToMove, uint32_t distanceToMove) override;
	void adjustPalette(const ColorFilter & shifter, uint32_t colorsToSkipMask) override;

	void horizontalFlip();
	void verticalFlip();
};