2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "object/background.hpp"
20 #include "math/sizef.hpp"
21 #include "supertux/globals.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
24 #include "util/log.hpp"
25 #include "util/reader.hpp"
27 Background::Background() :
28 alignment(NO_ALIGNMENT),
29 layer(LAYER_BACKGROUND0),
44 Background::Background(const Reader& reader) :
45 alignment(NO_ALIGNMENT),
46 layer(LAYER_BACKGROUND0),
59 // read position, defaults to (0,0)
64 this->pos = Vector(px,py);
69 std::string alignment_str;
70 if (reader.get("alignment", alignment_str))
72 if (alignment_str == "left")
74 alignment = LEFT_ALIGNMENT;
76 else if (alignment_str == "right")
78 alignment = RIGHT_ALIGNMENT;
80 else if (alignment_str == "top")
82 alignment = TOP_ALIGNMENT;
84 else if (alignment_str == "bottom")
86 alignment = BOTTOM_ALIGNMENT;
90 log_warning << "Background: invalid alignment: '" << alignment_str << "'" << std::endl;
91 alignment = NO_ALIGNMENT;
95 reader.get("scroll-offset-x", scroll_offset.x);
96 reader.get("scroll-offset-y", scroll_offset.y);
98 reader.get("scroll-speed-x", scroll_speed.x);
99 reader.get("scroll-speed-y", scroll_speed.y);
101 reader.get("layer", layer);
102 if(!reader.get("image", imagefile) || !reader.get("speed", speed))
103 throw std::runtime_error("Must specify image and speed for background");
105 set_image(imagefile, speed);
106 if (!reader.get("speed-y", speed_y))
111 if (reader.get("image-top", imagefile_top)) {
112 image_top = Surface::create(imagefile_top);
114 if (reader.get("image-bottom", imagefile_bottom)) {
115 image_bottom = Surface::create(imagefile_bottom);
119 Background::~Background()
124 Background::update(float delta)
126 scroll_offset += scroll_speed * delta;
130 Background::set_image(const std::string& name, float speed)
132 this->imagefile = name;
135 image = Surface::create(name);
139 Background::draw_image(DrawingContext& context, const Vector& pos)
141 Sizef level(Sector::current()->get_width(), Sector::current()->get_height());
142 Sizef screen(SCREEN_WIDTH, SCREEN_HEIGHT);
143 Sizef parallax_image_size = (1.0f - speed) * screen + level * speed;
145 // FIXME: Implement proper clipping here
146 int start_x = -level.width / image->get_width() / 2;
147 int end_x = level.width / image->get_width() / 2;
148 int start_y = -level.height / image->get_height() / 2;
149 int end_y = level.height / image->get_height() / 2;
154 for(int y = start_y; y < end_y; ++y)
156 Vector p(pos.x - parallax_image_size.width / 2.0f,
157 pos.y + y * image->get_height() - image->get_height() / 2.0f);
158 context.draw_surface(image.get(), p, layer);
162 case RIGHT_ALIGNMENT:
163 for(int y = start_y; y < end_y; ++y)
165 Vector p(pos.x + parallax_image_size.width / 2.0f - image->get_width(),
166 pos.y + y * image->get_height() - image->get_height() / 2.0f);
167 context.draw_surface(image.get(), p, layer);
172 for(int x = start_x; x < end_x; ++x)
174 Vector p(pos.x + x * image->get_width() - image->get_width() / 2.0f,
175 pos.y - parallax_image_size.height / 2.0f);
176 context.draw_surface(image.get(), p, layer);
180 case BOTTOM_ALIGNMENT:
181 for(int x = start_x; x < end_x; ++x)
183 Vector p(pos.x + x * image->get_width() - image->get_width() / 2.0f,
184 pos.y - image->get_height() + parallax_image_size.height / 2.0f);
185 context.draw_surface(image.get(), p, layer);
190 for(int y = start_y; y <= end_y; ++y)
191 for(int x = start_x; x <= end_x; ++x)
193 Vector p(pos.x + x * image->get_width() - image->get_width()/2,
194 pos.y + y * image->get_height() - image->get_height()/2);
196 if (image_top.get() != NULL && (y < 0))
198 context.draw_surface(image_top.get(), p, layer);
200 else if (image_bottom.get() != NULL && (y > 0))
202 context.draw_surface(image_bottom.get(), p, layer);
206 context.draw_surface(image.get(), p, layer);
214 Background::draw(DrawingContext& context)
216 if(image.get() == NULL)
219 Sizef level_size(Sector::current()->get_width(),
220 Sector::current()->get_height());
221 Sizef screen(SCREEN_WIDTH, SCREEN_HEIGHT);
222 Sizef translation_range = level_size - screen;
223 Vector center_offset(context.get_translation().x - translation_range.width / 2.0f,
224 context.get_translation().y - translation_range.height / 2.0f);
226 // FIXME: We are not handling 'pos'
227 draw_image(context, Vector(level_size.width / 2.0f, level_size.height / 2.0f) + center_offset * (1.0f - speed));