File: CGUISprite.h

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (170 lines) | stat: -rw-r--r-- 4,347 bytes parent folder | download | duplicates (4)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (C) 2022 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
	A GUI Sprite, which is actually a collage of several
	sprites.
*/

#ifndef INCLUDED_CGUISPRITE
#define INCLUDED_CGUISPRITE

#include "gui/GUIRenderer.h"
#include "gui/SettingTypes/CGUISize.h"
#include "gui/SettingTypes/CGUIColor.h"
#include "lib/file/vfs/vfs_path.h"
#include "ps/CStr.h"
#include "renderer/backend/Sampler.h"

#include <map>
#include <memory>
#include <vector>

class CCanvas2D;

struct SGUIImageEffects
{
	SGUIImageEffects() : m_Greyscale(false) {}
	CGUIColor m_AddColor;
	CGUIColor m_SolidColor;
	bool m_Greyscale;
};

/**
 * A CGUISprite is actually a collage of several <b>real</b>
 * sprites, this struct represents is such real sprite.
 */
struct SGUIImage
{
	NONCOPYABLE(SGUIImage);
public:
	SGUIImage() :
		m_FixedHAspectRatio(0.f),
		m_RoundCoordinates(true),
		m_AddressMode(Renderer::Backend::Sampler::AddressMode::REPEAT),
		m_Effects(),
		m_Size(CGUISize::Full()),
		m_TextureSize(CGUISize::Full())
	{
	}

	// Filename of the texture
	VfsPath m_TextureName;

	// Image placement (relative to object)
	CGUISize m_Size;

	// Texture placement (relative to image placement)
	CGUISize m_TextureSize;

	// Because OpenGL wants textures in squares with a power of 2 (64x64, 256x256)
	//  it's sometimes tedious to adjust this. So this value simulates which area
	//  is the real texture
	CRect m_TexturePlacementInFile;

	/**
	 * If non-zero, then the image's width will be adjusted when rendering so that
	 * the width:height ratio equals this value.
	 */
	float m_FixedHAspectRatio;

	/**
	 * If true, the image's coordinates will be rounded to integer pixels when
	 * rendering, to avoid blurry filtering.
	 */
	bool m_RoundCoordinates;

	/**
	 * Texture address mode (REPEAT, CLAMP_TO_EDGE, etc).
	 */
	Renderer::Backend::Sampler::AddressMode m_AddressMode;

	// Visual effects (e.g. color modulation)
	std::shared_ptr<SGUIImageEffects> m_Effects;

	// Color
	CGUIColor m_BackColor;
};

/**
 * The GUI sprite, is actually several real sprites (images)
 * like a collage. View the section \<sprites\> in the GUI
 * TDD for more information.
 *
 * Drawing routine is located in CGUI
 *
 * @see CGUI#DrawSprite
 */
class CGUISprite
{
	NONCOPYABLE(CGUISprite);
public:
	CGUISprite() {}
	virtual ~CGUISprite();

	/**
	 * Adds an image to the sprite collage.
	 *
	 * @param image Adds this image to the sprite collage.
	 */
	void AddImage(std::unique_ptr<SGUIImage> image);

	/// List of images
	std::vector<std::unique_ptr<SGUIImage>> m_Images;
};

// An instance of a sprite, usually stored in IGUIObjects - basically a string
// giving the sprite's name, but with some extra data to cache rendering
// calculations between draw calls.
class CGUISpriteInstance
{
public:
	NONCOPYABLE(CGUISpriteInstance);
	MOVABLE(CGUISpriteInstance);

	CGUISpriteInstance();
	CGUISpriteInstance(const CStr& SpriteName);

	void Draw(CGUI& pGUI, CCanvas2D& canvas, const CRect& Size, std::map<CStr, std::unique_ptr<const CGUISprite>>& Sprites) const;

	/**
	 * Whether this Sprite has no texture name set.
	 */
	operator bool() const { return !m_SpriteName.empty(); };

	/**
	 * Returns the sprite texture name.
	 */
	const CStr& GetName() const { return m_SpriteName; }

	/**
	 * Changes the texture name.
	 * Use as rarely as possible, because it clears the draw cache.
	 */
	void SetName(const CStr& SpriteName);

private:
	CStr m_SpriteName;

	// Stored drawing calls, for more efficient rendering
	mutable GUIRenderer::DrawCalls m_DrawCallCache;
	// Relevant details of previously rendered sprite; the cache is invalidated
	// whenever any of these values changes.
	mutable CRect m_CachedSize;
};

#endif // INCLUDED_CGUISPRITE