- memleak fix and menu fix from MatzeB
[supertux.git] / src / worldmap.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef SUPERTUX_WORLDMAP_H
21 #define SUPERTUX_WORLDMAP_H
22
23 #include <vector>
24 #include <string>
25
26 #include "musicref.h"
27
28 namespace WorldMapNS {
29
30 struct Point
31 {
32   Point() : x(0), y(0) {}
33
34   Point(const Point& pos)
35     : x(pos.x), y(pos.y) {}
36
37   Point& operator=(const Point& pos)
38   { x = pos.x;
39     y = pos.y; 
40     return *this; }
41
42   Point(int x_, int y_)
43     : x(x_), y(y_) {}
44
45   int x;
46   int y;
47 };
48
49 class Tile
50 {
51 public:
52   Tile();
53   ~Tile();
54   
55   Surface* sprite;
56
57   // Directions in which Tux is allowed to walk from this tile
58   bool north;
59   bool east;
60   bool south;
61   bool west;
62
63   /** Stop on this tile or walk over it? */
64   bool stop;
65 };
66
67 class TileManager
68 {
69 private:
70   typedef std::vector<Tile*> Tiles;
71   Tiles tiles;
72
73 public:
74   TileManager();
75   ~TileManager();
76
77   Tile* get(int i);
78 };
79
80 enum Direction { NONE, WEST, EAST, NORTH, SOUTH };
81
82 std::string direction_to_string(Direction d);
83 Direction   string_to_direction(const std::string& d);
84 Direction reverse_dir(Direction d);
85
86 class WorldMap;
87
88 class Tux
89 {
90 public:
91   Direction back_direction;
92 private:
93   WorldMap* worldmap;
94   Surface* sprite;
95
96   Direction input_direction;
97   Direction direction;
98   Point tile_pos;
99   /** Length by which tux is away from its current tile, length is in
100       input_direction direction */
101   float offset;
102   bool  moving;
103
104   void stop();
105 public: 
106   Tux(WorldMap* worldmap_);
107   ~Tux();
108   
109   void draw(const Point& offset);
110   void update(float delta);
111
112   void set_direction(Direction d) { input_direction = d; }
113
114   bool is_moving() const { return moving; }
115   Point get_pos();
116   Point get_tile_pos() const { return tile_pos; } 
117   void  set_tile_pos(Point p) { tile_pos = p; } 
118 };
119
120 /** */
121 class WorldMap
122 {
123 private:
124   Tux* tux;
125
126   bool quit;
127
128   Surface* level_sprite;
129   Surface* leveldot_green;
130   Surface* leveldot_red;
131
132   std::string name;
133   std::string music;
134
135   std::vector<int> tilemap;
136   int width;
137   int height;
138
139   TileManager* tile_manager;
140
141 public:
142   struct Level
143   {
144     int x;
145     int y;
146     std::string name;
147     std::string title;
148     bool solved;
149
150     // Directions which are walkable from this level
151     bool north;
152     bool east;
153     bool south;
154     bool west;
155   };
156
157 private:
158   typedef std::vector<Level> Levels;
159   Levels levels;
160
161   MusicRef song;
162
163   Direction input_direction;
164   bool enter_level;
165
166   Point offset;
167   std::string savegame_file;
168
169   void get_level_title(Levels::pointer level);
170
171   void draw_status();
172 public:
173   WorldMap();
174   ~WorldMap();
175
176   /** Busy loop */
177   void display();
178
179   void load_map();
180   
181   void get_input();
182
183   /** Update Tux position */
184   void update();
185
186   /** Draw one frame */
187   void draw(const Point& offset);
188
189   Point get_next_tile(Point pos, Direction direction);
190   Tile* at(Point pos);
191   WorldMap::Level* at_level();
192
193   /** Check if it is possible to walk from \a pos into \a direction,
194       if possible, write the new position to \a new_pos */
195   bool path_ok(Direction direction, Point pos, Point* new_pos);
196
197   void savegame(const std::string& filename);
198   void loadgame(const std::string& filename);
199 private:
200   void on_escape_press();
201 };
202
203 } // namespace WorldMapNS
204
205 #endif
206
207 /* Local Variables: */
208 /* mode:c++ */
209 /* End: */