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.
19 #include "background.h"
23 #include "screen/drawing_context.h"
24 #include "lispwriter.h"
26 Background::Background()
27 : type(INVALID), image(0)
31 Background::Background(LispReader& reader)
32 : type(INVALID), image(0)
34 if(reader.read_string("image", imagefile)
35 && reader.read_float("speed", speed)) {
36 set_image(imagefile, speed);
39 int tr, tg, tb, br, bg, bb;
40 if(reader.read_int("top_red", tr) && reader.read_int("top_green", tg)
41 && reader.read_int("top_blue", tb) && reader.read_int("bottom_red", br)
42 && reader.read_int("bottom_green", br)
43 && reader.read_int("bottom_blue", bb)) {
44 set_gradient(Color(tr, tg, tb), Color(br, bg, bb));
48 Background::~Background()
54 Background::write(LispWriter& writer)
59 writer.start_list("background");
62 writer.write_string("image", imagefile);
63 writer.write_float("speed", speed);
64 } else if(type == GRADIENT) {
65 writer.write_int("top_red", gradient_top.red);
66 writer.write_int("top_green", gradient_top.green);
67 writer.write_int("top_blue", gradient_top.blue);
68 writer.write_int("bottom_red", gradient_bottom.red);
69 writer.write_int("bottom_green", gradient_bottom.green);
70 writer.write_int("bottom_blue", gradient_bottom.blue);
73 writer.end_list("background");
77 Background::action(float)
82 Background::set_image(const std::string& name, float speed)
85 this->imagefile = name;
89 image = new Surface(datadir + "/images/background/" + name, IGNORE_ALPHA);
93 Background::set_gradient(Color top, Color bottom)
97 gradient_bottom = bottom;
101 Background::draw(DrawingContext& context)
103 if(type == GRADIENT) {
104 context.draw_gradient(gradient_top, gradient_bottom, LAYER_BACKGROUND0);
105 } else if(type == IMAGE) {
106 int sx = int(-context.get_translation().x * speed)
107 % image->w - image->w;
108 int sy = int(-context.get_translation().y * speed)
109 % image->h - image->h;
110 context.push_transform();
111 context.set_translation(Vector(0, 0));
112 for(int x = sx; x < screen->w; x += image->w)
113 for(int y = sy; y < screen->h; y += image->h)
114 context.draw_surface(image, Vector(x, y), LAYER_BACKGROUND0);
115 context.pop_transform();