Line data Source code
1 : /* Copyright (C) 2026 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" 27 : #include "gui/SettingTypes/CGUIColor.h" 28 : #include "gui/SettingTypes/CGUISize.h" 29 : #include "lib/code_annotation.h" 30 : #include "lib/file/vfs/vfs_path.h" 31 : #include "maths/Rect.h" 32 : #include "ps/CStr.h" 33 : #include "renderer/backend/Sampler.h" 34 : 35 : #include <map> 36 : #include <memory> 37 : #include <vector> 38 : 39 : class CCanvas2D; 40 : class CGUI; 41 : 42 : struct SGUIImageEffects 43 : { 44 0 : SGUIImageEffects() : m_Greyscale(false) {} 45 : CGUIColor m_AddColor; 46 : CGUIColor m_SolidColor; 47 : bool m_Greyscale; 48 : }; 49 : 50 : /** 51 : * A CGUISprite is actually a collage of several <b>real</b> 52 : * sprites, this struct represents is such real sprite. 53 : */ 54 : struct SGUIImage 55 : { 56 : NONCOPYABLE(SGUIImage); 57 : public: 58 0 : SGUIImage() : 59 0 : m_FixedHAspectRatio(0.f), 60 0 : m_RoundCoordinates(true), 61 0 : m_AddressMode(Renderer::Backend::Sampler::AddressMode::REPEAT), 62 0 : m_Effects(), 63 0 : m_Size(CGUISize::Full()), 64 0 : m_TextureSize(CGUISize::Full()) 65 : { 66 0 : } 67 : 68 : // Filename of the texture 69 : VfsPath m_TextureName; 70 : 71 : // Image placement (relative to object) 72 : CGUISize m_Size; 73 : 74 : // Texture placement (relative to image placement) 75 : CGUISize m_TextureSize; 76 : 77 : // Because OpenGL wants textures in squares with a power of 2 (64x64, 256x256) 78 : // it's sometimes tedious to adjust this. So this value simulates which area 79 : // is the real texture 80 : CRect m_TexturePlacementInFile; 81 : 82 : /** 83 : * If non-zero, then the image's width will be adjusted when rendering so that 84 : * the width:height ratio equals this value. 85 : */ 86 : float m_FixedHAspectRatio; 87 : 88 : /** 89 : * If true, the image's coordinates will be rounded to integer pixels when 90 : * rendering, to avoid blurry filtering. 91 : */ 92 : bool m_RoundCoordinates; 93 : 94 : /** 95 : * Texture address mode (REPEAT, CLAMP_TO_EDGE, etc). 96 : */ 97 : Renderer::Backend::Sampler::AddressMode m_AddressMode; 98 : 99 : // Visual effects (e.g. color modulation) 100 : std::shared_ptr<SGUIImageEffects> m_Effects; 101 : 102 : // Color 103 : CGUIColor m_BackColor; 104 : }; 105 : 106 : /** 107 : * The GUI sprite, is actually several real sprites (images) 108 : * like a collage. View the section \<sprites\> in the GUI 109 : * TDD for more information. 110 : * 111 : * Drawing routine is located in CGUI 112 : * 113 : * @see CGUI#DrawSprite 114 : */ 115 : class CGUISprite 116 : { 117 : NONCOPYABLE(CGUISprite); 118 : public: 119 0 : CGUISprite() {} 120 : virtual ~CGUISprite(); 121 : 122 : /** 123 : * Adds an image to the sprite collage. 124 : * 125 : * @param image Adds this image to the sprite collage. 126 : */ 127 : void AddImage(std::unique_ptr<SGUIImage> image); 128 : 129 : /// List of images 130 : std::vector<std::unique_ptr<SGUIImage>> m_Images; 131 : }; 132 : 133 : // An instance of a sprite, usually stored in IGUIObjects - basically a string 134 : // giving the sprite's name, but with some extra data to cache rendering 135 : // calculations between draw calls. 136 : class CGUISpriteInstance 137 : { 138 : public: 139 : NONCOPYABLE(CGUISpriteInstance); 140 0 : MOVABLE(CGUISpriteInstance); 141 : 142 : CGUISpriteInstance(); 143 : CGUISpriteInstance(const CStr& SpriteName); 144 : CGUISpriteInstance(const CStr& SpriteName, const bool RoundCoordinates); 145 : 146 : void Draw(CGUI& pGUI, CCanvas2D& canvas, const CRect& Size, std::map<CStr, std::unique_ptr<const CGUISprite>>& Sprites) const; 147 : 148 : /** 149 : * Whether this Sprite has no texture name set. 150 : */ 151 0 : operator bool() const { return !m_SpriteName.empty(); }; 152 : 153 : /** 154 : * Returns the sprite texture name. 155 : */ 156 0 : const CStr& GetName() const { return m_SpriteName; } 157 : 158 : /** 159 : * Changes the texture name. 160 : * Use as rarely as possible, because it clears the draw cache. 161 : */ 162 : void SetName(const CStr& SpriteName); 163 : 164 : /** 165 : * Sets the coordinates to be rounded to integer pixels. 166 : * This is useful for pixel art, icons in text, to avoid blurry filtering. 167 : * Use as rarely as possible, because it clears the draw cache. 168 : */ 169 : void SetRoundCoordinates(const bool RoundCoordinates); 170 : private: 171 : CStr m_SpriteName{}; 172 : 173 : bool m_RoundCoordinates{true}; 174 : 175 : // Stored drawing calls, for more efficient rendering 176 : mutable GUIRenderer::DrawCalls m_DrawCallCache; 177 : // Relevant details of previously rendered sprite; the cache is invalidated 178 : // whenever any of these values changes. 179 : mutable CRect m_CachedSize; 180 : }; 181 : 182 : #endif // INCLUDED_CGUISPRITE