Updated addon repository URL and improved debug output on download
[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
22 #include "lisp/list_iterator.hpp"
23 #include "supertux/tile_set.hpp"
24
25 TileManager::TileManager() :
26   tilesets()
27 {
28 }
29
30 TileManager::~TileManager()
31 {
32 }
33
34 TileSet*
35 TileManager::get_tileset(const std::string &filename)
36 {
37   TileSets::const_iterator i = tilesets.find(filename);
38   if(i != tilesets.end())
39   {
40     return i->second.get();
41   }
42   else
43   {
44     std::unique_ptr<TileSet> tileset(new TileSet(filename));
45     TileSet* result = tileset.get();
46     tilesets.insert(std::make_pair(filename, std::move(tileset)));
47     return result;
48   }
49 }
50
51 std::unique_ptr<TileSet>
52 TileManager::parse_tileset_definition(const Reader& reader)
53 {
54   std::unique_ptr<TileSet> result(new TileSet);
55
56   lisp::ListIterator iter(&reader);
57   while(iter.next()) {
58     const std::string& token = iter.item();
59     if(token != "tileset") {
60       log_warning << "Skipping unrecognized token \"" << token << "\" in tileset definition" << std::endl;
61       continue;
62     }
63     const lisp::Lisp* tileset_reader = iter.lisp();
64
65     std::string file;
66     if (!tileset_reader->get("file", file)) {
67       log_warning << "Skipping tileset import without file name" << std::endl;
68       continue;
69     }
70
71     const TileSet *tileset = get_tileset(file);
72
73     uint32_t start  = 0;
74     uint32_t end    = std::numeric_limits<uint32_t>::max();
75     uint32_t offset = 0;
76     tileset_reader->get("start",  start);
77     tileset_reader->get("end",    end);
78     tileset_reader->get("offset", offset);
79
80     result->merge(tileset, start, end, offset);
81   }
82
83   return std::move(result);
84 }
85
86 /* EOF */