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"
30 Background::Background()
31 : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
35 Background::Background(const lisp::Lisp& reader)
36 : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
38 reader.get("layer", layer);
39 if(reader.get("image", imagefile)
40 && reader.get("speed", speed)) {
41 set_image(imagefile, speed);
43 std::vector<float> bkgd_top_color, bkgd_bottom_color;
44 if(reader.get_vector("top_color", bkgd_top_color) &&
45 reader.get_vector("bottom_color", bkgd_bottom_color))
46 set_gradient(Color(bkgd_top_color),
47 Color(bkgd_bottom_color));
51 Background::~Background()
57 Background::write(lisp::Writer& writer)
62 writer.start_list("background");
65 writer.write_string("image", imagefile);
66 writer.write_float("speed", speed);
67 } else if(type == GRADIENT) {
68 std::vector<float> bkgd_top_color, bkgd_bottom_color;
69 bkgd_top_color.push_back(gradient_top.red);
70 bkgd_top_color.push_back(gradient_top.green);
71 bkgd_top_color.push_back(gradient_top.blue);
72 bkgd_bottom_color.push_back(gradient_bottom.red);
73 bkgd_bottom_color.push_back(gradient_bottom.green);
74 bkgd_bottom_color.push_back(gradient_bottom.blue);
75 writer.write_float_vector("top_color", bkgd_top_color);
76 writer.write_float_vector("bottom_color", bkgd_bottom_color);
78 writer.write_int("layer", layer);
80 writer.end_list("background");
84 Background::update(float)
89 Background::set_image(const std::string& name, float speed)
92 this->imagefile = name;
96 image = new Surface("images/background/" + name);
100 Background::set_gradient(Color top, Color bottom)
104 gradient_bottom = bottom;
106 if (gradient_top.red > 1.0 || gradient_top.green > 1.0
107 || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
108 std::cerr << "Warning: top gradient color has values above 1.0." << std::endl;
109 if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
110 || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
111 std::cerr << "Warning: bottom gradient color has values above 1.0." << std::endl;
118 Background::draw(DrawingContext& context)
120 if(type == GRADIENT) {
121 context.push_transform();
122 context.set_translation(Vector(0, 0));
123 context.draw_gradient(gradient_top, gradient_bottom, layer);
124 context.pop_transform();
125 } else if(type == IMAGE) {
129 int w = (int) image->get_width();
130 int h = (int) image->get_height();
131 int sx = int(-context.get_translation().x * speed) % w - w;
132 int sy = int(-context.get_translation().y * speed) % h - h;
133 context.push_transform();
134 context.set_translation(Vector(0, 0));
135 for(int x = sx; x < SCREEN_WIDTH; x += w)
136 for(int y = sy; y < SCREEN_HEIGHT; y += h)
137 context.draw_surface(image, Vector(x, y), layer);
138 context.pop_transform();
142 IMPLEMENT_FACTORY(Background, "background");