LCOV - code coverage report
Current view: top level - source/renderer - TerrainOverlay.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 11 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 5 0.0 %

          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             :  * 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"
      27             : #include "graphics/ShaderTechniquePtr.h"
      28             : #include "lib/code_annotation.h"
      29             : #include "lib/posix/posix_types.h"
      30             : 
      31             : #include <cstddef>
      32             : #include <functional>
      33             : #include <memory>
      34             : 
      35             : class CSimContext;
      36             : class CTerrain;
      37             : namespace Renderer::Backend { class IDeviceCommandContext; }
      38             : namespace Renderer::Backend { class ITexture; }
      39             : namespace Renderer::Backend { class IVertexInputLayout; }
      40             : struct CColor;
      41             : struct 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             :  */
      49             : class ITerrainOverlay
      50             : {
      51             :     NONCOPYABLE(ITerrainOverlay);
      52             : 
      53             : public:
      54             :     virtual ~ITerrainOverlay();
      55             : 
      56           0 :     virtual void RenderBeforeWater(Renderer::Backend::IDeviceCommandContext*) { }
      57             : 
      58           0 :     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             : 
      73             : protected:
      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             :  */
      88             : class TerrainOverlay : public ITerrainOverlay
      89             : {
      90             : protected:
      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             :      */
     168             :     void RenderTileOutline(
     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             :      */
     175             :     void RenderTileOutline(
     176             :         Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
     177             :         const CColor& color, bool drawHidden, ssize_t i, ssize_t j);
     178             : 
     179             : private:
     180             :     // Process all tiles
     181             :     void RenderBeforeWater(
     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)
     186             :     ssize_t m_i, m_j;
     187             : 
     188             :     CTerrain* m_Terrain;
     189             : 
     190             :     CShaderTechniquePtr m_OverlayTechTile, m_OverlayTechTileHidden;
     191             :     CShaderTechniquePtr m_OverlayTechOutline, m_OverlayTechOutlineHidden;
     192             : 
     193             :     Renderer::Backend::IVertexInputLayout* m_VertexInputLayout = nullptr;
     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             :  */
     201             : class TerrainTextureOverlay final : public ITerrainOverlay
     202             : {
     203             : public:
     204             :     TerrainTextureOverlay(float texelsPerTile,
     205             :         std::function<void(std::uint8_t*, std::size_t, std::size_t)> buildTextureRGBA);
     206             : 
     207             :     ~TerrainTextureOverlay() final;
     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           0 :     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           0 :         for (int i{0}; i != static_cast<int>(h); ++i)
     221             :         {
     222           0 :             for (int j{0}; j != static_cast<int>(w); ++j)
     223             :             {
     224           0 :                 SColor4ub color{generate(i, j)};
     225           0 :                 *data++ = color.R;
     226           0 :                 *data++ = color.G;
     227           0 :                 *data++ = color.B;
     228           0 :                 *data++ = color.A;
     229             :             }
     230             :         }
     231           0 :     };
     232             : 
     233             : private:
     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             : 
     239             :     float m_TexelsPerTile;
     240             :     std::unique_ptr<Renderer::Backend::ITexture> m_Texture;
     241             : };
     242             : 
     243             : #endif // INCLUDED_TERRAINOVERLAY

Generated by: LCOV version 1.16