Adonthell  0.4
mapview.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 1999/2000/2001 Alexandre Courbot
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell 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 2 of the License, or
8  (at your option) any later version.
9 
10  Adonthell 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 General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 /**
21  * @file mapview.h
22  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
23  *
24  * @brief Declares the mapview class.
25  *
26  *
27  */
28 
29 
30 
31 #ifndef MAPVIEW_H_
32 #define MAPVIEW_H_
33 
34 #include "landmap.h"
35 #include "python_class.h"
36 
37 
38 /**
39  * Allows you to display a landmap on a specified area of a surface.
40  *
41  * This class just acts as a "camera" which takes snapshots of a landmap.
42  * It's size can be specified, a schedule can be set to
43  * update it's movment. Nothing prevents you from having several mapviews
44  * on the same map that displays each a different (or similar) part of the
45  * landmap.
46  *
47  */
48 class mapview : public drawable
49 {
50 
51 public:
52 
53  /**
54  * Default constructor.
55  *
56  */
57  mapview ();
58 
59  /**
60  * Destructor.
61  *
62  */
63  ~mapview ();
64 
65  /**
66  * Attach/Detach a map
67  *
68  */
69 
70  //@{
71 
72 
73  /**
74  * Sets which map this mapview will display.
75  *
76  * @param m pointer to the map that will be displayed by the mapview.
77  */
78  void attach_map (landmap * m);
79 
80  /**
81  * Stops displaying a map.
82  *
83  */
84  void detach_map ();
85 
86  //@}
87 
88 
89  /**
90  * Position settings
91  *
92  */
93 
94  //@{
95 
96  /**
97  * Sets the position of the top-left corner of the mapview on the map.
98  *
99  * You'll probably don't want to use this method. To center the mapview
100  * on a precise position, see center_on () instead.
101  *
102  * @param sm submap.
103  * @param x X position.
104  * @param y Y position.
105  * @param ox X offset.
106  * @param oy Y offset.
107  *
108  * @sa center_on ()
109  *
110  */
111  s_int8 set_pos (u_int16 sm, u_int16 x, u_int16 y, s_int16 ox = 0, s_int16 oy = 0);
112 
113  /**
114  * Sets the position of the center of the mapview on the map.
115  *
116  * @param sm submap.
117  * @param x X position.
118  * @param y Y position.
119  * @param ox X offset.
120  * @param oy Y offset.
121  *
122  */
123  s_int8 center_on (u_int16 sm, u_int16 x, u_int16 y, s_int16 ox = 0, s_int16 oy = 0);
124 
125  //@}
126 
127 
128  /**
129  * Position information
130  *
131  */
132 
133  //@{
134 
135  /**
136  * Returns the submap this mapview is displaying.
137  *
138  *
139  * @return current submap this mapview is displaying.
140  */
142  {
143  return currentsubmap_;
144  }
145 
146  /**
147  * Returns the X position of the mapview.
148  *
149  *
150  * @return X position of the mapview.
151  */
152  u_int16 posx () const
153  {
154  return posx_;
155  }
156 
157  /**
158  * Returns the Y position of the mapview.
159  *
160  *
161  * @return Y position of the mapview.
162  */
163  u_int16 posy () const
164  {
165  return posy_;
166  }
167 
168  /**
169  * Returns the X offset of the mapview.
170  *
171  *
172  * @return X offset of the mapview.
173  */
174  u_int16 offx () const
175  {
176  return offx_;
177  }
178 
179  /**
180  * Returns the Y offset of the mapview.
181  *
182  *
183  * @return Y offset of the mapview.
184  */
185  u_int16 offy () const
186  {
187  return offy_;
188  }
189 
190  //@}
191 
192  /**
193  * Basic movment
194  *
195  */
196 
197  //@{
198 
199  /**
200  * Returns whether it is possible to scroll to right. A scrolling is impossible
201  * if the mapview is at the map limits.
202  *
203  *
204  * @return true if a right scrolling is possible, false otherwise.
205  */
207  {
208  s_int32 tpx = posx () * MAPSQUARE_SIZE + offx () + length ();
209  return (tpx < m_map->submap[currentsubmap_]->area_length () * MAPSQUARE_SIZE);
210  }
211 
212  /**
213  * Returns whether it is possible to scroll to left. A scrolling is impossible
214  * if the mapview is at the map limits.
215  *
216  *
217  * @return true if a left scrolling is possible, false otherwise.
218  */
220  {
221  return (posx_ || offx_);
222  }
223 
224  /**
225  * Returns whether it is possible to scroll to up. A scrolling is impossible
226  * if the mapview is at the map limits.
227  *
228  *
229  * @return true if a up scrolling is possible, false otherwise.
230  */
232  {
233  return (posy_ || offy_);
234  }
235 
236  /**
237  * Returns whether it is possible to scroll to down. A scrolling is impossible
238  * if the mapview is at the map limits.
239  *
240  *
241  * @return true if a down scrolling is possible, false otherwise.
242  */
244  {
245  s_int32 tpy = posy () * MAPSQUARE_SIZE + offy () + height ();
246  return (tpy < m_map->submap[currentsubmap_]->area_height () * MAPSQUARE_SIZE);
247  }
248 
249  /**
250  * Scrolls 1 pixel to right, if possible.
251  *
252  */
253  void scroll_right ();
254 
255  /**
256  * Scrolls 1 pixel to left, if possible.
257  *
258  */
259  void scroll_left ();
260 
261  /**
262  * Scrolls 1 pixel to down, if possible.
263  *
264  */
265  void scroll_down ();
266 
267  /**
268  * Scrolls 1 pixel to up, if possible.
269  *
270  */
271  void scroll_up ();
272 
273  //@}
274 
275 
276  /**
277  * State saving/loading
278  *
279  */
280 
281  //@{
282 
283  /**
284  * Restore the mapview's state from an opened file.
285  *
286  * @param file the opened file from which to load the state.
287  *
288  * @return 0 in case of success, error code otherwise.
289  */
290  s_int8 get_state (igzstream& file);
291 
292  /**
293  * Saves the mapview's state into an opened file.
294  *
295  * @param file the opened file where to the state.
296  *
297  * @return 0 in case of success, error code otherwise.
298  */
299  s_int8 put_state (ogzstream& file);
300 
301  //@}
302 
303  /**
304  * Resize the mapview. The parameters are given in pixels.
305  *
306  * @param l new length.
307  * @param h new height.
308  */
309  void resize (u_int16 l, u_int16 h);
310 
311  /**
312  * Assign a schedule to the mapview.
313  *
314  * The schedule's filename will be \e "scripts/schedules/mapviews/<file>.py".
315  *
316  * @param file name of the schedule to use.
317  * @param args Python tuple containing extra arguments passed to the class constructor.
318  *
319  * @warning the args tuple argument MUST ONLY contain strings or integers, as it will
320  * be saved with the mapcharacter state by python::put_tuple ().
321  *
322  */
323  void set_schedule (string file, PyObject * args = NULL);
324 
325  /**
326  * Returns the name of the mapview's current schedule.
327  *
328  *
329  * @return name of the mapview's current schedule.
330  */
331  string schedule_file ()
332  {
333  return schedule_file_;
334  }
335 
336  /**
337  * Updates the mapview's state and launchs his schedule.
338  *
339  */
340  bool update ();
341 
342  void draw (s_int16 x, s_int16 y, const drawing_area * da_opt = NULL,
343  surface *target = NULL) const;
344 
345 private:
346  /**
347  * Forbid value passing.
348  */
349  mapview (const mapview& src);
350 
351 #ifndef SWIG
352  void draw_tile (s_int16 x, s_int16 y, const drawing_area * da_opt, surface * target,
353  list<mapsquare_tile>::iterator it) const;
354 
355  void draw_mapchar (s_int16 x, s_int16 y, const drawing_area * da_opt,
356  surface * target, list<mapsquare_char>::iterator itc) const;
357  void draw_bubble (s_int16 x, s_int16 y, const drawing_area * da_opt,
358  surface * target, list<mapcharacter *>::iterator itc) const;
359 #endif
360 
361  landmap *m_map;
362 
363  u_int16 d_length, d_height; // size of the view in map squares
364 
365  u_int16 currentsubmap_;
366  u_int16 posx_, posy_;
367  u_int16 offx_, offy_;
368 
369 
370  mutable drawing_area da;
371 
372  py_object schedule;
373 
374  PyObject * schedule_args;
375 
376  string schedule_file_;
377 };
378 
379 #endif
Class to write data from a Gzip compressed file.
Definition: fileops.h:227
#define s_int32
32 bits long signed integer
Definition: types.h:50
u_int16 length() const
Returns the length of the drawable.
Definition: drawable.h:80
Class to read data from a Gzip compressed file.
Definition: fileops.h:135
s_int8 get_state(igzstream &file)
State saving/loading.
Definition: mapview.cc:172
bool can_scroll_down()
Returns whether it is possible to scroll to down.
Definition: mapview.h:243
Python object class.
Definition: py_object.h:45
bool can_scroll_up()
Returns whether it is possible to scroll to up.
Definition: mapview.h:231
bool update()
Updates the mapview&#39;s state and launchs his schedule.
Definition: mapview.cc:267
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
Class where drawables can actually be drawn to.
Definition: surface.h:85
string schedule_file()
Returns the name of the mapview&#39;s current schedule.
Definition: mapview.h:331
void detach_map()
Stops displaying a map.
Definition: mapview.cc:57
void scroll_left()
Scrolls 1 pixel to left, if possible.
Definition: mapview.cc:124
void scroll_down()
Scrolls 1 pixel to down, if possible.
Definition: mapview.cc:137
Declares the landmap class.
void scroll_up()
Scrolls 1 pixel to up, if possible.
Definition: mapview.cc:150
bool can_scroll_right()
Basic movment.
Definition: mapview.h:206
Allows you to display a landmap on a specified area of a surface.
Definition: mapview.h:48
u_int16 posx() const
Returns the X position of the mapview.
Definition: mapview.h:152
Implements "drawing zones" for drawing operations.
Definition: drawing_area.h:54
void scroll_right()
Scrolls 1 pixel to right, if possible.
Definition: mapview.cc:111
u_int16 offy() const
Returns the Y offset of the mapview.
Definition: mapview.h:185
u_int16 height() const
Returns the height of the drawable.
Definition: drawable.h:91
#define s_int16
16 bits long signed integer
Definition: types.h:47
Defines the python class. This file is named this way so it doesn&#39;t conflicts with Python...
Map where the world takes place.
Definition: landmap.h:56
void draw(s_int16 x, s_int16 y, const drawing_area *da_opt=NULL, surface *target=NULL) const
Draw the object on the screen.
Definition: mapview.cc:274
u_int16 currentsubmap()
Position information.
Definition: mapview.h:141
void set_schedule(string file, PyObject *args=NULL)
Assign a schedule to the mapview.
Definition: mapview.cc:234
const u_int16 MAPSQUARE_SIZE
Size of a mapsquare (in pixels).
bool can_scroll_left()
Returns whether it is possible to scroll to left.
Definition: mapview.h:219
void resize(u_int16 l, u_int16 h)
Resize the mapview.
Definition: mapview.cc:163
s_int8 set_pos(u_int16 sm, u_int16 x, u_int16 y, s_int16 ox=0, s_int16 oy=0)
Position settings.
Definition: mapview.cc:64
u_int16 posy() const
Returns the Y position of the mapview.
Definition: mapview.h:163
Abstract class for drawable objects manipulation.
Definition: drawable.h:59
void attach_map(landmap *m)
Attach/Detach a map.
Definition: mapview.cc:50
mapview()
Default constructor.
Definition: mapview.cc:33
~mapview()
Destructor.
Definition: mapview.cc:44
s_int8 center_on(u_int16 sm, u_int16 x, u_int16 y, s_int16 ox=0, s_int16 oy=0)
Sets the position of the center of the mapview on the map.
Definition: mapview.cc:94
#define s_int8
8 bits long signed integer
Definition: types.h:44
s_int8 put_state(ogzstream &file)
Saves the mapview&#39;s state into an opened file.
Definition: mapview.cc:205
u_int16 offx() const
Returns the X offset of the mapview.
Definition: mapview.h:174