9d11a8f5bdad9b3e75de30c6b9b6daae9f2e7991
[supertux.git] / src / tile.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
21 #include "tile.h"
22 #include "scene.h"
23 #include "assert.h"
24
25 TileManager* TileManager::instance_  = 0;
26 std::vector<TileGroup>* TileManager::tilegroups_  = 0;
27
28 TileManager::TileManager()
29 {
30   std::string filename = datadir +  "images/tilesets/supertux.stgt";
31   load_tileset(filename);
32 }
33
34 void TileManager::load_tileset(std::string filename)
35 {
36   lisp_object_t* root_obj = lisp_read_from_file(filename);
37
38   if (!root_obj)
39     st_abort("Couldn't load file", filename);
40
41   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") == 0)
42     {
43       lisp_object_t* cur = lisp_cdr(root_obj);
44       int tileset_id = 0;
45
46       while(!lisp_nil_p(cur))
47         {
48           lisp_object_t* element = lisp_car(cur);
49
50           if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
51             {
52               std::vector<std::string> editor_filenames;
53              
54               Tile* tile = new Tile;
55               tile->id      = -1;
56               tile->solid   = false;
57               tile->brick   = false;
58               tile->ice     = false;
59               tile->water   = false;
60               tile->fullbox = false;
61               tile->distro  = false;
62               tile->data    = 0;
63               tile->next_tile  = 0;
64               tile->anim_speed = 25;
65
66               LispReader reader(lisp_cdr(element));
67               assert(reader.read_int("id",  &tile->id));
68               reader.read_bool("solid",     &tile->solid);
69               reader.read_bool("brick",     &tile->brick);
70               reader.read_bool("ice",       &tile->ice);
71               reader.read_bool("water",     &tile->water);
72               reader.read_bool("fullbox",   &tile->fullbox);
73               reader.read_bool("distro",    &tile->distro);
74               reader.read_int("data",       &tile->data);
75               reader.read_int("anim-speed", &tile->anim_speed);
76               reader.read_int("next-tile",  &tile->next_tile);
77               reader.read_string_vector("images",  &tile->filenames);
78               reader.read_string_vector("editor-images", &editor_filenames);
79
80               for(std::vector<std::string>::iterator it = tile->
81                   filenames.begin();
82                   it != tile->filenames.end();
83                   ++it)
84                 {
85                   Surface* cur_image;
86                   tile->images.push_back(cur_image);
87                   tile->images[tile->images.size()-1] = new Surface(
88                                datadir +  "images/tilesets/" + (*it),
89                                USE_ALPHA);
90                 }
91               for(std::vector<std::string>::iterator it = editor_filenames.begin();
92                   it != editor_filenames.end();
93                   ++it)
94                 {
95                   Surface* cur_image;
96                   tile->editor_images.push_back(cur_image);
97                   tile->editor_images[tile->editor_images.size()-1] = new Surface(
98                                datadir +  "images/tilesets/" + (*it),
99                                USE_ALPHA);
100                 }
101                 
102               if (tile->id + tileset_id >= int(tiles.size())
103                  )
104                 tiles.resize(tile->id + tileset_id+1);
105
106               tiles[tile->id + tileset_id] = tile;
107             }
108           else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
109             {
110               LispReader reader(lisp_cdr(element));
111               std::string filename;
112               reader.read_string("file",  &filename);
113               filename = datadir + "images/tilesets/" + filename;
114               load_tileset(filename);
115             }
116           else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0)
117             {
118               TileGroup new_;
119               if(!tilegroups_)
120                 tilegroups_ = new std::vector<TileGroup>;
121               tilegroups_->push_back(new_);
122               LispReader reader(lisp_cdr(element));
123               tilegroups_->back().name;
124               reader.read_string("name",  &tilegroups_->back().name);
125               reader.read_int_vector("tiles", &tilegroups_->back().tiles);
126             }
127           else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
128             {
129               LispReader reader(lisp_cdr(element));
130               reader.read_int("id",  &tileset_id);
131               tileset_id *= 1000;
132             }
133           else
134             {
135               puts("Unhandled symbol");
136             }
137
138           cur = lisp_cdr(cur);
139         }
140     }
141   else
142     {
143       assert(0);
144     }
145 }
146
147 void
148 Tile::draw(float x, float y, unsigned int c, Uint8 alpha)
149 {
150   if (c != 0)
151     {
152       Tile* ptile = TileManager::instance()->get(c);
153       if(ptile)
154         {
155           if(ptile->images.size() > 1)
156             {
157               ptile->images[( ((global_frame_counter*25) / ptile->anim_speed) % (ptile->images.size()))]->draw(x,y, alpha);
158             }
159           else if (ptile->images.size() == 1)
160             {
161               ptile->images[0]->draw(x,y, alpha);
162             }
163           else
164             {
165               //printf("Tile not dravable %u\n", c);
166             }
167         }
168     }
169 }
170
171 // EOF //
172