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