initial commit, lordsawar source, slightly modified
[lordsawar] / src / bigmap.h
1 // Copyright (C) 2003 Michael Bartl
2 // Copyright (C) 2003, 2004, 2005 Ulf Lorenz
3 // Copyright (C) 2004, 2005 Bryan Duff
4 // Copyright (C) 2004, 2005, 2006 Andrea Paternesi
5 // Copyright (C) 2006, 2007, 2008, 2009 Ben Asselstine
6 // Copyright (C) 2007 Ole Laursen
7 //
8 //  This program is free software; you can redistribute it and/or modify
9 //  it under the terms of the GNU General Public License as published by
10 //  the Free Software Foundation; either version 3 of the License, or
11 //  (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU Library General Public License for more details.
17 //
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
21 //  02110-1301, USA.
22
23 #ifndef BIGMAP_H
24 #define BIGMAP_H
25
26 #include <sigc++/signal.h>
27 #include <sigc++/trackable.h>
28 #include <sigc++/connection.h>
29 #include <string>
30 #include <gtkmm.h>
31
32 #include "vector.h"
33 #include "input-events.h"
34 #include "map-tip-position.h"
35 #include "rectangle.h"
36 #include "PixMask.h"
37
38 class Player;
39 class Stack;
40 class MapRenderer;
41 class Item;
42 class MapBackpack;
43 class City;
44 class Ruin;
45 class Signpost;
46 class Temple;
47 class Location;
48
49 /** The large map
50   * 
51   * The detailed map in which all the action takes place. Handles scrolling
52   * and mouse clicks via signals.
53   *
54   * Draws everything to a buffer to simplify scrolling, the buffer is then
55   * blitted to the screen. The current view of the map is kept track of both
56   * approximately (in tiles) and more precisely in pixels.
57   */
58 class BigMap: public sigc::trackable
59 {
60  public:
61     BigMap();
62     virtual ~BigMap();
63
64     // draw everything
65     void draw(Player *player, bool redraw_buffer = true);
66
67     // view the rectangle, measured in tiles
68     void set_view(Rectangle rect);
69     void screen_size_changed(Gtk::Allocation box);
70
71     // return a good position of a map tip given that it should be close to the
72     // tiles in tile_area without covering them
73     MapTipPosition map_tip_position(Rectangle tile_area);
74     MapTipPosition map_tip_position(Vector<int> tile);
75
76     // emitted when the view has changed because of user interactions
77     sigc::signal<void, Rectangle> view_changed;
78
79     // Emitted after a call to SmallMap::Draw.
80     /**
81      * Classes that use BigMap must catch this signal to display the map.
82      */
83     sigc::signal<void, Glib::RefPtr<Gdk::Pixmap> > map_changed;
84     void blank(bool on);
85
86     //! Save the whole map as one big image (bmp file).
87     bool saveAsBitmap(std::string filename);
88
89     //! Save the whole map, but not the game objects on top of it.
90     bool saveUnderlyingMapAsBitmap(std::string filename);
91
92     //! Save the current view as an image (bmp file).
93     bool saveViewAsBitmap(std::string filename);
94     void toggle_grid();
95     
96     Glib::RefPtr<Gdk::Pixmap> get_surface() const {return outgoing;}
97  protected:
98     MapRenderer* d_renderer;
99
100     Rectangle view;             // approximate view of screen, in tiles
101     Vector<int> view_pos;       // precise position of view in pixels
102
103     Glib::RefPtr<Gdk::Pixmap> buffer;   // the buffer we draw things in
104     Glib::RefPtr<Gdk::Pixmap> outgoing; //goes out to the gtk::image
105     Glib::RefPtr<Gdk::GC> buffer_gc;
106     Glib::RefPtr<Gdk::Pixmap> magnified_buffer; // the zoomed buffer;
107     double magnification_factor; //how much we're zoomed
108     Rectangle buffer_view;      // current view of the buffer, in tiles
109
110     bool input_locked;
111     bool blank_screen;
112
113     bool d_grid_toggled;
114     Gtk::Allocation image;
115
116     // helpers
117     Vector<int> mouse_pos_to_tile(Vector<int> pos);
118     // offset in pixels within tile
119     Vector<int> mouse_pos_to_tile_offset(Vector<int> pos); 
120     Vector<int> tile_to_buffer_pos(Vector<int> tile);
121     Vector<int> get_view_pos_from_view();
122     void draw_buffer();  
123     void blit_object(const Location &obj, Vector<int> tile, PixMask* image, Glib::RefPtr<Gdk::Pixmap> surface, Glib::RefPtr<Gdk::GC> surface_gc);
124
125     virtual void after_draw() { }
126
127  protected:
128     void draw_stack(Stack *s, Glib::RefPtr<Gdk::Pixmap> surface, Glib::RefPtr<Gdk::GC> surface_gc);
129  private:
130     void draw_buffer(Rectangle map_view, Glib::RefPtr<Gdk::Pixmap> surface, Glib::RefPtr<Gdk::GC> context);
131     void draw_buffer_tiles(Rectangle map_view, Glib::RefPtr<Gdk::Pixmap> surface, Glib::RefPtr<Gdk::GC> context);
132
133     void draw_buffer_tile(Vector<int> tile, Glib::RefPtr<Gdk::Pixmap> surface, Glib::RefPtr<Gdk::GC> context);
134     Glib::RefPtr<Gdk::Pixmap> magnify(Glib::RefPtr<Gdk::Pixmap> orig);
135     void clip_viewable_buffer(Glib::RefPtr<Gdk::Pixmap> pixmap, Glib::RefPtr<Gdk::GC> gc, Vector<int> pos, Glib::RefPtr<Gdk::Pixmap> out);
136 };
137
138 #endif