Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TerrainOverlay.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 "lib/ogl.h"
27 
28 struct CColor;
29 class CTerrain;
30 class CSimContext;
31 
32 /**
33  * Common interface for terrain-tile-based and texture-based debug overlays.
34  *
35  * An overlay object will be rendered for as long as it is allocated
36  * (it is automatically registered/deregistered by constructor/destructor).
37  */
39 {
41 
42 public:
43  virtual ~ITerrainOverlay();
44 
45  virtual void RenderBeforeWater() { }
46 
47  virtual void RenderAfterWater() { }
48 
49  /**
50  * Draw all ITerrainOverlay objects that exist
51  * and that should be drawn before water.
52  */
53  static void RenderOverlaysBeforeWater();
54 
55  /**
56  * Draw all ITerrainOverlay objects that exist
57  * and that should be drawn after water.
58  */
59  static void RenderOverlaysAfterWater();
60 
61 protected:
62  ITerrainOverlay(int priority);
63 };
64 
65 /**
66  * Base class for (relatively) simple drawing of
67  * data onto terrain tiles, intended for debugging purposes and for the Atlas
68  * editor (hence not trying to be very efficient).
69  *
70  * To start drawing a terrain overlay, first create a subclass of TerrainOverlay.
71  * Override the method GetTileExtents if you want to change the range over which
72  * it is drawn.
73  * Override ProcessTile to do your processing for each tile, which should call
74  * RenderTile and RenderTileOutline as appropriate.
75  */
77 {
78 protected:
79  /**
80  * Construct the object and register it with the global
81  * list of terrain overlays.
82  * <p>
83  * The priority parameter controls the order in which overlays are drawn,
84  * if several exist - they are processed in order of increasing priority,
85  * so later ones draw on top of earlier ones.
86  * Most should use the default of 100. Numbers from 200 are used
87  * by Atlas.
88  *
89  * @param priority controls the order of drawing
90  */
91  TerrainOverlay(const CSimContext& simContext, int priority = 100);
92 
93  /**
94  * Override to perform processing at the start of the overlay rendering,
95  * before the ProcessTile calls
96  */
97  virtual void StartRender();
98 
99  /**
100  * Override to perform processing at the end of the overlay rendering,
101  * after the ProcessTile calls
102  */
103  virtual void EndRender();
104 
105  /**
106  * Override to limit the range over which ProcessTile will
107  * be called. Defaults to the size of the map.
108  *
109  * @param min_i_inclusive [output] smallest <i>i</i> coordinate, in tile-space units
110  * (1 unit per tile, <i>+i</i> is world-space <i>+x</i> and game-space East)
111  * @param min_j_inclusive [output] smallest <i>j</i> coordinate
112  * (<i>+j</i> is world-space <i>+z</i> and game-space North)
113  * @param max_i_inclusive [output] largest <i>i</i> coordinate
114  * @param max_j_inclusive [output] largest <i>j</i> coordinate
115  */
116  virtual void GetTileExtents(ssize_t& min_i_inclusive, ssize_t& min_j_inclusive,
117  ssize_t& max_i_inclusive, ssize_t& max_j_inclusive);
118 
119  /**
120  * Override to perform processing of each tile. Typically calls
121  * RenderTile and/or RenderTileOutline.
122  *
123  * @param i <i>i</i> coordinate of tile being processed
124  * @param j <i>j</i> coordinate of tile being processed
125  */
126  virtual void ProcessTile(ssize_t i, ssize_t j) = 0;
127 
128  /**
129  * Draw a filled quad on top of the current tile.
130  *
131  * @param colour colour to draw. May be transparent (alpha &lt; 1)
132  * @param draw_hidden true if hidden tiles (i.e. those behind other tiles)
133  * should be drawn
134  */
135  void RenderTile(const CColor& colour, bool draw_hidden);
136 
137  /**
138  * Draw a filled quad on top of the given tile.
139  */
140  void RenderTile(const CColor& colour, bool draw_hidden, ssize_t i, ssize_t j);
141 
142  /**
143  * Draw an outlined quad on top of the current tile.
144  *
145  * @param colour colour to draw. May be transparent (alpha &lt; 1)
146  * @param line_width width of lines in pixels. 1 is a sensible value
147  * @param draw_hidden true if hidden tiles (i.e. those behind other tiles)
148  * should be drawn
149  */
150  void RenderTileOutline(const CColor& colour, int line_width, bool draw_hidden);
151 
152  /**
153  * Draw an outlined quad on top of the given tile.
154  */
155  void RenderTileOutline(const CColor& colour, int line_width, bool draw_hidden, ssize_t i, ssize_t j);
156 
157 private:
158  // Process all tiles
159  virtual void RenderBeforeWater();
160 
161  // Temporary storage of tile coordinates, so ProcessTile doesn't need to
162  // pass it to RenderTile/etc (and doesn't have a chance to get it wrong)
164 
166 };
167 
168 /**
169  * Base class for texture-based terrain overlays, with an arbitrary number of
170  * texels per terrain tile, intended for debugging purposes.
171  * Subclasses must implement BuildTextureRGBA which will be called each frame.
172  */
174 {
175 public:
176  TerrainTextureOverlay(float texelsPerTile, int priority = 100);
177 
178  virtual ~TerrainTextureOverlay();
179 
180 protected:
181  /**
182  * Called each frame to generate the texture to render on the terrain.
183  * @p data is w*h*4 bytes, where w and h are the terrain size multiplied
184  * by texelsPerTile. @p data defaults to fully transparent, and should
185  * be filled with data in RGBA order.
186  */
187  virtual void BuildTextureRGBA(u8* data, size_t w, size_t h) = 0;
188 
189 private:
190  void RenderAfterWater();
191 
193  GLuint m_Texture;
195 };
196 
197 #endif // INCLUDED_TERRAINOVERLAY
#define u8
Definition: types.h:39
virtual void RenderAfterWater()
Definition: Overlay.h:34
virtual void RenderBeforeWater()
static void RenderOverlaysBeforeWater()
Draw all ITerrainOverlay objects that exist and that should be drawn before water.
CTerrain * m_Terrain
static void RenderOverlaysAfterWater()
Draw all ITerrainOverlay objects that exist and that should be drawn after water. ...
TerrainOverlay(const CSimContext &simContext, int priority=100)
Construct the object and register it with the global list of terrain overlays.
Base class for (relatively) simple drawing of data onto terrain tiles, intended for debugging purpose...
Contains pointers to various &#39;global&#39; objects that are needed by the simulation code, to allow easy access without using real (evil) global variables.
Definition: SimContext.h:32
virtual ~ITerrainOverlay()
TerrainTextureOverlay(float texelsPerTile, int priority=100)
virtual void BuildTextureRGBA(u8 *data, size_t w, size_t h)=0
Called each frame to generate the texture to render on the terrain.
virtual void ProcessTile(ssize_t i, ssize_t j)=0
Override to perform processing of each tile.
void RenderTile(const CColor &colour, bool draw_hidden)
Draw a filled quad on top of the current tile.
Common interface for terrain-tile-based and texture-based debug overlays.
NONCOPYABLE(ITerrainOverlay)
virtual void StartRender()
Override to perform processing at the start of the overlay rendering, before the ProcessTile calls...
void RenderTileOutline(const CColor &colour, int line_width, bool draw_hidden)
Draw an outlined quad on top of the current tile.
intptr_t ssize_t
Definition: wposix_types.h:82
virtual void RenderBeforeWater()
Base class for texture-based terrain overlays, with an arbitrary number of texels per terrain tile...
ITerrainOverlay(int priority)
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.
virtual void EndRender()
Override to perform processing at the end of the overlay rendering, after the ProcessTile calls...