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