Removed unused TileSet member variable from Tile
[supertux.git] / src / supertux / tile.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "supertux/tile.hpp"
19
20 #include "supertux/tile_set.hpp"
21 #include "video/drawing_context.hpp"
22
23 bool Tile::draw_editor_images = false;
24
25 Tile::Tile() :
26   imagespecs(),
27   images(),
28   editor_imagespecs(),
29   editor_images(),
30   attributes(0), 
31   data(0), 
32   fps(1)
33 {
34 }
35
36 Tile::Tile(const std::vector<ImageSpec>& imagespecs_, const std::vector<ImageSpec>& editor_imagespecs_, 
37            uint32_t attributes, uint32_t data, float fps) :
38   imagespecs(imagespecs_),
39   images(),
40   editor_imagespecs(editor_imagespecs_),
41   editor_images(),
42   attributes(attributes), 
43   data(data), 
44   fps(fps)
45 {
46   correct_attributes();
47 }
48
49 Tile::~Tile()
50 {
51 }
52
53 void
54 Tile::load_images()
55 {
56   if(images.size() == 0 && imagespecs.size() != 0)
57   {
58     assert(images.size() == 0);
59     for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i != imagespecs.end(); ++i) 
60     {
61       const ImageSpec& spec = *i;
62
63       SurfacePtr surface;
64       if(spec.rect.get_width() <= 0) 
65       {
66         surface = Surface::create(spec.file);
67       }
68       else 
69       {
70         surface = Surface::create(spec.file,
71                                   Rect((int) spec.rect.p1.x,
72                                        (int) spec.rect.p1.y,
73                                        Size((int) spec.rect.get_width(),
74                                             (int) spec.rect.get_height())));
75       }
76       images.push_back(surface);
77     }
78   }
79
80   if(editor_images.size() == 0 && editor_imagespecs.size() != 0)
81   {
82     assert(editor_images.size() == 0);
83     for(std::vector<ImageSpec>::iterator i = editor_imagespecs.begin(); i != editor_imagespecs.end(); ++i) 
84     {
85       const ImageSpec& spec = *i;
86
87       SurfacePtr surface;
88       if(spec.rect.get_width() <= 0) 
89       {
90         surface = Surface::create(spec.file);
91       }
92       else 
93       {
94         surface = Surface::create(spec.file,
95                                   Rect((int) spec.rect.p1.x,
96                                        (int) spec.rect.p1.y,
97                                        Size((int) spec.rect.get_width(),
98                                             (int) spec.rect.get_height())));
99       }
100       editor_images.push_back(surface);
101     }
102   }
103 }
104
105 void
106 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
107 {
108   if(draw_editor_images) {
109     if(editor_images.size() > 1) {
110       size_t frame = size_t(game_time * fps) % editor_images.size();
111       context.draw_surface(editor_images[frame], pos, z_pos);
112       return;
113     } else if (editor_images.size() == 1) {
114       context.draw_surface(editor_images[0], pos, z_pos);
115       return;
116     }
117   }
118
119   if(images.size() > 1) {
120     size_t frame = size_t(game_time * fps) % images.size();
121     context.draw_surface(images[frame], pos, z_pos);
122   } else if (images.size() == 1) {
123     context.draw_surface(images[0], pos, z_pos);
124   }
125 }
126
127 void
128 Tile::correct_attributes()
129 {
130   //Fix little oddities in attributes (not many, currently...)
131   if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
132     attributes |= SOLID;
133     //But still be vocal about it
134     log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;
135   }
136 }
137
138 void
139 Tile::print_debug(int id) const
140 {
141   log_debug << " Tile: id " << id << ", data " << getData() << ", attributes " << getAttributes() << ":" << std::endl;
142   for(std::vector<Tile::ImageSpec>::const_iterator im = editor_imagespecs.begin(); im != editor_imagespecs.end(); ++im) 
143     log_debug << "  Editor Imagespec: file " << im->file << "; rect " << im->rect << std::endl;
144   for(std::vector<Tile::ImageSpec>::const_iterator im = imagespecs.begin(); im != imagespecs.end(); ++im) 
145     log_debug << "  Imagespec:        file " << im->file << "; rect " << im->rect << std::endl;
146 }
147
148 /* EOF */