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), image(0)
36 Background::Background(const lisp::Lisp& reader)
37 : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
39 reader.get("layer", layer);
40 if(reader.get("image", imagefile)
41 && reader.get("speed", speed)) {
42 set_image(imagefile, speed);
44 std::vector<float> bkgd_top_color, bkgd_bottom_color;
45 if(reader.get_vector("top_color", bkgd_top_color) &&
46 reader.get_vector("bottom_color", bkgd_bottom_color))
47 set_gradient(Color(bkgd_top_color),
48 Color(bkgd_bottom_color));
52 Background::~Background()
58 Background::write(lisp::Writer& writer)
63 writer.start_list("background");
66 writer.write_string("image", imagefile);
67 writer.write_float("speed", speed);
68 } else if(type == GRADIENT) {
69 std::vector<float> bkgd_top_color, bkgd_bottom_color;
70 bkgd_top_color.push_back(gradient_top.red);
71 bkgd_top_color.push_back(gradient_top.green);
72 bkgd_top_color.push_back(gradient_top.blue);
73 bkgd_bottom_color.push_back(gradient_bottom.red);
74 bkgd_bottom_color.push_back(gradient_bottom.green);
75 bkgd_bottom_color.push_back(gradient_bottom.blue);
76 writer.write_float_vector("top_color", bkgd_top_color);
77 writer.write_float_vector("bottom_color", bkgd_bottom_color);
79 writer.write_int("layer", layer);
81 writer.end_list("background");
85 Background::update(float)
90 Background::set_image(const std::string& name, float speed)
93 this->imagefile = name;
97 image = new Surface(name);
101 Background::set_gradient(Color top, Color bottom)
105 gradient_bottom = bottom;
107 if (gradient_top.red > 1.0 || gradient_top.green > 1.0
108 || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
109 msg_warning("top gradient color has values above 1.0");
110 if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
111 || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
112 msg_warning("bottom gradient color has values above 1.0");
119 Background::draw(DrawingContext& context)
121 if(type == GRADIENT) {
122 context.push_transform();
123 context.set_translation(Vector(0, 0));
124 context.draw_gradient(gradient_top, gradient_bottom, layer);
125 context.pop_transform();
126 } else if(type == IMAGE) {
130 int w = (int) image->get_width();
131 int h = (int) image->get_height();
132 int sx = int(-context.get_translation().x * speed) % w - w;
133 int sy = int(-context.get_translation().y * speed) % h - h;
134 context.push_transform();
135 context.set_translation(Vector(0, 0));
136 for(int x = sx; x < SCREEN_WIDTH; x += w)
137 for(int y = sy; y < SCREEN_HEIGHT; y += h)
138 context.draw_surface(image, Vector(x, y), layer);
139 context.pop_transform();
143 IMPLEMENT_FACTORY(Background, "background");