fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / tile_manager.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21
22 #ifndef HEADER_TILE_MANAGER_HXX
23 #define HEADER_TILE_MANAGER_HXX
24
25 #include <set>
26 #include <vector>
27 #include <string>
28 #include <map>
29 #include <iostream>
30 #include <stdint.h>
31 #include <assert.h>
32 #include "log.hpp"
33 #include "tile.hpp"
34
35 struct TileGroup
36 {
37   friend bool operator<(const TileGroup& lhs, const TileGroup& rhs)
38   { return lhs.name < rhs.name; };
39   friend bool operator>(const TileGroup& lhs, const TileGroup& rhs)
40   { return lhs.name > rhs.name; };
41
42   std::string name;
43   std::vector<int> tiles;
44 };
45
46 class TileManager
47 {
48 private:
49   typedef std::vector<Tile*> Tiles;
50   Tiles tiles;
51
52   static TileManager* instance_ ;
53   std::set<TileGroup> tilegroups;
54
55   std::string tiles_path;
56
57   void load_tileset(std::string filename);
58
59 public:
60   TileManager(const std::string& filename);
61   ~TileManager();
62
63   const std::set<TileGroup>& get_tilegroups() const
64   {
65     return tilegroups;
66   }
67
68   const Tile* get(uint32_t id) const
69   {
70     //FIXME: Commenting out tiles in sprites.strf makes tiles.size() fail - it's being set to the first tile commented out.
71     assert(id < tiles.size());
72     Tile* tile = tiles[id];
73     if(!tile) {
74       log_warning << "Invalid tile: " << id << std::endl;
75       return tiles[0];
76     }
77
78     if(tile->images.size() == 0 && tile->imagespecs.size() != 0)
79       tile->load_images(tiles_path);
80
81     return tile;
82   }
83
84   uint32_t get_max_tileid() const
85   {
86     return tiles.size();
87   }
88
89   int get_default_width() const
90   {
91     return 32;
92   }
93
94   int get_default_height() const
95   {
96     return 32;
97   }
98 };
99
100 extern TileManager* tile_manager;
101
102 #endif