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 4 0.0 %
Date: 2022-03-08 13:03:03 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2022 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 "renderer/backend/gl/DeviceCommandContext.h"
      27             : #include "renderer/backend/gl/Texture.h"
      28             : 
      29             : struct CColor;
      30             : struct SColor4ub;
      31             : class CTerrain;
      32             : class CSimContext;
      33             : 
      34             : /**
      35             :  * Common interface for terrain-tile-based and texture-based debug overlays.
      36             :  *
      37             :  * An overlay object will be rendered for as long as it is allocated
      38             :  * (it is automatically registered/deregistered by constructor/destructor).
      39             :  */
      40             : class ITerrainOverlay
      41             : {
      42             :     NONCOPYABLE(ITerrainOverlay);
      43             : 
      44             : public:
      45             :     virtual ~ITerrainOverlay();
      46             : 
      47           0 :     virtual void RenderBeforeWater(Renderer::Backend::GL::CDeviceCommandContext* UNUSED(deviceCommandContext)) { }
      48             : 
      49           0 :     virtual void RenderAfterWater(
      50           0 :         Renderer::Backend::GL::CDeviceCommandContext* UNUSED(deviceCommandContext), int UNUSED(cullGroup)) { }
      51             : 
      52             :     /**
      53             :      * Draw all ITerrainOverlay objects that exist
      54             :      * and that should be drawn before water.
      55             :      */
      56             :     static void RenderOverlaysBeforeWater(
      57             :         Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext);
      58             : 
      59             :     /**
      60             :      * Draw all ITerrainOverlay objects that exist
      61             :      * and that should be drawn after water.
      62             :      */
      63             :     static void RenderOverlaysAfterWater(
      64             :         Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext, int cullGroup);
      65             : 
      66             : protected:
      67             :     ITerrainOverlay(int priority);
      68             : };
      69             : 
      70             : /**
      71             :  * Base class for (relatively) simple drawing of
      72             :  * data onto terrain tiles, intended for debugging purposes and for the Atlas
      73             :  * editor (hence not trying to be very efficient).
      74             :  *
      75             :  * To start drawing a terrain overlay, first create a subclass of TerrainOverlay.
      76             :  * Override the method GetTileExtents if you want to change the range over which
      77             :  * it is drawn.
      78             :  * Override ProcessTile to do your processing for each tile, which should call
      79             :  * RenderTile and RenderTileOutline as appropriate.
      80             :  */
      81             : class TerrainOverlay : public ITerrainOverlay
      82             : {
      83             : protected:
      84             :     /**
      85             :      * Construct the object and register it with the global
      86             :      * list of terrain overlays.
      87             :      * <p>
      88             :      * The priority parameter controls the order in which overlays are drawn,
      89             :      * if several exist - they are processed in order of increasing priority,
      90             :      * so later ones draw on top of earlier ones.
      91             :      * Most should use the default of 100. Numbers from 200 are used
      92             :      * by Atlas.
      93             :      *
      94             :      * @param priority  controls the order of drawing
      95             :      */
      96             :     TerrainOverlay(const CSimContext& simContext, int priority = 100);
      97             : 
      98             :     /**
      99             :      * Override to perform processing at the start of the overlay rendering,
     100             :      * before the ProcessTile calls
     101             :      */
     102             :     virtual void StartRender();
     103             : 
     104             :     /**
     105             :      * Override to perform processing at the end of the overlay rendering,
     106             :      * after the ProcessTile calls
     107             :      */
     108             :     virtual void EndRender();
     109             : 
     110             :     /**
     111             :      * Override to limit the range over which ProcessTile will
     112             :      * be called. Defaults to the size of the map.
     113             :      *
     114             :      * @param min_i_inclusive  [output] smallest <i>i</i> coordinate, in tile-space units
     115             :      * (1 unit per tile, <i>+i</i> is world-space <i>+x</i> and game-space East)
     116             :      * @param min_j_inclusive  [output] smallest <i>j</i> coordinate
     117             :      * (<i>+j</i> is world-space <i>+z</i> and game-space North)
     118             :      * @param max_i_inclusive  [output] largest <i>i</i> coordinate
     119             :      * @param max_j_inclusive  [output] largest <i>j</i> coordinate
     120             :      */
     121             :     virtual void GetTileExtents(ssize_t& min_i_inclusive, ssize_t& min_j_inclusive,
     122             :                                 ssize_t& max_i_inclusive, ssize_t& max_j_inclusive);
     123             : 
     124             :     /**
     125             :      * Override to perform processing of each tile. Typically calls
     126             :      * RenderTile and/or RenderTileOutline.
     127             :      *
     128             :      * @param i  <i>i</i> coordinate of tile being processed
     129             :      * @param j  <i>j</i> coordinate of tile being processed
     130             :      */
     131             :     virtual void ProcessTile(
     132             :         Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
     133             :         ssize_t i, ssize_t j) = 0;
     134             : 
     135             :     /**
     136             :      * Draw a filled quad on top of the current tile.
     137             :      *
     138             :      * @param color  color to draw. May be transparent (alpha &lt; 1)
     139             :      * @param drawHidden  true if hidden tiles (i.e. those behind other tiles)
     140             :      * should be drawn
     141             :      */
     142             :     void RenderTile(
     143             :         Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
     144             :         const CColor& color, bool drawHidden);
     145             : 
     146             :     /**
     147             :      * Draw a filled quad on top of the given tile.
     148             :      */
     149             :     void RenderTile(
     150             :         Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
     151             :         const CColor& color, bool drawHidden, ssize_t i, ssize_t j);
     152             : 
     153             :     /**
     154             :      * Draw an outlined quad on top of the current tile.
     155             :      *
     156             :      * @param color  color to draw. May be transparent (alpha &lt; 1)
     157             :      * @param lineWidth  width of lines in pixels. 1 is a sensible value
     158             :      * @param drawHidden  true if hidden tiles (i.e. those behind other tiles)
     159             :      * should be drawn
     160             :      */
     161             :     void RenderTileOutline(
     162             :         Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
     163             :         const CColor& color, bool drawHidden);
     164             : 
     165             :     /**
     166             :      * Draw an outlined quad on top of the given tile.
     167             :      */
     168             :     void RenderTileOutline(
     169             :         Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext,
     170             :         const CColor& color, bool drawHidden, ssize_t i, ssize_t j);
     171             : 
     172             : private:
     173             :     // Process all tiles
     174             :     void RenderBeforeWater(
     175             :         Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext) override;
     176             : 
     177             :     // Temporary storage of tile coordinates, so ProcessTile doesn't need to
     178             :     // pass it to RenderTile/etc (and doesn't have a chance to get it wrong)
     179             :     ssize_t m_i, m_j;
     180             : 
     181             :     CTerrain* m_Terrain;
     182             : };
     183             : 
     184             : /**
     185             :  * Base class for texture-based terrain overlays, with an arbitrary number of
     186             :  * texels per terrain tile, intended for debugging purposes.
     187             :  * Subclasses must implement BuildTextureRGBA which will be called each frame.
     188             :  */
     189           0 : class TerrainTextureOverlay : public ITerrainOverlay
     190             : {
     191             : public:
     192             :     TerrainTextureOverlay(float texelsPerTile, int priority = 100);
     193             : 
     194             :     ~TerrainTextureOverlay() override;
     195             : 
     196             : protected:
     197             :     /**
     198             :      * Called each frame to generate the texture to render on the terrain.
     199             :      * @p data is w*h*4 bytes, where w and h are the terrain size multiplied
     200             :      * by texelsPerTile. @p data defaults to fully transparent, and should
     201             :      * be filled with data in RGBA order.
     202             :      */
     203             :     virtual void BuildTextureRGBA(u8* data, size_t w, size_t h) = 0;
     204             : 
     205             :     /**
     206             :      * Returns an arbitrary color, for subclasses that want to distinguish
     207             :      * different integers visually.
     208             :      */
     209             :     SColor4ub GetColor(size_t idx, u8 alpha) const;
     210             : 
     211             : private:
     212             :     void RenderAfterWater(
     213             :         Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext, int cullGroup) override;
     214             : 
     215             :     float m_TexelsPerTile;
     216             :     std::unique_ptr<Renderer::Backend::GL::CTexture> m_Texture;
     217             : };
     218             : 
     219             : #endif // INCLUDED_TERRAINOVERLAY

Generated by: LCOV version 1.13