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 "editor/editor.hpp" |
20 | #include "supertux/resources.hpp" |
21 | #include "supertux/tile.hpp" |
22 | #include "supertux/tile_set_parser.hpp" |
23 | #include "util/gettext.hpp" |
24 | #include "util/log.hpp" |
25 | #include "video/drawing_context.hpp" |
26 | #include "video/surface.hpp" |
27 | |
28 | Tilegroup::Tilegroup() : |
29 | developers_group(), |
30 | name(), |
31 | tiles() |
32 | { |
33 | } |
34 | |
35 | std::unique_ptr<TileSet> |
36 | TileSet::from_file(const std::string& filename) |
37 | { |
38 | auto tileset = std::make_unique<TileSet>(); |
39 | |
40 | TileSetParser parser(*tileset, filename); |
41 | parser.parse(); |
42 | |
43 | tileset->print_debug_info(filename); |
44 | |
45 | return tileset; |
46 | } |
47 | |
48 | TileSet::TileSet() : |
49 | m_tiles(1), |
50 | m_tilegroups() |
51 | { |
52 | m_tiles[0] = std::make_unique<Tile>(); |
53 | } |
54 | |
55 | void |
56 | TileSet::add_tile(int id, std::unique_ptr<Tile> tile) |
57 | { |
58 | if (id >= static_cast<int>(m_tiles.size())) { |
59 | m_tiles.resize(id + 1); |
60 | } |
61 | |
62 | if (m_tiles[id]) { |
63 | log_warning << "Tile with ID "<< id << " redefined"<< std::endl; |
64 | } else { |
65 | m_tiles[id] = std::move(tile); |
66 | } |
67 | } |
68 | |
69 | const Tile& |
70 | TileSet::get(const uint32_t id) const |
71 | { |
72 | if (id >= m_tiles.size()) { |
73 | // log_warning << "Invalid tile: " << id << std::endl; |
74 | return *m_tiles[0]; |
75 | } else { |
76 | assert(id < m_tiles.size()); |
77 | Tile* tile = m_tiles[id].get(); |
78 | if (tile) { |
79 | return *tile; |
80 | } else { |
81 | // log_warning << "Invalid tile: " << id << std::endl; |
82 | return *m_tiles[0]; |
83 | } |
84 | } |
85 | } |
86 | |
87 | void |
88 | TileSet::add_unassigned_tilegroup() |
89 | { |
90 | Tilegroup unassigned_group; |
91 | |
92 | unassigned_group.name = _("Others"); |
93 | unassigned_group.developers_group = true; |
94 | |
95 | for (auto tile = 0; tile < static_cast<int>(m_tiles.size()); tile++) |
96 | { |
97 | bool found = false; |
98 | for (const auto& group : m_tilegroups) |
99 | { |
100 | for (const auto& tile_in_group : group.tiles) |
101 | { |
102 | if (tile_in_group == tile) |
103 | { |
104 | found = true; |
105 | } |
106 | } |
107 | } |
108 | |
109 | // Weed out all the tiles that have an ID |
110 | // but no image (mostly tiles that act as |
111 | // spacing between other tiles). |
112 | if (found == false && m_tiles[tile].get()) |
113 | { |
114 | unassigned_group.tiles.push_back(tile); |
115 | } |
116 | } |
117 | |
118 | if (!unassigned_group.tiles.empty()) |
119 | { |
120 | m_tilegroups.push_back(unassigned_group); |
121 | } |
122 | } |
123 | |
124 | void |
125 | TileSet::add_tilegroup(const Tilegroup& tilegroup) |
126 | { |
127 | m_tilegroups.push_back(tilegroup); |
128 | } |
129 | |
130 | void |
131 | TileSet::print_debug_info(const std::string& filename) |
132 | { |
133 | if (false) |
134 | { // enable this if you want to see a list of free tiles |
135 | log_info << "Last Tile ID is "<< m_tiles.size()-1 << std::endl; |
136 | int last = -1; |
137 | for (int i = 0; i < int(m_tiles.size()); ++i) |
138 | { |
139 | if (m_tiles[i] == nullptr && last == -1) |
140 | { |
141 | last = i; |
142 | } |
143 | else if (m_tiles[i] && last != -1) |
144 | { |
145 | log_info << "Free Tile IDs ("<< i - last << "): "<< last << " - "<< i-1 << std::endl; |
146 | last = -1; |
147 | } |
148 | } |
149 | } |
150 | } |
151 | |
152 | /* EOF */ |
153 |