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 : #ifndef INCLUDED_MINIMAPTEXTURE 19 : #define INCLUDED_MINIMAPTEXTURE 20 : 21 : #include "graphics/Color.h" 22 : #include "graphics/ShaderTechniquePtr.h" 23 : #include "graphics/Texture.h" 24 : #include "lib/code_annotation.h" 25 : #include "lib/posix/posix_types.h" 26 : #include "maths/Vector2D.h" 27 : #include "renderer/VertexArray.h" 28 : 29 : #include <cstddef> 30 : #include <memory> 31 : #include <string> 32 : #include <unordered_map> 33 : #include <vector> 34 : 35 : class CLOSTexture; 36 : class CSimulation2; 37 : class CTerrain; 38 : class CTerritoryTexture; 39 : namespace Renderer::Backend { class IDevice; } 40 : namespace Renderer::Backend { class IDeviceCommandContext; } 41 : namespace Renderer::Backend { class IFramebuffer; } 42 : namespace Renderer::Backend { class ITexture; } 43 : namespace Renderer::Backend { class IVertexInputLayout; } 44 : 45 : class CMiniMapTexture 46 : { 47 : NONCOPYABLE(CMiniMapTexture); 48 : public: 49 : CMiniMapTexture(Renderer::Backend::IDevice* device, CSimulation2& simulation); 50 : ~CMiniMapTexture(); 51 : 52 : void RequestRendering(); 53 : 54 : /** 55 : * Marks the texture as dirty if it's old enough to redraw it on Render. 56 : */ 57 : void Update(const float deltaRealTime); 58 : 59 : /** 60 : * Redraws the texture if it's dirty. 61 : */ 62 : void Render( 63 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 64 : CLOSTexture& losTexture, CTerritoryTexture& territoryTexture); 65 : 66 0 : const CTexturePtr& GetTexture() const { return m_FinalTexture; } 67 : 68 0 : bool IsFlipped() const { return m_Flipped; } 69 : 70 : /** 71 : * @return The maximum height for unit passage in water. 72 : */ 73 : static float GetShallowPassageHeight(); 74 : 75 0 : struct Icon 76 : { 77 : CTexturePtr texture; 78 : CColor color; 79 : CVector2D worldPosition; 80 : float halfSize; 81 : }; 82 : // Returns icons for corresponding entities on the minimap texture. 83 0 : const std::vector<Icon>& GetIcons() { return m_Icons; } 84 : 85 : private: 86 : void CreateTextures( 87 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 88 : const CTerrain& terrain); 89 : void DestroyTextures(); 90 : void RebuildTerrainTexture( 91 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 92 : const CTerrain& terrain); 93 : void RenderFinalTexture( 94 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 95 : CLOSTexture& losTexture, CTerritoryTexture& territoryTexture); 96 : void UpdateAndUploadEntities( 97 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 98 : const float entityRadius, const double& currentTime); 99 : void DrawEntities( 100 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 101 : const float entityRadius); 102 : 103 : CSimulation2& m_Simulation; 104 : 105 : bool m_RenderingRequested = false; 106 : 107 : bool m_TerrainTextureDirty = true; 108 : bool m_FinalTextureDirty = true; 109 : double m_LastFinalTextureUpdate = 0.0; 110 : bool m_Flipped = false; 111 : 112 : // minimap texture handles 113 : std::unique_ptr<Renderer::Backend::ITexture> m_TerrainTexture; 114 : 115 : CTexturePtr m_FinalTexture; 116 : 117 : std::unique_ptr<Renderer::Backend::IFramebuffer> 118 : m_FinalTextureFramebuffer; 119 : 120 : // texture data 121 : std::unique_ptr<std::uint32_t[]> m_TerrainData; 122 : 123 : // map size 124 : ssize_t m_MapSize = 0; 125 : 126 : // Maximal water height to allow the passage of a unit (for underwater shallows). 127 : float m_ShallowPassageHeight = 0.0f; 128 : float m_WaterHeight = 0.0f; 129 : 130 : Renderer::Backend::IVertexInputLayout* m_QuadVertexInputLayout = nullptr; 131 : Renderer::Backend::IVertexInputLayout* m_EntitiesVertexInputLayout = nullptr; 132 : 133 : VertexIndexArray m_IndexArray; 134 : VertexArray m_VertexArray; 135 : VertexArray::Attribute m_AttributePos; 136 : VertexArray::Attribute m_AttributeColor; 137 : 138 : bool m_UseInstancing = false; 139 : // Vertex data if instancing is supported. 140 : VertexArray m_InstanceVertexArray; 141 : VertexArray::Attribute m_InstanceAttributePosition; 142 : 143 : CShaderTechniquePtr m_TerritoryTechnique; 144 : 145 : size_t m_EntitiesDrawn = 0; 146 : 147 : double m_PingDuration = 25.0; 148 : double m_HalfBlinkDuration = 0.0; 149 : double m_NextBlinkTime = 0.0; 150 : bool m_BlinkState = false; 151 : 152 : std::vector<Icon> m_Icons; 153 : // We store the map as a member to avoid redundant reallocations on each 154 : // update. We use a grid approach to combine icons by distance. 155 0 : struct CellIconKey 156 : { 157 : std::string path; 158 : std::uint8_t r, g, b; 159 : }; 160 : struct CellIconKeyHash 161 : { 162 : size_t operator()(const CellIconKey& key) const; 163 : }; 164 : struct CellIconKeyEqual 165 : { 166 : bool operator()(const CellIconKey& lhs, const CellIconKey& rhs) const; 167 : }; 168 : struct CellIcon 169 : { 170 : // TODO: use CVector2DI. 171 : std::uint16_t gridX, gridY; 172 : float halfSize; 173 : CVector2D worldPosition; 174 : }; 175 : std::unordered_map<CellIconKey, std::vector<CellIcon>, CellIconKeyHash, CellIconKeyEqual> m_IconsCache; 176 : }; 177 : 178 : #endif // INCLUDED_MINIMAPTEXTURE