Pyrogenesis  trunk
CGUISprite.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  A GUI Sprite, which is actually a collage of several
20  sprites.
21 */
22 
23 #ifndef INCLUDED_CGUISPRITE
24 #define INCLUDED_CGUISPRITE
25 
26 #include "gui/GUIRenderer.h"
29 #include "lib/file/vfs/vfs_path.h"
30 #include "ps/CStr.h"
32 
33 #include <map>
34 #include <memory>
35 #include <vector>
36 
37 class CCanvas2D;
38 
40 {
45 };
46 
47 /**
48  * A CGUISprite is actually a collage of several <b>real</b>
49  * sprites, this struct represents is such real sprite.
50  */
51 struct SGUIImage
52 {
54 public:
56  m_FixedHAspectRatio(0.f),
57  m_RoundCoordinates(true),
58  m_AddressMode(Renderer::Backend::Sampler::AddressMode::REPEAT),
59  m_Effects(),
60  m_Size(CGUISize::Full()),
61  m_TextureSize(CGUISize::Full())
62  {
63  }
64 
65  // Filename of the texture
67 
68  // Image placement (relative to object)
70 
71  // Texture placement (relative to image placement)
73 
74  // Because OpenGL wants textures in squares with a power of 2 (64x64, 256x256)
75  // it's sometimes tedious to adjust this. So this value simulates which area
76  // is the real texture
78 
79  /**
80  * If non-zero, then the image's width will be adjusted when rendering so that
81  * the width:height ratio equals this value.
82  */
84 
85  /**
86  * If true, the image's coordinates will be rounded to integer pixels when
87  * rendering, to avoid blurry filtering.
88  */
90 
91  /**
92  * Texture address mode (REPEAT, CLAMP_TO_EDGE, etc).
93  */
95 
96  // Visual effects (e.g. color modulation)
97  std::shared_ptr<SGUIImageEffects> m_Effects;
98 
99  // Color
101 };
102 
103 /**
104  * The GUI sprite, is actually several real sprites (images)
105  * like a collage. View the section <sprites> in the GUI
106  * TDD for more information.
107  *
108  * Drawing routine is located in CGUI
109  *
110  * @see CGUI#DrawSprite
111  */
113 {
115 public:
117  virtual ~CGUISprite();
118 
119  /**
120  * Adds an image to the sprite collage.
121  *
122  * @param image Adds this image to the sprite collage.
123  */
124  void AddImage(std::unique_ptr<SGUIImage> image);
125 
126  /// List of images
127  std::vector<std::unique_ptr<SGUIImage>> m_Images;
128 };
129 
130 // An instance of a sprite, usually stored in IGUIObjects - basically a string
131 // giving the sprite's name, but with some extra data to cache rendering
132 // calculations between draw calls.
134 {
135 public:
138 
140  CGUISpriteInstance(const CStr& SpriteName);
141 
142  void Draw(CGUI& pGUI, CCanvas2D& canvas, const CRect& Size, std::map<CStr, std::unique_ptr<const CGUISprite>>& Sprites) const;
143 
144  /**
145  * Whether this Sprite has no texture name set.
146  */
147  operator bool() const { return !m_SpriteName.empty(); };
148 
149  /**
150  * Returns the sprite texture name.
151  */
152  const CStr& GetName() const { return m_SpriteName; }
153 
154  /**
155  * Changes the texture name.
156  * Use as rarely as possible, because it clears the draw cache.
157  */
158  void SetName(const CStr& SpriteName);
159 
160 private:
162 
163  // Stored drawing calls, for more efficient rendering
165  // Relevant details of previously rendered sprite; the cache is invalidated
166  // whenever any of these values changes.
168 };
169 
170 #endif // INCLUDED_CGUISPRITE
#define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:227
CRect m_CachedSize
Definition: CGUISprite.h:167
Definition: CGUISprite.h:39
float m_FixedHAspectRatio
If non-zero, then the image&#39;s width will be adjusted when rendering so that the width:height ratio eq...
Definition: CGUISprite.h:83
#define MOVABLE(className)
Indicates that move semantics can be used, so that a NONCOPYABLE class can still be assigned by takin...
Definition: code_annotation.h:235
Same as the CColor class, but this one can also parse colors predefined in the GUI page (such as "yel...
Definition: CGUIColor.h:29
void Draw(DrawCalls &Calls, CCanvas2D &canvas)
Definition: GUIRenderer.cpp:314
Renderer::Backend::Sampler::AddressMode m_AddressMode
Texture address mode (REPEAT, CLAMP_TO_EDGE, etc).
Definition: CGUISprite.h:94
CGUISize m_Size
Definition: CGUISprite.h:69
The main object that represents a whole GUI page.
Definition: CGUI.h:60
CGUIColor m_BackColor
Definition: CGUISprite.h:100
bool m_RoundCoordinates
If true, the image&#39;s coordinates will be rounded to integer pixels when rendering, to avoid blurry filtering.
Definition: CGUISprite.h:89
SGUIImage()
Definition: CGUISprite.h:55
Definition: path.h:79
Definition: GUIRenderer.h:58
AddressMode
Definition: Sampler.h:41
This class represents a rectangle relative to a parent rectangle The value can be initialized from a ...
Definition: CGUISize.h:29
Definition: Canvas2D.h:35
CGUIColor m_SolidColor
Definition: CGUISprite.h:43
std::shared_ptr< SGUIImageEffects > m_Effects
Definition: CGUISprite.h:97
CRect m_TexturePlacementInFile
Definition: CGUISprite.h:77
Backend
Definition: Backend.h:27
CGUIColor m_AddColor
Definition: CGUISprite.h:42
SGUIImageEffects()
Definition: CGUISprite.h:41
VfsPath m_TextureName
Definition: CGUISprite.h:66
A CGUISprite is actually a collage of several real sprites, this struct represents is such real sprit...
Definition: CGUISprite.h:51
The GUI sprite, is actually several real sprites (images) like a collage.
Definition: CGUISprite.h:112
CStr m_SpriteName
Definition: CGUISprite.h:161
std::vector< std::unique_ptr< SGUIImage > > m_Images
List of images.
Definition: CGUISprite.h:127
Definition: VideoMode.h:28
const CStr & GetName() const
Returns the sprite texture name.
Definition: CGUISprite.h:152
CGUISize m_TextureSize
Definition: CGUISprite.h:72
CGUISprite()
Definition: CGUISprite.h:116
Definition: CGUISprite.h:133
bool m_Greyscale
Definition: CGUISprite.h:44
Rectangle class used for screen rectangles.
Definition: Rect.h:30
GUIRenderer::DrawCalls m_DrawCallCache
Definition: CGUISprite.h:164