- added a more compact form to define tiles
[supertux.git] / src / tile_manager.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include <memory>
23 #include <stdexcept>
24 #include <sstream>
25 #include <iostream>
26 #include <assert.h>
27 #include "video/drawing_context.hpp"
28 #include "lisp/lisp.hpp"
29 #include "lisp/parser.hpp"
30 #include "lisp/list_iterator.hpp"
31 #include "tile.hpp"
32 #include "tile_manager.hpp"
33 #include "resources.hpp"
34
35 TileManager::TileManager(const std::string& filename)
36 {
37   load_tileset(filename);
38 }
39
40 TileManager::~TileManager()
41 {
42   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
43     delete *i;
44 }
45
46 void TileManager::load_tileset(std::string filename)
47 {
48   // free old tiles
49   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
50     delete *i;
51   tiles.clear();
52
53   std::string::size_type t = filename.rfind('/');
54   if(t == std::string::npos) {
55     tiles_path = "";
56   } else {
57     tiles_path = filename.substr(0, t+1);
58   }
59  
60   lisp::Parser parser;
61   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
62
63   const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
64   if(!tiles_lisp)
65     throw std::runtime_error("file is not a supertux tiles file.");
66
67   lisp::ListIterator iter(tiles_lisp);
68   while(iter.next()) {
69     if(iter.item() == "tile") {
70       Tile* tile = new Tile();
71       tile->parse(*(iter.lisp()));
72       while(tile->id >= tiles.size()) {
73         tiles.push_back(0);
74       }
75       tiles[tile->id] = tile;
76     } else if(iter.item() == "tilegroup") {
77       TileGroup tilegroup;
78       const lisp::Lisp* tilegroup_lisp = iter.lisp();
79       tilegroup_lisp->get("name", tilegroup.name);
80       tilegroup_lisp->get_vector("tiles", tilegroup.tiles);
81       tilegroups.insert(tilegroup);
82     } else if (iter.item() == "tiles") {
83       // List of ids (use 0 if the tile should be ignored)
84       std::vector<unsigned int> ids;
85       // List of attributes of the tile
86       std::vector<unsigned int> attributes;
87       std::string image;
88
89       // width and height of the image in tile units, this is used for two
90       // purposes: 
91       //  a) so we don't have to load the image here to know its dimensions
92       //  b) so that the resulting 'tiles' entry is more robust,
93       //  ie. enlarging the image won't break the tile id mapping
94       // FIXME: height is actually not used, since width might be enough for
95       // all purposes, still feels somewhat more natural this way
96       unsigned int width  = 0;
97       unsigned int height = 0;
98
99       iter.lisp()->get_vector("ids",        ids);
100       iter.lisp()->get_vector("attributes", attributes);
101       iter.lisp()->get("image",      image);
102       iter.lisp()->get("width",      width);
103       iter.lisp()->get("height",     height);
104
105       if (ids.size() != attributes.size())
106         {
107           std::ostringstream err;
108           err << "Number of ids (" << ids.size() <<  ") and attributes (" << attributes.size()
109               << ") missmatch for image '" << image << "', but must be equal";
110           throw std::runtime_error(err.str());
111         }
112
113       for(std::vector<unsigned int>::size_type i = 0; i < ids.size() && i < width*height; ++i)
114         {
115           if (ids[i])
116             {
117               if(ids[i] >= tiles.size())
118                 tiles.resize(ids[i]+1, 0);
119
120               int x = 32*(i % width);
121               int y = 32*(i / width);
122               Tile* tile = new Tile(ids[i], attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32)));
123               tiles[ids[i]] = tile;
124             }
125         }
126       
127     } else if(iter.item() == "properties") {
128       // deprecated
129     } else {
130       std::cerr << "Unknown symbol '" << iter.item() << "' tile defintion file.\n";
131     }
132   }
133 }
134