From e9a23a4eb6bf0709c253416c5443e188a4b2f338 Mon Sep 17 00:00:00 2001 From: grumbel Date: Sun, 6 Dec 2009 00:52:59 +0000 Subject: [PATCH] Moved Tile parsing code into TileSetParser git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6173 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- src/supertux/tile.cpp | 100 ------------------------------------- src/supertux/tile.hpp | 12 ++--- src/supertux/tile_manager.cpp | 1 - src/supertux/tile_set.cpp | 7 --- src/supertux/tile_set.hpp | 2 + src/supertux/tile_set_parser.cpp | 104 +++++++++++++++++++++++++++++++++++++-- src/supertux/tile_set_parser.hpp | 8 +++ 7 files changed, 114 insertions(+), 120 deletions(-) diff --git a/src/supertux/tile.cpp b/src/supertux/tile.cpp index 65a74c5c1..419de8e4f 100644 --- a/src/supertux/tile.cpp +++ b/src/supertux/tile.cpp @@ -18,8 +18,6 @@ #include "supertux/tile.hpp" #include "supertux/tile_set.hpp" -#include "supertux/timer.hpp" -#include "util/reader.hpp" #include "video/drawing_context.hpp" Tile::Tile(const TileSet& new_tileset) : @@ -55,104 +53,6 @@ Tile::~Tile() } } -uint32_t -Tile::parse(const Reader& reader) -{ - uint32_t id; - if(!reader.get("id", id)) { - throw std::runtime_error("Missing tile-id."); - } - - bool value = false; - if(reader.get("solid", value) && value) - attributes |= SOLID; - if(reader.get("unisolid", value) && value) - attributes |= UNISOLID | SOLID; - if(reader.get("brick", value) && value) - attributes |= BRICK; - if(reader.get("ice", value) && value) - attributes |= ICE; - if(reader.get("water", value) && value) - attributes |= WATER; - if(reader.get("hurts", value) && value) - attributes |= HURTS; - if(reader.get("fire", value) && value) - attributes |= FIRE; - if(reader.get("fullbox", value) && value) - attributes |= FULLBOX; - if(reader.get("coin", value) && value) - attributes |= COIN; - if(reader.get("goal", value) && value) - attributes |= GOAL; - - if(reader.get("north", value) && value) - data |= WORLDMAP_NORTH; - if(reader.get("south", value) && value) - data |= WORLDMAP_SOUTH; - if(reader.get("west", value) && value) - data |= WORLDMAP_WEST; - if(reader.get("east", value) && value) - data |= WORLDMAP_EAST; - if(reader.get("stop", value) && value) - data |= WORLDMAP_STOP; - - reader.get("data", data); - reader.get("anim-fps", anim_fps); - - if(reader.get("slope-type", data)) { - attributes |= SOLID | SLOPE; - } - - const lisp::Lisp* images; -#ifndef NDEBUG - images = reader.get_lisp("editor-images"); - if(images) - parse_images(*images); - else { -#endif - images = reader.get_lisp("images"); - if(images) - parse_images(*images); -#ifndef NDEBUG - } -#endif - - correct_attributes(); - return id; -} - -void -Tile::parse_images(const Reader& images_lisp) -{ - const lisp::Lisp* list = &images_lisp; - while(list) { - const lisp::Lisp* cur = list->get_car(); - if(cur->get_type() == lisp::Lisp::TYPE_STRING) { - std::string file; - cur->get(file); - imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0))); - } else if(cur->get_type() == lisp::Lisp::TYPE_CONS && - cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL && - cur->get_car()->get_symbol() == "region") { - const lisp::Lisp* ptr = cur->get_cdr(); - - std::string file; - float x = 0, y = 0, w = 0, h = 0; - ptr->get_car()->get(file); ptr = ptr->get_cdr(); - ptr->get_car()->get(x); ptr = ptr->get_cdr(); - ptr->get_car()->get(y); ptr = ptr->get_cdr(); - ptr->get_car()->get(w); ptr = ptr->get_cdr(); - ptr->get_car()->get(h); - imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h))); - } else { - log_warning << "Expected string or list in images tag" << std::endl; - continue; - } - - list = list->get_cdr(); - } -} - void Tile::load_images() { diff --git a/src/supertux/tile.hpp b/src/supertux/tile.hpp index 2611a8a51..66ebc0ffb 100644 --- a/src/supertux/tile.hpp +++ b/src/supertux/tile.hpp @@ -18,8 +18,8 @@ #ifndef HEADER_SUPERTUX_SUPERTUX_TILE_HPP #define HEADER_SUPERTUX_SUPERTUX_TILE_HPP -#include #include +#include #include "math/rect.hpp" #include "video/surface.hpp" @@ -28,12 +28,11 @@ class TileSet; class DrawingContext; -/** - Tile Class -*/ class Tile { public: + friend class TileSetParser; + /// bitset for tile attributes enum { /** solid tile that is indestructible by Tux */ @@ -151,14 +150,9 @@ public: } } - /// parses the tile and returns it's id number - uint32_t parse(const Reader& reader); - void print_debug(int id) const; private: - void parse_images(const Reader& cur); - //Correct small oddities in attributes that naive people //might miss (and rebuke them for it) void correct_attributes(); diff --git a/src/supertux/tile_manager.cpp b/src/supertux/tile_manager.cpp index ac31395e0..49c2cd6c7 100644 --- a/src/supertux/tile_manager.cpp +++ b/src/supertux/tile_manager.cpp @@ -18,7 +18,6 @@ #include "supertux/tile_manager.hpp" #include -#include #include "lisp/list_iterator.hpp" #include "supertux/tile_set.hpp" diff --git a/src/supertux/tile_set.cpp b/src/supertux/tile_set.cpp index f1a326e2f..42a48ec87 100644 --- a/src/supertux/tile_set.cpp +++ b/src/supertux/tile_set.cpp @@ -16,13 +16,6 @@ #include "supertux/tile_set.hpp" -#include -#include -#include - -#include "lisp/list_iterator.hpp" -#include "lisp/parser.hpp" -#include "util/file_system.hpp" #include "supertux/tile_set_parser.hpp" TileSet::TileSet() : diff --git a/src/supertux/tile_set.hpp b/src/supertux/tile_set.hpp index 54f9b5978..a5085c526 100644 --- a/src/supertux/tile_set.hpp +++ b/src/supertux/tile_set.hpp @@ -22,6 +22,8 @@ #include "supertux/tile.hpp" #include "util/log.hpp" +class Tile; + class TileSet { private: diff --git a/src/supertux/tile_set_parser.cpp b/src/supertux/tile_set_parser.cpp index 8a11e52c3..68c4d37a3 100644 --- a/src/supertux/tile_set_parser.cpp +++ b/src/supertux/tile_set_parser.cpp @@ -20,11 +20,10 @@ #include #include -#include "lisp/parser.hpp" #include "lisp/list_iterator.hpp" +#include "lisp/parser.hpp" #include "supertux/tile_set.hpp" #include "util/file_system.hpp" -#include "util/reader.hpp" TileSetParser::TileSetParser(TileSet& tileset, const std::string& filename) : m_tileset(tileset), @@ -51,7 +50,7 @@ TileSetParser::parse() while(iter.next()) { if(iter.item() == "tile") { std::auto_ptr tile(new Tile(m_tileset)); - uint32_t id = tile->parse(*(iter.lisp())); + uint32_t id = parse_tile(*tile, *iter.lisp()); if(id >= m_tileset.tiles.size()) m_tileset.tiles.resize(id+1, 0); @@ -148,4 +147,103 @@ TileSetParser::parse() } } +uint32_t +TileSetParser::parse_tile(Tile& tile, const Reader& reader) +{ + uint32_t id; + if(!reader.get("id", id)) { + throw std::runtime_error("Missing tile-id."); + } + + bool value = false; + if(reader.get("solid", value) && value) + tile.attributes |= Tile::SOLID; + if(reader.get("unisolid", value) && value) + tile.attributes |= Tile::UNISOLID | Tile::SOLID; + if(reader.get("brick", value) && value) + tile.attributes |= Tile::BRICK; + if(reader.get("ice", value) && value) + tile.attributes |= Tile::ICE; + if(reader.get("water", value) && value) + tile.attributes |= Tile::WATER; + if(reader.get("hurts", value) && value) + tile.attributes |= Tile::HURTS; + if(reader.get("fire", value) && value) + tile.attributes |= Tile::FIRE; + if(reader.get("fullbox", value) && value) + tile.attributes |= Tile::FULLBOX; + if(reader.get("coin", value) && value) + tile.attributes |= Tile::COIN; + if(reader.get("goal", value) && value) + tile.attributes |= Tile::GOAL; + + if(reader.get("north", value) && value) + tile.data |= Tile::WORLDMAP_NORTH; + if(reader.get("south", value) && value) + tile.data |= Tile::WORLDMAP_SOUTH; + if(reader.get("west", value) && value) + tile.data |= Tile::WORLDMAP_WEST; + if(reader.get("east", value) && value) + tile.data |= Tile::WORLDMAP_EAST; + if(reader.get("stop", value) && value) + tile.data |= Tile::WORLDMAP_STOP; + + reader.get("data", tile.data); + reader.get("anim-fps", tile.anim_fps); + + if(reader.get("slope-type", tile.data)) { + tile.attributes |= Tile::SOLID | Tile::SLOPE; + } + + const lisp::Lisp* images; +#ifndef NDEBUG + images = reader.get_lisp("editor-images"); + if(images) + parse_images(tile, *images); + else { +#endif + images = reader.get_lisp("images"); + if(images) + parse_images(tile, *images); +#ifndef NDEBUG + } +#endif + + tile.correct_attributes(); + + return id; +} + +void +TileSetParser::parse_images(Tile& tile, const Reader& images_lisp) +{ + const lisp::Lisp* list = &images_lisp; + while(list) { + const lisp::Lisp* cur = list->get_car(); + if(cur->get_type() == lisp::Lisp::TYPE_STRING) { + std::string file; + cur->get(file); + tile.imagespecs.push_back(Tile::ImageSpec(file, Rect(0, 0, 0, 0))); + } else if(cur->get_type() == lisp::Lisp::TYPE_CONS && + cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL && + cur->get_car()->get_symbol() == "region") { + const lisp::Lisp* ptr = cur->get_cdr(); + + std::string file; + float x = 0, y = 0, w = 0, h = 0; + ptr->get_car()->get(file); ptr = ptr->get_cdr(); + ptr->get_car()->get(x); ptr = ptr->get_cdr(); + ptr->get_car()->get(y); ptr = ptr->get_cdr(); + ptr->get_car()->get(w); ptr = ptr->get_cdr(); + ptr->get_car()->get(h); + tile.imagespecs.push_back(Tile::ImageSpec(file, Rect(x, y, x+w, y+h))); + } else { + log_warning << "Expected string or list in images tag" << std::endl; + continue; + } + + list = list->get_cdr(); + } +} + /* EOF */ diff --git a/src/supertux/tile_set_parser.hpp b/src/supertux/tile_set_parser.hpp index 71c2482be..04adbda9b 100644 --- a/src/supertux/tile_set_parser.hpp +++ b/src/supertux/tile_set_parser.hpp @@ -19,8 +19,12 @@ #define HEADER_SUPERTUX_SUPERTUX_TILE_SET_PARSER_HPP #include +#include + +#include "util/reader_fwd.hpp" class TileSet; +class Tile; class TileSetParser { @@ -34,6 +38,10 @@ public: void parse(); private: + uint32_t parse_tile(Tile& tile, const Reader& reader); + void parse_images(Tile& tile, const Reader& cur); + +private: TileSetParser(const TileSetParser&); TileSetParser& operator=(const TileSetParser&); }; -- 2.11.0