3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Matthias Braun <matze@braunis.de
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.
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.
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 02111-1307, USA.
21 #include "background.hpp"
23 #include "video/drawing_context.hpp"
24 #include "lisp/lisp.hpp"
25 #include "lisp/writer.hpp"
26 #include "object_factory.hpp"
27 #include "resources.hpp"
31 Background::Background()
32 : type(INVALID), layer(LAYER_BACKGROUND0)
36 Background::Background(const lisp::Lisp& reader)
37 : type(INVALID), layer(LAYER_BACKGROUND0)
39 // read position, defaults to (0,0)
44 this->pos = Vector(px,py);
49 reader.get("layer", layer);
50 if(reader.get("image", imagefile) && reader.get("speed", speed)) {
51 set_image(imagefile, speed);
52 reader.get("speed-y", speed_y);
53 if (reader.get("image-top", imagefile_top)) {
54 image_top.reset(new Surface(imagefile_top));
56 if (reader.get("image-bottom", imagefile_bottom)) {
57 image_bottom.reset(new Surface(imagefile_bottom));
60 std::vector<float> bkgd_top_color, bkgd_bottom_color;
61 if(reader.get_vector("top_color", bkgd_top_color) &&
62 reader.get_vector("bottom_color", bkgd_bottom_color))
63 set_gradient(Color(bkgd_top_color),
64 Color(bkgd_bottom_color));
68 Background::~Background()
73 Background::write(lisp::Writer& writer)
78 writer.start_list("background");
81 if (image_top.get() != NULL)
82 writer.write_string("image-top", imagefile_top);
84 writer.write_string("image", imagefile);
85 if (image_bottom.get() != NULL)
86 writer.write_string("image-bottom", imagefile_bottom);
88 writer.write_float("speed", speed);
89 writer.write_float("speed-y", speed_y);
90 } else if(type == GRADIENT) {
91 std::vector<float> bkgd_top_color, bkgd_bottom_color;
92 bkgd_top_color.push_back(gradient_top.red);
93 bkgd_top_color.push_back(gradient_top.green);
94 bkgd_top_color.push_back(gradient_top.blue);
95 bkgd_bottom_color.push_back(gradient_bottom.red);
96 bkgd_bottom_color.push_back(gradient_bottom.green);
97 bkgd_bottom_color.push_back(gradient_bottom.blue);
98 writer.write_float_vector("top_color", bkgd_top_color);
99 writer.write_float_vector("bottom_color", bkgd_bottom_color);
101 writer.write_int("layer", layer);
103 writer.end_list("background");
107 Background::update(float)
112 Background::set_image(const std::string& name, float speed)
115 this->imagefile = name;
118 image.reset(new Surface(name));
122 Background::set_gradient(Color top, Color bottom)
126 gradient_bottom = bottom;
128 if (gradient_top.red > 1.0 || gradient_top.green > 1.0
129 || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
130 msg_warning("top gradient color has values above 1.0");
131 if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
132 || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
133 msg_warning("bottom gradient color has values above 1.0");
139 Background::draw(DrawingContext& context)
141 if(type == GRADIENT) {
142 context.push_transform();
143 context.set_translation(Vector(0, 0));
144 context.draw_gradient(gradient_top, gradient_bottom, layer);
145 context.pop_transform();
146 } else if(type == IMAGE) {
147 if(image.get() == NULL)
150 int w = (int) image->get_width();
151 int h = (int) image->get_height();
152 int sx = int(pos.x-context.get_translation().x * speed) % w - w;
153 int sy = int(pos.y-context.get_translation().y * speed_y) % h - h;
154 int center_image_py = int(pos.y-context.get_translation().y * speed_y);
155 int bottom_image_py = int(pos.y-context.get_translation().y * speed_y) + h;
156 context.push_transform();
157 context.set_translation(Vector(0, 0));
158 for(int x = sx; x < SCREEN_WIDTH; x += w) {
159 for(int y = sy; y < SCREEN_HEIGHT; y += h) {
160 if (image_top.get() != NULL && (y < center_image_py)) {
161 context.draw_surface(image_top.get(), Vector(x, y), layer);
164 if (image_bottom.get() != NULL && (y >= bottom_image_py)) {
165 context.draw_surface(image_bottom.get(), Vector(x, y), layer);
168 context.draw_surface(image.get(), Vector(x, y), layer);
171 context.pop_transform();
175 IMPLEMENT_FACTORY(Background, "background");