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_top(0), image(0), image_bottom(0)
36 Background::Background(const lisp::Lisp& reader)
37 : type(INVALID), layer(LAYER_BACKGROUND0), image_top(0), image(0), image_bottom(0)
39 // read position, defaults to (0,0)
44 this->pos = Vector(px,py);
49 reader.get("layer", layer);
50 if(reader.get("image", imagefile)
51 && reader.get("speed", speed)) {
52 set_image(imagefile, speed);
53 reader.get("speed-y", speed_y);
54 if (reader.get("image-top", imagefile_top)) image_top = new Surface(imagefile_top);
55 if (reader.get("image-bottom", imagefile_bottom)) image_bottom = new Surface(imagefile_bottom);
57 std::vector<float> bkgd_top_color, bkgd_bottom_color;
58 if(reader.get_vector("top_color", bkgd_top_color) &&
59 reader.get_vector("bottom_color", bkgd_bottom_color))
60 set_gradient(Color(bkgd_top_color),
61 Color(bkgd_bottom_color));
65 Background::~Background()
71 Background::write(lisp::Writer& writer)
76 writer.start_list("background");
79 if (image_top) writer.write_string("image-top", imagefile_top);
80 writer.write_string("image", imagefile);
81 if (image_bottom) writer.write_string("image-bottom", imagefile_bottom);
82 writer.write_float("speed", speed);
83 writer.write_float("speed-y", speed_y);
84 } else if(type == GRADIENT) {
85 std::vector<float> bkgd_top_color, bkgd_bottom_color;
86 bkgd_top_color.push_back(gradient_top.red);
87 bkgd_top_color.push_back(gradient_top.green);
88 bkgd_top_color.push_back(gradient_top.blue);
89 bkgd_bottom_color.push_back(gradient_bottom.red);
90 bkgd_bottom_color.push_back(gradient_bottom.green);
91 bkgd_bottom_color.push_back(gradient_bottom.blue);
92 writer.write_float_vector("top_color", bkgd_top_color);
93 writer.write_float_vector("bottom_color", bkgd_bottom_color);
95 writer.write_int("layer", layer);
97 writer.end_list("background");
101 Background::update(float)
106 Background::set_image(const std::string& name, float speed)
109 this->imagefile = name;
113 image = new Surface(name);
117 Background::set_gradient(Color top, Color bottom)
121 gradient_bottom = bottom;
123 if (gradient_top.red > 1.0 || gradient_top.green > 1.0
124 || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
125 msg_warning("top gradient color has values above 1.0");
126 if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
127 || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
128 msg_warning("bottom gradient color has values above 1.0");
135 Background::draw(DrawingContext& context)
137 if(type == GRADIENT) {
138 context.push_transform();
139 context.set_translation(Vector(0, 0));
140 context.draw_gradient(gradient_top, gradient_bottom, layer);
141 context.pop_transform();
142 } else if(type == IMAGE) {
146 int w = (int) image->get_width();
147 int h = (int) image->get_height();
148 int sx = int(pos.x-context.get_translation().x * speed) % w - w;
149 int sy = int(pos.y-context.get_translation().y * speed_y) % h - h;
150 int center_image_py = int(pos.y-context.get_translation().y * speed_y);
151 int bottom_image_py = int(pos.y-context.get_translation().y * speed_y) + h;
152 context.push_transform();
153 context.set_translation(Vector(0, 0));
154 for(int x = sx; x < SCREEN_WIDTH; x += w) {
155 for(int y = sy; y < SCREEN_HEIGHT; y += h) {
156 if (image_top && (y < center_image_py)) {
157 context.draw_surface(image_top, Vector(x, y), layer);
160 if (image_bottom && (y >= bottom_image_py)) {
161 context.draw_surface(image_bottom, Vector(x, y), layer);
164 context.draw_surface(image, Vector(x, y), layer);
167 context.pop_transform();
171 IMPLEMENT_FACTORY(Background, "background");