4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
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.
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.
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
22 #include "video/drawing_context.h"
23 #include "app/setup.h"
24 #include "app/globals.h"
25 #include "utils/lispreader.h"
27 #include "tile_manager.h"
30 TileManager* TileManager::instance_ = 0;
31 std::set<TileGroup>* TileManager::tilegroups_ = 0;
33 TileManager::TileManager()
35 std::string filename = datadir + "/images/tilesets/supertux.stgt";
36 load_tileset(filename);
39 TileManager::~TileManager()
41 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
47 void TileManager::load_tileset(std::string filename)
49 if(filename == current_tileset)
53 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
57 lisp_object_t* root_obj = lisp_read_from_file(filename);
60 Termination::abort("Couldn't load file", filename);
62 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") == 0)
64 lisp_object_t* cur = lisp_cdr(root_obj);
67 while(!lisp_nil_p(cur))
69 lisp_object_t* element = lisp_car(cur);
71 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
73 LispReader reader(lisp_cdr(element));
75 Tile* tile = new Tile;
76 int tile_id = tile->read(reader);
79 << "Warning: parse error when reading a tile (id < 0), skipping.\n";
83 tiles[tile_id] = tile;
85 else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
87 LispReader reader(lisp_cdr(element));
89 reader.read_string("file", filename);
90 filename = datadir + "/images/tilesets/" + filename;
91 load_tileset(filename);
93 else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0)
96 LispReader reader(lisp_cdr(element));
97 reader.read_string("name", new_.name);
98 reader.read_int_vector("tiles", new_.tiles);
100 tilegroups_ = new std::set<TileGroup>;
101 tilegroups_->insert(new_).first;
103 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
105 LispReader reader(lisp_cdr(element));
106 reader.read_int("id", tileset_id);
111 std::cerr << "Unknown symbol: " <<
112 lisp_symbol(lisp_car(element)) << "\n";
124 current_tileset = filename;
128 TileManager::draw_tile(DrawingContext& context, unsigned int c,
129 const Vector& pos, int layer)
136 if(!tile->images.size())
139 if(tile->images.size() > 1)
142 = ((global_frame_counter*25) / tile->anim_speed) % tile->images.size();
143 context.draw_surface(tile->images[frame], pos, layer);
145 else if (tile->images.size() == 1)
147 context.draw_surface(tile->images[0], pos, layer);
152 TileManager::get(unsigned int id)
154 Tiles::iterator i = tiles.find(id);
158 std::cerr << "Warning: Asked for a non-existing tile id. Ignoring.\n";
159 // Never return 0, but return the first tile instead so that
160 // user code doesn't have to check for NULL pointers all over