- moved tilemanager into its own class
[supertux.git] / src / tile_manager.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 HEADER_TILE_MANAGER_HXX
22 #define HEADER_TILE_MANAGER_HXX
23
24 #include <set>
25 #include <vector>
26 #include <string>
27
28 class Tile;
29
30 struct TileGroup
31 {
32   friend bool operator<(const TileGroup& lhs, const TileGroup& rhs)
33   { return lhs.name < rhs.name; };
34   friend bool operator>(const TileGroup& lhs, const TileGroup& rhs)
35   { return lhs.name > rhs.name; };
36
37   std::string name;
38   std::vector<int> tiles;
39 };
40
41 class TileManager
42 {
43  private:
44   TileManager();
45   ~TileManager();
46   
47   std::vector<Tile*> tiles;
48   static TileManager* instance_ ;
49   static std::set<TileGroup>* tilegroups_;
50   void load_tileset(std::string filename);
51
52   std::string current_tileset;
53   
54  public:
55   static TileManager* instance()
56   { return instance_ ? instance_ : instance_ = new TileManager(); }
57   static void destroy_instance()
58   { delete instance_; instance_ = 0; }
59
60   void draw_tile(DrawingContext& context, unsigned int id,
61       const Vector& pos, int layer);
62   
63   static std::set<TileGroup>* tilegroups() { if(!instance_) { instance_ = new TileManager(); } return tilegroups_ ? tilegroups_ : tilegroups_ = new std::set<TileGroup>; }
64   Tile& get(unsigned int id) {
65
66     if(id < tiles.size())
67       {
68         return *tiles[id]; 
69       }
70     else
71       {
72         // Never return 0, but return the 0th tile instead so that
73         // user code doesn't have to check for NULL pointers all over
74         // the place
75         return *tiles[0]; 
76       } 
77   }
78 };
79
80 #endif
81
82 /* EOF */