let the TileManager always return a valid tile in case of error
[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 #include <map>
28 #include <stdint.h>
29 #include <assert.h>
30
31 class Tile;
32
33 struct TileGroup
34 {
35   friend bool operator<(const TileGroup& lhs, const TileGroup& rhs)
36   { return lhs.name < rhs.name; };
37   friend bool operator>(const TileGroup& lhs, const TileGroup& rhs)
38   { return lhs.name > rhs.name; };
39
40   std::string name;
41   std::vector<int> tiles;
42 };
43
44 class TileManager
45 {
46  private:
47   TileManager();
48   ~TileManager();
49   
50   typedef std::vector<Tile*> Tiles;
51   Tiles tiles;
52
53   static TileManager* instance_ ;
54   std::set<TileGroup> tilegroups;
55   void load_tileset(std::string filename);
56
57   std::string current_tileset;
58   
59  public:
60   static TileManager* instance()
61   { return instance_ ? instance_ : instance_ = new TileManager(); }
62   static void destroy_instance()
63   { delete instance_; instance_ = 0; }
64
65   const std::set<TileGroup>& get_tilegroups() const
66   {
67     return tilegroups;
68   }
69
70   const Tile* get(uint32_t id) const
71   {
72     assert(id < tiles.size());
73     Tile* t = tiles[id];
74     if (t) 
75       {
76         return t;
77       }
78     else
79       {
80         std::cout << "TileManager: Invalid tile: " << id << std::endl;
81         return tiles[0];
82       }
83   }
84
85   uint32_t get_max_tileid() const
86   {
87     return tiles.size();
88   }
89 };
90
91 #endif
92
93 /* EOF */