- did some C++ifying. let's try to follow suit :)
[supertux.git] / src / tile.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@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
19 //  02111-1307, USA.
20
21 #ifndef TILE_H
22 #define TILE_H
23
24 #include <set>
25 #include <map>
26 #include <vector>
27
28 #include "screen/texture.h"
29 #include "globals.h"
30 #include "lispreader.h"
31 #include "setup.h"
32 #include "vector.h"
33
34 class LispReader;
35
36 /**
37 Tile Class
38 */
39 class Tile
40 {
41 public:
42   Tile();
43   ~Tile();
44
45   /// parses the tile and returns it's id number
46   int read(LispReader& reader);
47
48   int id;
49
50   std::vector<Surface*> images;
51   std::vector<Surface*> editor_images;
52   
53   /// bitset for tileflags
54   enum {
55       /** solid tile that is indestructable by Tux */
56       SOLID     = 0x0001,
57       /** uni-directional solid tile */
58       UNISOLID  = 0x0002,
59       /** a brick that can be destroyed by jumping under it */
60       BRICK     = 0x0004,
61       /** an ice brick that makes tux sliding more than usual */
62       ICE       = 0x0008,
63       /** a water tile in which tux starts to swim */
64       WATER     = 0x0010,
65       /** a tile that hurts the player if he touches it */
66       SPIKE     = 0x0020,
67       /** Bonusbox, content is stored in \a data */
68       FULLBOX   = 0x0040,
69       /** Tile is a coin */
70       COIN      = 0x0080,
71       /** the level should be finished when touching a goaltile.
72        * if data is 0 then the endsequence should be triggered, if data is 1
73        * then we can finish the level instantly.
74        */
75       GOAL      = 0x0100
76   };
77
78   /** tile attributes */
79   Uint32 attributes;
80   
81   /** General purpose data attached to a tile (content of a box, type of coin)*/
82   int data;
83
84   /** Id of the tile that is going to replace this tile once it has
85       been collected or jumped at */
86   int next_tile;
87
88   int anim_speed;
89   
90   /** Draw a tile on the screen: */
91   static void draw(const Vector& pos, unsigned int c, Uint8 alpha = 255);
92
93   /// returns the width of the tile in pixels
94   int getWidth() const
95   { 
96     if(!images.size())
97       return 0;
98     return images[0]->w;
99   }
100
101   /// returns the height of the tiles in pixels
102   int getHeight() const
103   {
104     if(!images.size())
105       return 0;
106     return images[0]->h;
107   }
108 };
109
110 struct TileGroup
111 {
112   friend bool operator<(const TileGroup& lhs, const TileGroup& rhs)
113   { return lhs.name < rhs.name; };
114   friend bool operator>(const TileGroup& lhs, const TileGroup& rhs)
115   { return lhs.name > rhs.name; };
116
117   std::string name;
118   std::vector<int> tiles;
119 };
120
121 class TileManager
122 {
123  private:
124   TileManager();
125   ~TileManager();
126   
127   std::vector<Tile*> tiles;
128   static TileManager* instance_ ;
129   static std::set<TileGroup>* tilegroups_;
130   void load_tileset(std::string filename);
131
132   std::string current_tileset;
133   
134  public:
135   static TileManager* instance()
136   { return instance_ ? instance_ : instance_ = new TileManager(); }
137   static void destroy_instance()
138   { delete instance_; instance_ = 0; }
139
140   void draw_tile(DrawingContext& context, unsigned int id,
141       const Vector& pos, int layer);
142   
143   static std::set<TileGroup>* tilegroups() { if(!instance_) { instance_ = new TileManager(); } return tilegroups_ ? tilegroups_ : tilegroups_ = new std::set<TileGroup>; }
144   Tile* get(unsigned int id) {
145     if(id < tiles.size())
146       {
147         return tiles[id]; 
148       }
149     else
150       {
151         // Never return 0, but return the 0th tile instead so that
152         // user code doesn't have to check for NULL pointers all over
153         // the place
154         return tiles[0]; 
155       } 
156   }
157 };
158
159 #endif