* Fixed gradient background bug
[supertux.git] / src / object / background.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
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.
10 //
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.
15 //
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 <config.h>
20
21 #include "background.hpp"
22 #include "camera.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"
28 #include "main.hpp"
29
30 Background::Background()
31   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
32 {
33 }
34
35 Background::Background(const lisp::Lisp& reader)
36   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
37 {
38   reader.get("layer", layer);
39   if(reader.get("image", imagefile) 
40       && reader.get("speed", speed)) {
41     set_image(imagefile, speed);
42   } else {
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));
48   }
49 }
50
51 Background::~Background()
52 {
53   delete image;
54 }
55
56 void
57 Background::write(lisp::Writer& writer)
58 {
59   if(type == INVALID)
60     return;
61     
62   writer.start_list("background");
63
64   if(type == IMAGE) {
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);
77   }
78   writer.write_int("layer", layer);
79   
80   writer.end_list("background");
81 }
82
83 void
84 Background::update(float)
85 {
86 }
87
88 void
89 Background::set_image(const std::string& name, float speed)
90 {
91   this->type = IMAGE;
92   this->imagefile = name;
93   this->speed = speed;
94
95   delete image;
96   image = new Surface("images/background/" + name);
97 }
98
99 void
100 Background::set_gradient(Color top, Color bottom)
101 {
102   type = GRADIENT;
103   gradient_top = top;
104   gradient_bottom = bottom;
105   
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;
112
113   delete image;
114   image = NULL;
115 }
116
117 void
118 Background::draw(DrawingContext& context)
119 {
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) {
126     if(!image)
127       return;
128     
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();
139   }
140 }
141
142 IMPLEMENT_FACTORY(Background, "background");