Possible fix for coverity #29375
[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_loaded(false)
24 {
25   tiles.resize(1, 0);
26   tiles[0] = new Tile();
27 }
28
29 TileSet::TileSet(const std::string& filename) :
30   tiles(),
31   tiles_loaded(true)
32 {
33   TileSetParser parser(*this, filename);
34   parser.parse();
35
36   if (0)
37   { // enable this if you want to see a list of free tiles
38     log_info << "Last Tile ID is " << tiles.size()-1 << std::endl;
39     int last = -1;
40     for(int i = 0; i < int(tiles.size()); ++i)
41     {
42       if (tiles[i] == 0 && last == -1)
43       {
44         last = i;
45       }
46       else if (tiles[i] && last != -1)
47       {
48         log_info << "Free Tile IDs (" << i - last << "): " << last << " - " << i-1 << std::endl;
49         last = -1;
50       }
51     }
52   }
53
54   if (0)
55   { // enable this to dump the (large) list of tiles to log_debug
56     // Two dumps are identical iff the tilesets specify identical tiles
57     log_debug << "Tileset in " << filename << std::endl;
58     for(int i = 0; i < int(tiles.size()); ++i)
59     {
60       if(tiles[i] != 0)
61       {
62         tiles[i]->print_debug(i);
63       }
64     }
65   }
66 }
67
68 TileSet::~TileSet()
69 {
70   if(tiles_loaded) {
71     for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
72       delete *i;
73   }
74 }
75
76 void TileSet::merge(const TileSet *tileset, uint32_t start, uint32_t end,
77                     uint32_t offset)
78 {
79   for(uint32_t id = start; id <= end && id < tileset->tiles.size(); ++id) {
80     uint32_t dest_id = id - start + offset;
81
82     if(dest_id >= tiles.size())
83       tiles.resize(dest_id + 1, 0);
84
85     if(dest_id == 0)
86       continue;
87
88     Tile *tile = tileset->tiles[id];
89     if(tile == NULL)
90       continue;
91
92     if(tiles[dest_id] != NULL) {
93       log_warning << "tileset merge resulted in multiple definitions for id "
94                   << dest_id << "(originally " << id << ")" << std::endl;
95     }
96     tiles[dest_id] = tile;
97   }
98 }
99
100 /* EOF */