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.
22 #include "background.h"
23 #include "app/globals.h"
25 #include "video/drawing_context.h"
26 #include "lisp/lisp.h"
27 #include "lisp/writer.h"
28 #include "object_factory.h"
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);
44 std::vector <unsigned int> 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), Color(bkgd_bottom_color));
50 Background::~Background()
56 Background::write(lisp::Writer& writer)
61 writer.start_list("background");
64 writer.write_string("image", imagefile);
65 writer.write_float("speed", speed);
66 } else if(type == GRADIENT) {
67 std::vector <unsigned int> bkgd_top_color, bkgd_bottom_color;
68 bkgd_top_color.push_back(gradient_top.red);
69 bkgd_top_color.push_back(gradient_top.green);
70 bkgd_top_color.push_back(gradient_top.blue);
71 bkgd_bottom_color.push_back(gradient_top.red);
72 bkgd_bottom_color.push_back(gradient_top.green);
73 bkgd_bottom_color.push_back(gradient_top.blue);
74 writer.write_int_vector("top_color", bkgd_top_color);
75 writer.write_int_vector("bottom_color", bkgd_bottom_color);
77 writer.write_int("layer", layer);
79 writer.end_list("background");
83 Background::action(float)
88 Background::set_image(const std::string& name, float speed)
91 this->imagefile = name;
95 image = new Surface(datadir + "/images/background/" + name, false);
99 Background::set_gradient(Color top, Color bottom)
103 gradient_bottom = bottom;
106 image = new Surface(top, bottom, screen->w, screen->h);
110 Background::draw(DrawingContext& context)
112 if(type == GRADIENT) {
113 context.push_transform();
114 context.set_translation(Vector(0, 0));
115 context.draw_surface(image, Vector(0, 0), layer);
116 context.pop_transform();
117 } else if(type == IMAGE) {
121 int sx = int(-context.get_translation().x * speed) % image->w - image->w;
122 int sy = int(-context.get_translation().y * speed) % image->h - image->h;
123 context.push_transform();
124 context.set_translation(Vector(0, 0));
125 for(int x = sx; x < screen->w; x += image->w)
126 for(int y = sy; y < screen->h; y += image->h)
127 context.draw_surface(image, Vector(x, y), layer);
128 context.pop_transform();
132 IMPLEMENT_FACTORY(Background, "background");