Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / supertux / tile_manager.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "supertux/tile_manager.hpp"
19
20 #include <limits>
21 #include <memory>
22
23 #include "lisp/list_iterator.hpp"
24 #include "supertux/tile_set.hpp"
25
26 TileManager* tile_manager    = NULL;
27 TileSet*     current_tileset = NULL;
28
29 TileManager::TileManager()
30 {
31 }
32
33 TileManager::~TileManager()
34 {
35 }
36
37 TileSet* TileManager::get_tileset(const std::string &filename)
38 {
39   TileSets::const_iterator i = tilesets.find(filename);
40   if(i != tilesets.end())
41     return i->second;
42
43   std::auto_ptr<TileSet> tileset (new TileSet(filename));
44   tilesets.insert(std::make_pair(filename, tileset.get()));
45
46   return tileset.release();
47 }
48
49 TileSet* TileManager::parse_tileset_definition(const Reader& reader)
50 {
51   std::auto_ptr<TileSet> result(new TileSet());
52
53   lisp::ListIterator iter(&reader);
54   while(iter.next()) {
55     const std::string& token = iter.item();
56     if(token != "tileset") {
57       log_warning << "Skipping unrecognized token \"" << token << "\" in tileset definition" << std::endl;
58       continue;
59     }
60     const lisp::Lisp* tileset_reader = iter.lisp();
61
62     std::string file; 
63     if (!tileset_reader->get("file", file)) {
64       log_warning << "Skipping tileset import without file name" << std::endl;
65       continue;
66     }
67
68     const TileSet *tileset = get_tileset(file);
69
70     uint32_t start  = 0;
71     uint32_t end    = std::numeric_limits<uint32_t>::max();
72     uint32_t offset = 0;
73     tileset_reader->get("start",  start);
74     tileset_reader->get("end",    end);
75     tileset_reader->get("offset", offset);
76
77     result->merge(tileset, start, end, offset);
78   }
79
80   return result.release();
81 }
82
83 /* EOF */