Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
TerrainOverlay.h
Go to the documentation of this file.
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 * System for representing tile-based information on top of the
20 * terrain.
21 */
22
23#ifndef INCLUDED_TERRAINOVERLAY
24#define INCLUDED_TERRAINOVERLAY
25
26#include "graphics/SColor.h"
28#include "lib/code_annotation.h"
30
31#include <cstddef>
32#include <functional>
33#include <memory>
34
35class CSimContext;
36class CTerrain;
37namespace Renderer::Backend { class IDeviceCommandContext; }
38namespace Renderer::Backend { class ITexture; }
39namespace Renderer::Backend { class IVertexInputLayout; }
40struct CColor;
41struct SColor4ub;
42
43/**
44 * Common interface for terrain-tile-based and texture-based debug overlays.
45 *
46 * An overlay object will be rendered for as long as it is allocated
47 * (it is automatically registered/deregistered by constructor/destructor).
48 */
50{
52
53public:
54 virtual ~ITerrainOverlay();
55
57
58 virtual void RenderAfterWater(Renderer::Backend::IDeviceCommandContext*, int /*cullGroup*/) { }
59 /**
60 * Draw all ITerrainOverlay objects that exist
61 * and that should be drawn before water.
62 */
63 static void RenderOverlaysBeforeWater(
64 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
65
66 /**
67 * Draw all ITerrainOverlay objects that exist
68 * and that should be drawn after water.
69 */
70 static void RenderOverlaysAfterWater(
71 Renderer::Backend::IDeviceCommandContext* deviceCommandContext, int cullGroup);
72
73protected:
74 ITerrainOverlay(int priority);
75};
76
77/**
78 * Base class for (relatively) simple drawing of
79 * data onto terrain tiles, intended for debugging purposes and for the Atlas
80 * editor (hence not trying to be very efficient).
81 *
82 * To start drawing a terrain overlay, first create a subclass of TerrainOverlay.
83 * Override the method GetTileExtents if you want to change the range over which
84 * it is drawn.
85 * Override ProcessTile to do your processing for each tile, which should call
86 * RenderTile and RenderTileOutline as appropriate.
87 */
89{
90protected:
91 /**
92 * Construct the object and register it with the global
93 * list of terrain overlays.
94 * <p>
95 * The priority parameter controls the order in which overlays are drawn,
96 * if several exist - they are processed in order of increasing priority,
97 * so later ones draw on top of earlier ones.
98 * Most should use the default of 100. Numbers from 200 are used
99 * by Atlas.
100 *
101 * @param priority controls the order of drawing
102 */
103 TerrainOverlay(const CSimContext& simContext, int priority = 100);
104
105 /**
106 * Override to perform processing at the start of the overlay rendering,
107 * before the ProcessTile calls
108 */
109 virtual void StartRender();
110
111 /**
112 * Override to perform processing at the end of the overlay rendering,
113 * after the ProcessTile calls
114 */
115 virtual void EndRender();
116
117 /**
118 * Override to limit the range over which ProcessTile will
119 * be called. Defaults to the size of the map.
120 *
121 * @param min_i_inclusive [output] smallest <i>i</i> coordinate, in tile-space units
122 * (1 unit per tile, <i>+i</i> is world-space <i>+x</i> and game-space East)
123 * @param min_j_inclusive [output] smallest <i>j</i> coordinate
124 * (<i>+j</i> is world-space <i>+z</i> and game-space North)
125 * @param max_i_inclusive [output] largest <i>i</i> coordinate
126 * @param max_j_inclusive [output] largest <i>j</i> coordinate
127 */
128 virtual void GetTileExtents(ssize_t& min_i_inclusive, ssize_t& min_j_inclusive,
129 ssize_t& max_i_inclusive, ssize_t& max_j_inclusive);
130
131 /**
132 * Override to perform processing of each tile. Typically calls
133 * RenderTile and/or RenderTileOutline.
134 *
135 * @param i <i>i</i> coordinate of tile being processed
136 * @param j <i>j</i> coordinate of tile being processed
137 */
138 virtual void ProcessTile(
139 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
140 ssize_t i, ssize_t j) = 0;
141
142 /**
143 * Draw a filled quad on top of the current tile.
144 *
145 * @param color color to draw. May be transparent (alpha &lt; 1)
146 * @param drawHidden true if hidden tiles (i.e. those behind other tiles)
147 * should be drawn
148 */
149 void RenderTile(
150 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
151 const CColor& color, bool drawHidden);
152
153 /**
154 * Draw a filled quad on top of the given tile.
155 */
156 void RenderTile(
157 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
158 const CColor& color, bool drawHidden, ssize_t i, ssize_t j);
159
160 /**
161 * Draw an outlined quad on top of the current tile.
162 *
163 * @param color color to draw. May be transparent (alpha &lt; 1)
164 * @param lineWidth width of lines in pixels. 1 is a sensible value
165 * @param drawHidden true if hidden tiles (i.e. those behind other tiles)
166 * should be drawn
167 */
169 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
170 const CColor& color, bool drawHidden);
171
172 /**
173 * Draw an outlined quad on top of the given tile.
174 */
176 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
177 const CColor& color, bool drawHidden, ssize_t i, ssize_t j);
178
179private:
180 // Process all tiles
182 Renderer::Backend::IDeviceCommandContext* deviceCommandContext) override;
183
184 // Temporary storage of tile coordinates, so ProcessTile doesn't need to
185 // pass it to RenderTile/etc (and doesn't have a chance to get it wrong)
187
189
192
194};
195
196/**
197 * Base class for texture-based terrain overlays, with an arbitrary number of
198 * texels per terrain tile, intended for debugging purposes.
199 * Subclasses must implement BuildTextureRGBA which will be called each frame.
200 */
202{
203public:
204 TerrainTextureOverlay(float texelsPerTile,
205 std::function<void(std::uint8_t*, std::size_t, std::size_t)> buildTextureRGBA);
206
208
209 /**
210 * Returns an arbitrary color, for subclasses that want to distinguish
211 * different integers visually.
212 */
213 static SColor4ub GetColor(std::size_t idx, std::uint8_t alpha);
214
215 static void OverwriteEachTile(std::uint8_t* data, const std::size_t w, const std::size_t h,
216 const auto generate)
217 {
218 static_assert(std::is_invocable_r_v<SColor4ub, decltype(generate), int, int>);
219
220 for (int i{0}; i != static_cast<int>(h); ++i)
221 {
222 for (int j{0}; j != static_cast<int>(w); ++j)
223 {
224 SColor4ub color{generate(i, j)};
225 *data++ = color.R;
226 *data++ = color.G;
227 *data++ = color.B;
228 *data++ = color.A;
229 }
230 }
231 };
232
233private:
234 void RenderAfterWater(
235 Renderer::Backend::IDeviceCommandContext* deviceCommandContext, int cullGroup) final;
236
237 std::function<void(std::uint8_t*, std::size_t, std::size_t)> m_BuildTextureRGBA;
238
240 std::unique_ptr<Renderer::Backend::ITexture> m_Texture;
241};
242
243#endif // INCLUDED_TERRAINOVERLAY
std::shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechniquePtr.h:30
Contains pointers to various 'global' objects that are needed by the simulation code,...
Definition: SimContext.h:34
Definition: Terrain.h:52
Common interface for terrain-tile-based and texture-based debug overlays.
Definition: TerrainOverlay.h:50
ITerrainOverlay(int priority)
Definition: TerrainOverlay.cpp:109
virtual void RenderAfterWater(Renderer::Backend::IDeviceCommandContext *, int)
Definition: TerrainOverlay.h:58
NONCOPYABLE(ITerrainOverlay)
virtual void RenderBeforeWater(Renderer::Backend::IDeviceCommandContext *)
Definition: TerrainOverlay.h:56
virtual ~ITerrainOverlay()
Definition: TerrainOverlay.cpp:122
static void RenderOverlaysBeforeWater(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Draw all ITerrainOverlay objects that exist and that should be drawn before water.
Definition: TerrainOverlay.cpp:131
static void RenderOverlaysAfterWater(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, int cullGroup)
Draw all ITerrainOverlay objects that exist and that should be drawn after water.
Definition: TerrainOverlay.cpp:144
Definition: IDeviceCommandContext.h:46
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:78
Base class for (relatively) simple drawing of data onto terrain tiles, intended for debugging purpose...
Definition: TerrainOverlay.h:89
ssize_t m_j
Definition: TerrainOverlay.h:186
CShaderTechniquePtr m_OverlayTechOutlineHidden
Definition: TerrainOverlay.h:191
void RenderTile(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CColor &color, bool drawHidden)
Draw a filled quad on top of the current tile.
Definition: TerrainOverlay.cpp:220
void RenderTileOutline(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CColor &color, bool drawHidden)
Draw an outlined quad on top of the current tile.
Definition: TerrainOverlay.cpp:292
CShaderTechniquePtr m_OverlayTechOutline
Definition: TerrainOverlay.h:191
CShaderTechniquePtr m_OverlayTechTile
Definition: TerrainOverlay.h:190
TerrainOverlay(const CSimContext &simContext, int priority=100)
Construct the object and register it with the global list of terrain overlays.
Definition: TerrainOverlay.cpp:159
CTerrain * m_Terrain
Definition: TerrainOverlay.h:188
virtual void GetTileExtents(ssize_t &min_i_inclusive, ssize_t &min_j_inclusive, ssize_t &max_i_inclusive, ssize_t &max_j_inclusive)
Override to limit the range over which ProcessTile will be called.
Definition: TerrainOverlay.cpp:186
virtual void EndRender()
Override to perform processing at the end of the overlay rendering, after the ProcessTile calls.
Definition: TerrainOverlay.cpp:182
CShaderTechniquePtr m_OverlayTechTileHidden
Definition: TerrainOverlay.h:190
void RenderBeforeWater(Renderer::Backend::IDeviceCommandContext *deviceCommandContext) override
Definition: TerrainOverlay.cpp:195
Renderer::Backend::IVertexInputLayout * m_VertexInputLayout
Definition: TerrainOverlay.h:193
virtual void ProcessTile(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, ssize_t i, ssize_t j)=0
Override to perform processing of each tile.
virtual void StartRender()
Override to perform processing at the start of the overlay rendering, before the ProcessTile calls.
Definition: TerrainOverlay.cpp:178
ssize_t m_i
Definition: TerrainOverlay.h:186
Base class for texture-based terrain overlays, with an arbitrary number of texels per terrain tile,...
Definition: TerrainOverlay.h:202
static SColor4ub GetColor(std::size_t idx, std::uint8_t alpha)
Returns an arbitrary color, for subclasses that want to distinguish different integers visually.
Definition: TerrainOverlay.cpp:393
std::function< void(std::uint8_t *, std::size_t, std::size_t)> m_BuildTextureRGBA
Definition: TerrainOverlay.h:237
~TerrainTextureOverlay() final
static void OverwriteEachTile(std::uint8_t *data, const std::size_t w, const std::size_t h, const auto generate)
Definition: TerrainOverlay.h:215
float m_TexelsPerTile
Definition: TerrainOverlay.h:239
void RenderAfterWater(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, int cullGroup) final
Definition: TerrainOverlay.cpp:355
TerrainTextureOverlay(float texelsPerTile, std::function< void(std::uint8_t *, std::size_t, std::size_t)> buildTextureRGBA)
Definition: TerrainOverlay.cpp:345
std::unique_ptr< Renderer::Backend::ITexture > m_Texture
Definition: TerrainOverlay.h:240
Definition: Canvas2D.h:30
Definition: ShaderDefines.cpp:43
Definition: Color.h:46
Definition: SColor.h:31
std::uint8_t R
Definition: SColor.h:32
intptr_t ssize_t
Definition: wposix_types.h:36