First step towards multiple tilesets per tilemap. Code is very inefficient for now...
[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   std::set<TileGroup> tilegroups;
53
54   std::string tiles_path;
55
56
57 public:
58   TileManager();
59   ~TileManager();
60
61   /**
62    * Load tileset from "filename". 
63    * Import starts at the file's tile id "start", ends at tile id "end" with all loaded tile ids being offset by "offset"
64    */ 
65   void load_tileset(std::string filename, unsigned int start, unsigned int end, int offset);
66
67   const std::set<TileGroup>& get_tilegroups() const
68   {
69     return tilegroups;
70   }
71
72   const Tile* get(uint32_t id) const
73   {
74     //FIXME: Commenting out tiles in sprites.strf makes tiles.size() fail - it's being set to the first tile commented out.
75     assert(id < tiles.size());
76     Tile* tile = tiles[id];
77     if(!tile) {
78       log_warning << "Invalid tile: " << id << std::endl;
79       return tiles[0];
80     }
81
82     if(tile->images.size() == 0 && tile->imagespecs.size() != 0)
83       tile->load_images(tiles_path);
84
85     return tile;
86   }
87
88   uint32_t get_max_tileid() const
89   {
90     return tiles.size();
91   }
92
93   int get_default_width() const
94   {
95     return 32;
96   }
97
98   int get_default_height() const
99   {
100     return 32;
101   }
102 };
103
104 #endif