Pyrogenesis  trunk
MiniMapTexture.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 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"
23 #include "graphics/Texture.h"
24 #include "maths/Vector2D.h"
28 #include "renderer/VertexArray.h"
29 
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 
41 {
43 public:
44  CMiniMapTexture(CSimulation2& simulation);
46 
47  /**
48  * Marks the texture as dirty if it's old enough to redraw it on Render.
49  */
50  void Update(const float deltaRealTime);
51 
52  /**
53  * Redraws the texture if it's dirty.
54  */
55  void Render(
56  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
57  CLOSTexture& losTexture, CTerritoryTexture& territoryTexture);
58 
59  const CTexturePtr& GetTexture() const { return m_FinalTexture; }
60 
61  bool IsFlipped() const { return m_Flipped; }
62 
63  /**
64  * @return The maximum height for unit passage in water.
65  */
66  static float GetShallowPassageHeight();
67 
68  struct Icon
69  {
73  float halfSize;
74  };
75  // Returns icons for corresponding entities on the minimap texture.
76  const std::vector<Icon>& GetIcons() { return m_Icons; }
77 
78 private:
79  void CreateTextures(
80  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
81  const CTerrain* terrain);
82  void DestroyTextures();
84  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
85  const CTerrain* terrain);
86  void RenderFinalTexture(
87  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
88  CLOSTexture& losTexture, CTerritoryTexture& territoryTexture);
90  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
91  const float entityRadius, const double& currentTime);
92  void DrawEntities(
93  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
94  const float entityRadius);
95 
97 
98  bool m_TerrainTextureDirty = true;
99  bool m_FinalTextureDirty = true;
101  bool m_Flipped = false;
102 
103  // minimap texture handles
104  std::unique_ptr<Renderer::Backend::ITexture> m_TerrainTexture;
105 
107 
108  std::unique_ptr<Renderer::Backend::IFramebuffer>
110 
111  // texture data
112  std::unique_ptr<u32[]> m_TerrainData;
113 
114  // map size
116 
117  // Maximal water height to allow the passage of a unit (for underwater shallows).
119  float m_WaterHeight = 0.0f;
120 
123 
128 
129  bool m_UseInstancing = false;
130  // Vertex data if instancing is supported.
133 
135 
136  size_t m_EntitiesDrawn = 0;
137 
138  double m_PingDuration = 25.0;
139  double m_HalfBlinkDuration = 0.0;
140  double m_NextBlinkTime = 0.0;
141  bool m_BlinkState = false;
142 
143  std::vector<Icon> m_Icons;
144  // We store the map as a member to avoid redundant reallocations on each
145  // update. We use a grid approach to combine icons by distance.
146  struct CellIconKey
147  {
148  std::string path;
149  u8 r, g, b;
150  };
152  {
153  size_t operator()(const CellIconKey& key) const;
154  };
156  {
157  bool operator()(const CellIconKey& lhs, const CellIconKey& rhs) const;
158  };
159  struct CellIcon
160  {
161  // TODO: use CVector2DI.
162  u16 gridX, gridY;
163  float halfSize;
165  };
166  std::unordered_map<CellIconKey, std::vector<CellIcon>, CellIconKeyHash, CellIconKeyEqual> m_IconsCache;
167 };
168 
169 #endif // INCLUDED_MINIMAPTEXTURE
CVector2D worldPosition
Definition: MiniMapTexture.h:72
CSimulation2 & m_Simulation
Definition: MiniMapTexture.h:96
VertexArray::Attribute m_AttributePos
Definition: MiniMapTexture.h:126
double m_LastFinalTextureUpdate
Definition: MiniMapTexture.h:100
CShaderTechniquePtr m_TerritoryTechnique
Definition: MiniMapTexture.h:134
std::string path
Definition: MiniMapTexture.h:148
bool m_UseInstancing
Definition: MiniMapTexture.h:129
Definition: MiniMapTexture.h:146
Renderer::Backend::IVertexInputLayout * m_EntitiesVertexInputLayout
Definition: MiniMapTexture.h:122
uint16_t u16
Definition: types.h:38
Definition: MiniMapTexture.h:40
const std::vector< Icon > & GetIcons()
Definition: MiniMapTexture.h:76
Definition: MiniMapTexture.h:151
Definition: Color.h:42
Definition: MiniMapTexture.h:155
void DrawEntities(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const float entityRadius)
Definition: MiniMapTexture.cpp:799
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:73
bool m_TerrainTextureDirty
Definition: MiniMapTexture.h:98
Definition: Terrain.h:51
VertexIndexArray m_IndexArray
Definition: MiniMapTexture.h:124
double m_HalfBlinkDuration
Definition: MiniMapTexture.h:139
void RebuildTerrainTexture(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CTerrain *terrain)
Definition: MiniMapTexture.cpp:429
std::unordered_map< CellIconKey, std::vector< CellIcon >, CellIconKeyHash, CellIconKeyEqual > m_IconsCache
Definition: MiniMapTexture.h:166
uint8_t u8
Definition: types.h:37
VertexArray::Attribute m_AttributeColor
Definition: MiniMapTexture.h:127
Definition: MiniMapTexture.h:68
float m_ShallowPassageHeight
Definition: MiniMapTexture.h:118
Public API for simulation system.
Definition: Simulation2.h:46
A VertexArray that is specialised to handle 16-bit array indices.
Definition: VertexArray.h:219
CTexturePtr m_FinalTexture
Definition: MiniMapTexture.h:106
size_t m_EntitiesDrawn
Definition: MiniMapTexture.h:136
Renderer::Backend::IVertexInputLayout * m_QuadVertexInputLayout
Definition: MiniMapTexture.h:121
VertexArray m_VertexArray
Definition: MiniMapTexture.h:125
CMiniMapTexture(CSimulation2 &simulation)
Definition: MiniMapTexture.cpp:192
const CTexturePtr & GetTexture() const
Definition: MiniMapTexture.h:59
void Render(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CLOSTexture &losTexture, CTerritoryTexture &territoryTexture)
Redraws the texture if it&#39;s dirty.
Definition: MiniMapTexture.cpp:355
u8 r
Definition: MiniMapTexture.h:149
Definition: VertexArray.h:140
void RenderFinalTexture(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CLOSTexture &losTexture, CTerritoryTexture &territoryTexture)
Definition: MiniMapTexture.cpp:495
~CMiniMapTexture()
Definition: MiniMapTexture.cpp:341
CTexturePtr texture
Definition: MiniMapTexture.h:70
void CreateTextures(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CTerrain *terrain)
Definition: MiniMapTexture.cpp:372
std::unique_ptr< Renderer::Backend::ITexture > m_TerrainTexture
Definition: MiniMapTexture.h:104
pthread_key_t key
Definition: wpthread.cpp:140
float m_WaterHeight
Definition: MiniMapTexture.h:119
Definition: VertexArray.h:137
VertexArray m_InstanceVertexArray
Definition: MiniMapTexture.h:131
NONCOPYABLE(CMiniMapTexture)
VertexArray::Attribute m_InstanceAttributePosition
Definition: MiniMapTexture.h:132
float halfSize
Definition: MiniMapTexture.h:73
CVector2D worldPosition
Definition: MiniMapTexture.h:164
std::unique_ptr< Renderer::Backend::IFramebuffer > m_FinalTextureFramebuffer
Definition: MiniMapTexture.h:109
bool m_Flipped
Definition: MiniMapTexture.h:101
intptr_t ssize_t
Definition: wposix_types.h:82
bool IsFlipped() const
Definition: MiniMapTexture.h:61
Definition: MiniMapTexture.h:159
float halfSize
Definition: MiniMapTexture.h:163
Definition: Vector2D.h:31
void Update(const float deltaRealTime)
Marks the texture as dirty if it&#39;s old enough to redraw it on Render.
Definition: MiniMapTexture.cpp:346
bool m_BlinkState
Definition: MiniMapTexture.h:141
CColor color
Definition: MiniMapTexture.h:71
static float GetShallowPassageHeight()
Definition: MiniMapTexture.cpp:866
void UpdateAndUploadEntities(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const float entityRadius, const double &currentTime)
Definition: MiniMapTexture.cpp:621
ssize_t m_MapSize
Definition: MiniMapTexture.h:115
u16 gridY
Definition: MiniMapTexture.h:162
std::shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechniquePtr.h:27
Maintains the LOS (fog-of-war / shroud-of-darkness) texture, used for rendering and for the minimap...
Definition: LOSTexture.h:38
Definition: IDeviceCommandContext.h:40
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:22
Maintains the territory boundary texture, used for rendering and for the minimap. ...
Definition: TerritoryTexture.h:30
void DestroyTextures()
Definition: MiniMapTexture.cpp:422
std::vector< Icon > m_Icons
Definition: MiniMapTexture.h:143
std::unique_ptr< u32[]> m_TerrainData
Definition: MiniMapTexture.h:112
double m_NextBlinkTime
Definition: MiniMapTexture.h:140
bool m_FinalTextureDirty
Definition: MiniMapTexture.h:99
double m_PingDuration
Definition: MiniMapTexture.h:138