tile_set.cpp 4.3 KB

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