1 // Copyright (C) 2006 Ulf Lorenz
2 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 // Copyright (C) 2007 Ole Laursen
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.
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.
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
23 #include <sigc++/trackable.h>
26 #include "rectangle.h"
28 #include "SmallTile.h"
33 //! Generates a miniature graphic of the game map.
35 * This is a base class that draws the terrain, ruins, temples, and roads
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.
46 class OverviewMap : public sigc::trackable
49 //! Default constructor.
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
54 * This constructor is not normally called by itself, usually it is
55 * called by a derived class.
60 virtual ~OverviewMap();
63 //! Draw and scale the miniature map graphic to the given size.
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
69 * This method should be called before the draw method.
71 * @param max_dimensions Two integers; the first of which dictates the
72 * width of the map graphic, and the second dictates
76 void resize(Vector<int> max_dimensions);
78 //! Draw and scale the mini map graphic to the correct size of the game map.
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.
86 //! Draw the terrain features (roads, ruins, etc) on the map.
88 * Draws the roads, cities, ruins, temples and roads onto the map in the
89 * correct aspect ratio.
91 * @param player draw the map from the given player's perspective.
94 * The aspect ratio for the terrain features is governed by the dimensions
95 * passed to the OverviewMap::resize method.
97 * This method calls the after_draw method from the derived classes.
99 void draw(Player *player);
101 //! Redraw a portion of the map graphic.
103 * This method draws the terrain (water, grass, etc) for the given
104 * portion of the screen.
106 * @note This method redraws all terrain features (roads, ruins, etc),
107 * including the features outside of the given portion.
109 * @param tiles The rectangle to redraw.
111 void redraw_tiles(Rectangle tiles);
113 //! Returns the map graphic.
115 * It only makes sense to get the surface after OverviewMap::resize and
116 * OverviewMap::draw have been called.
118 Glib::RefPtr<Gdk::Pixmap> get_surface();
121 static void draw_terrain_tile (Glib::RefPtr<Gdk::Pixmap> surf,
122 Glib::RefPtr<Gdk::GC> gc,
123 SmallTile::Pattern pattern,
127 int i, int j, bool shadowed);
129 static void draw_pixel(Glib::RefPtr<Gdk::Pixmap> surf, Glib::RefPtr<Gdk::GC> gc, int x, int y, Gdk::Color color);
132 void draw_filled_rect(int x, int y, int width, int height, Gdk::Color color);
134 void draw_rect(int x, int y, int width, int height, Gdk::Color color);
136 void draw_line( int src_x, int src_y, int dst_x, int dst_y, Gdk::Color color);
138 //! Make the map go black.
142 //! An SDL surface of the terrain without the features.
144 * This is the cached surface after the resize method was called.
145 * It is cached so that we don't have recalculate it.
147 Glib::RefPtr<Gdk::Pixmap> static_surface;
148 Glib::RefPtr<Gdk::GC> static_surface_gc;
150 //! Returns whether or not the given pixel appears sunken (Tile::SUNKEN).
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
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
160 * @param j The pixel on the vertical axis on the map graphic that
163 * @return True if the given pixel location should be shaded, false if not.
165 bool isShadowed(guint32 type, int i, int j);
167 //! Draw the given tile at the given pixel location on the map.
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.
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.
180 void draw_terrain_tile (Maptile *tile, int i, int j);
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);
187 void draw_rect(bool front, int x, int y, int width, int height, Gdk::Color color);
189 void draw_line(bool front, int src_x, int src_y, int dst_x, int dst_y, Gdk::Color color);
192 //! Every pixel on the graphic is this wide and tall.
193 double pixels_per_tile;
195 //! Maps the given point in graphic coordinates to a game map coordinate.
196 Vector<int> mapFromScreen(Vector<int> pos);
198 //! And the other way round. Map a map coordinate to a surface pixel.
199 Vector<int> mapToSurface(Vector<int> pos);
201 //! A hook method for derived classes to put features on the map.
202 virtual void after_draw();
204 //! This method draws the cities onto the map.
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.
209 * Derived classes may call this method in their after_draw method.
211 * @param all_razed Show each city as if it were razed.
213 void draw_cities(bool all_razed);
215 //! Redraw the specified region.
216 void draw_terrain_tiles(Rectangle r);
218 int calculateResizeFactor();
220 //! The surface containing the drawn map.
221 Glib::RefPtr<Gdk::Pixmap> surface;
222 Glib::RefPtr<Gdk::GC> surface_gc;
228 #endif // OVERVIEWMAP_H