42a48ec87d83c14ffeeedf42deeb80293c693e74
[supertux.git] / src / supertux / tile_set.cpp
1 //  SuperTux
2 //  Copyright (C) 2008 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/tile_set.hpp"
18
19 #include "supertux/tile_set_parser.hpp"
20
21 TileSet::TileSet() :
22   tiles(),
23   tiles_path(), 
24   tiles_loaded(false)
25 {
26   tiles.resize(1, 0);
27   tiles[0] = new Tile(*this);
28 }
29
30 TileSet::TileSet(const std::string& filename) :
31   tiles(),
32   tiles_path(), 
33   tiles_loaded(true)
34 {
35   TileSetParser parser(*this, filename);
36   parser.parse();
37
38   if (0)
39   { // enable this if you want to see a list of free tiles
40     log_info << "Last Tile ID is " << tiles.size()-1 << std::endl;
41     int last = -1;
42     for(int i = 0; i < int(tiles.size()); ++i)
43     {
44       if (tiles[i] == 0 && last == -1)
45       {
46         last = i;
47       }
48       else if (tiles[i] && last != -1)
49       {
50         log_info << "Free Tile IDs (" << i - last << "): " << last << " - " << i-1 << std::endl;
51         last = -1;
52       }
53     }
54   }
55
56   if (0)
57   { // enable this to dump the (large) list of tiles to log_debug
58     // Two dumps are identical iff the tilesets specify identical tiles
59     log_debug << "Tileset in " << filename << std::endl;
60     for(int i = 0; i < int(tiles.size()); ++i)
61     {
62       if(tiles[i] != 0)
63       {
64         tiles[i]->print_debug(i);
65       }
66     }
67   }
68 }
69
70 TileSet::~TileSet()
71 {
72   if(tiles_loaded) {
73     for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
74       delete *i;
75   }
76 }
77
78 void TileSet::merge(const TileSet *tileset, uint32_t start, uint32_t end,
79                     uint32_t offset)
80 {
81   for(uint32_t id = start; id <= end && id < tileset->tiles.size(); ++id) {
82     uint32_t dest_id = id - start + offset;
83
84     if(dest_id >= tiles.size())
85       tiles.resize(dest_id + 1, 0);
86
87     if(dest_id == 0)
88       continue;
89
90     Tile *tile = tileset->tiles[id];
91     if(tile == NULL)
92       continue;
93
94     if(tiles[dest_id] != NULL) {
95       log_warning << "tileset merge resulted in multiple definitions for id "
96                   << dest_id << "(originally " << id << ")" << std::endl;
97     }
98     tiles[dest_id] = tile;
99   }
100 }
101
102 /* EOF */