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"
29 Background::Background() :
30 alignment(NO_ALIGNMENT),
31 layer(LAYER_BACKGROUND0),
46 Background::Background(const Reader& reader) :
47 alignment(NO_ALIGNMENT),
48 layer(LAYER_BACKGROUND0),
61 // read position, defaults to (0,0)
66 this->pos = Vector(px,py);
71 std::string alignment_str;
72 if (reader.get("alignment", alignment_str))
74 if (alignment_str == "left")
76 alignment = LEFT_ALIGNMENT;
78 else if (alignment_str == "right")
80 alignment = RIGHT_ALIGNMENT;
82 else if (alignment_str == "top")
84 alignment = TOP_ALIGNMENT;
86 else if (alignment_str == "bottom")
88 alignment = BOTTOM_ALIGNMENT;
90 else if (alignment_str == "none")
92 alignment = NO_ALIGNMENT;
96 log_warning << "Background: invalid alignment: '" << alignment_str << "'" << std::endl;
97 alignment = NO_ALIGNMENT;
101 reader.get("scroll-offset-x", scroll_offset.x);
102 reader.get("scroll-offset-y", scroll_offset.y);
104 reader.get("scroll-speed-x", scroll_speed.x);
105 reader.get("scroll-speed-y", scroll_speed.y);
107 reader.get("layer", layer);
108 if(!reader.get("image", imagefile) || !reader.get("speed", speed))
109 throw std::runtime_error("Must specify image and speed for background");
111 set_image(imagefile, speed);
112 if (!reader.get("speed-y", speed_y))
117 if (reader.get("image-top", imagefile_top)) {
118 image_top = Surface::create(imagefile_top);
120 if (reader.get("image-bottom", imagefile_bottom)) {
121 image_bottom = Surface::create(imagefile_bottom);
125 Background::~Background()
130 Background::update(float delta)
132 scroll_offset += scroll_speed * delta;
136 Background::set_image(const std::string& name, float speed)
138 this->imagefile = name;
141 image = Surface::create(name);
145 Background::draw_image(DrawingContext& context, const Vector& pos)
147 Sizef level(Sector::current()->get_width(), Sector::current()->get_height());
148 Sizef screen(SCREEN_WIDTH, SCREEN_HEIGHT);
149 Sizef parallax_image_size = (1.0f - speed) * screen + level * speed;
151 // FIXME: Implement proper clipping here
152 int start_x = -parallax_image_size.width / 2.0f / image->get_width() - 1;
153 int end_x = parallax_image_size.width / 2.0f / image->get_width() + 1;
154 int start_y = -parallax_image_size.height / 2.0f / image->get_height() - 1;
155 int end_y = parallax_image_size.height / 2.0f / image->get_height() + 1;
160 for(int y = start_y; y <= end_y; ++y)
162 Vector p(pos.x - parallax_image_size.width / 2.0f,
163 pos.y + y * image->get_height() - image->get_height() / 2.0f);
164 context.draw_surface(image, p, layer);
168 case RIGHT_ALIGNMENT:
169 for(int y = start_y; y <= end_y; ++y)
171 Vector p(pos.x + parallax_image_size.width / 2.0f - image->get_width(),
172 pos.y + y * image->get_height() - image->get_height() / 2.0f);
173 context.draw_surface(image, p, layer);
178 for(int x = start_x; x <= end_x; ++x)
180 Vector p(pos.x + x * image->get_width() - image->get_width() / 2.0f,
181 pos.y - parallax_image_size.height / 2.0f);
182 context.draw_surface(image, p, layer);
186 case BOTTOM_ALIGNMENT:
187 for(int x = start_x; x <= end_x; ++x)
189 Vector p(pos.x + x * image->get_width() - image->get_width() / 2.0f,
190 pos.y - image->get_height() + parallax_image_size.height / 2.0f);
191 context.draw_surface(image, p, layer);
196 for(int y = start_y; y <= end_y; ++y)
197 for(int x = start_x; x <= end_x; ++x)
199 Vector p(pos.x + x * image->get_width() - image->get_width()/2,
200 pos.y + y * image->get_height() - image->get_height()/2);
202 if (image_top.get() != NULL && (y < 0))
204 context.draw_surface(image_top, p, layer);
206 else if (image_bottom.get() != NULL && (y > 0))
208 context.draw_surface(image_bottom, p, layer);
212 context.draw_surface(image, p, layer);
220 Background::draw(DrawingContext& context)
222 if(image.get() == NULL)
225 Sizef level_size(Sector::current()->get_width(),
226 Sector::current()->get_height());
227 Sizef screen(SCREEN_WIDTH, SCREEN_HEIGHT);
228 Sizef translation_range = level_size - screen;
229 Vector center_offset(context.get_translation().x - translation_range.width / 2.0f,
230 context.get_translation().y - translation_range.height / 2.0f);
232 // FIXME: We are not handling 'pos'
233 draw_image(context, Vector(level_size.width / 2.0f, level_size.height / 2.0f) + center_offset * (1.0f - speed));