initial commit, lordsawar source, slightly modified
[lordsawar] / src / overviewmap.h
1 // Copyright (C) 2006 Ulf Lorenz
2 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 // Copyright (C) 2007 Ole Laursen
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU Library General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
18 //  02110-1301, USA.
19
20 #ifndef OVERVIEWMAP_H
21 #define OVERVIEWMAP_H
22
23 #include <sigc++/trackable.h>
24 #include <gtkmm.h>
25 #include "vector.h"
26 #include "rectangle.h"
27 #include "Tile.h"
28 #include "SmallTile.h"
29
30 class Maptile;
31 class Player;
32
33 //! Generates a miniature graphic of the game map.
34 /**
35  * This is a base class that draws the terrain, ruins, temples, and roads 
36  * onto a Gdk::Pixmap.
37  * This class is responsible for drawing terrain features using the correct
38  * pattern, as specified in Tile::Pattern.  Ruins and Temples are drawn as 
39  * white dots, and roads are drawn as brown lines.
40  * If a tile on the game map is obscured due to fog of war on a hidden map,
41  * it appears as black.
42  * This class provides a method for optionally drawing cities onto the map.
43  * Derived classes can add their own stuff to the map by overriding the 
44  * after_draw method that is called by OverviewMap::draw.
45  */
46 class OverviewMap : public sigc::trackable
47 {
48  public:
49      //! Default constructor.
50      /**
51       * Make a new overview map.  This constructor doesn't do anything, the
52       * real work is done in the OverviewMap::resize and OverviewMap::draw
53       * methods.
54       * This constructor is not normally called by itself, usually it is
55       * called by a derived class.
56       */
57      OverviewMap();
58
59      //! Destructor.
60      virtual ~OverviewMap();
61
62
63     //! Draw and scale the miniature map graphic to the given size.
64     /**
65      * This method is responsible for drawing the terrain (e.g. grass, water, 
66      * etc), but not the terrain features (roads, ruins, etc).
67      * It will draw a map that is max_dimensions large, but will keep it's
68      * aspect ratio.
69      * This method should be called before the draw method.  
70      *
71      * @param max_dimensions  Two integers; the first of which dictates the
72      *                        width of the map graphic, and the second dictates
73      *                        the height.
74      *
75      */
76     void resize(Vector<int> max_dimensions);
77
78     //! Draw and scale the mini map graphic to the correct size of the game map.
79     /**
80      * @note This method depends on the map being one of the 3 sizes as 
81      * defined in the GamePreferencesDialog.
82      * This method should be called before the draw method.
83      */
84     void resize();
85
86     //! Draw the terrain features (roads, ruins, etc) on the map.
87     /**
88      * Draws the roads, cities, ruins, temples and roads onto the map in the
89      * correct aspect ratio.
90      *
91      * @param player draw the map from the given player's perspective.
92      *
93      *
94      * The aspect ratio for the terrain features is governed by the dimensions 
95      * passed to the OverviewMap::resize method.
96      *
97      * This method calls the after_draw method from the derived classes.
98      */
99     void draw(Player *player);
100
101     //! Redraw a portion of the map graphic.
102     /**
103      * This method draws the terrain (water, grass, etc) for the given 
104      * portion of the screen.
105      *
106      * @note This method redraws all terrain features (roads, ruins, etc), 
107      *       including the features outside of the given portion.
108      *
109      * @param tiles  The rectangle to redraw.
110      */
111     void redraw_tiles(Rectangle tiles);
112
113     //! Returns the map graphic.
114     /**
115      * It only makes sense to get the surface after OverviewMap::resize and 
116      * OverviewMap::draw have been called.
117      */
118     Glib::RefPtr<Gdk::Pixmap> get_surface();
119
120
121     static void draw_terrain_tile (Glib::RefPtr<Gdk::Pixmap> surf,
122                                 Glib::RefPtr<Gdk::GC> gc,
123                                 SmallTile::Pattern pattern,
124                                 Gdk::Color first, 
125                                 Gdk::Color second,
126                                 Gdk::Color third,
127                                 int i, int j, bool shadowed);
128
129     static void draw_pixel(Glib::RefPtr<Gdk::Pixmap> surf, Glib::RefPtr<Gdk::GC> gc, int x, int y, Gdk::Color color);
130
131
132     void draw_filled_rect(int x, int y, int width, int height, Gdk::Color color);
133
134     void draw_rect(int x, int y, int width, int height, Gdk::Color color);
135
136     void draw_line( int src_x, int src_y, int dst_x, int dst_y, Gdk::Color color);
137
138     //! Make the map go black.
139     void blank(bool on);
140
141  private:
142     //! An SDL surface of the terrain without the features.
143     /**
144      * This is the cached surface after the resize method was called.
145      * It is cached so that we don't have recalculate it.
146      */
147     Glib::RefPtr<Gdk::Pixmap> static_surface;
148     Glib::RefPtr<Gdk::GC> static_surface_gc;
149
150     //! Returns whether or not the given pixel appears sunken (Tile::SUNKEN).
151     /**
152      * The leftmost and bottommost pixels of a sunken terrain type are shaded
153      * in a different colour.  This method returns whether or not a given pixel
154      * should be shaded.
155      *
156      * @param type  The tile associated with the pixel location must be of
157      *              this type.  If not, then this method always returns false.
158      * @param i     The pixel on the horizontal axis on the map graphic that 
159      *              we're querying.
160      * @param j     The pixel on the vertical axis on the map graphic that
161      *              we're querying.
162      *
163      * @return True if the given pixel location should be shaded, false if not.
164      */
165     bool isShadowed(guint32 type, int i, int j);
166
167     //! Draw the given tile at the given pixel location on the map.
168     /**
169      * This method draws a square that is Overviewmap::pixels_per_tile pixels
170      * in height and width.  It draws it at position (i,j) on the graphic.
171      * This method uses the pattern assocaited the given Maptile to know 
172      * what this box should look like.
173      *
174      * @param tile  The tile of the game map we're trying to draw.
175      * @param i     The pixel on the horizontal axis of the map graphic.  This
176      *              value must be a multiple of OverviewMap::pixels_per_tile.
177      * @param j     The pixel on the vertical axis of the map graphic.  This
178      *              value must be a multiple of OverviewMap::pixels_per_tile.
179      */
180     void draw_terrain_tile (Maptile *tile, int i, int j);
181
182
183     void choose_surface(bool front, Glib::RefPtr<Gdk::Pixmap> &surf,
184                                  Glib::RefPtr<Gdk::GC> &gc);
185     void draw_filled_rect(bool front, int x, int y, int width, int height, Gdk::Color color);
186
187     void draw_rect(bool front, int x, int y, int width, int height, Gdk::Color color);
188
189     void draw_line(bool front, int src_x, int src_y, int dst_x, int dst_y, Gdk::Color color);
190  protected:
191
192     //! Every pixel on the graphic is this wide and tall.
193     double pixels_per_tile;
194
195     //! Maps the given point in graphic coordinates to a game map coordinate.
196     Vector<int> mapFromScreen(Vector<int> pos);
197
198     //! And the other way round. Map a map coordinate to a surface pixel.
199     Vector<int> mapToSurface(Vector<int> pos);
200
201     //! A hook method for derived classes to put features on the map.
202     virtual void after_draw();
203
204     //! This method draws the cities onto the map.
205     /**
206      * Scan through all of the cities in the citylist, and draw each one with
207      * a small shield graphic belonging to the owner of that city.
208      *
209      * Derived classes may call this method in their after_draw method.
210      *
211      * @param all_razed  Show each city as if it were razed.
212      */
213     void draw_cities(bool all_razed);
214
215     //! Redraw the specified region.
216     void draw_terrain_tiles(Rectangle r);
217
218     int calculateResizeFactor();
219
220     //! The surface containing the drawn map.
221     Glib::RefPtr<Gdk::Pixmap> surface;
222     Glib::RefPtr<Gdk::GC> surface_gc;
223
224
225     bool blank_screen;
226 };
227
228 #endif // OVERVIEWMAP_H