Background can now render three images: Top, Center and Bottom
[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 #include "msg.hpp"
30
31 Background::Background()
32   : type(INVALID), layer(LAYER_BACKGROUND0), image_top(0), image(0), image_bottom(0)
33 {
34 }
35
36 Background::Background(const lisp::Lisp& reader)
37   : type(INVALID), layer(LAYER_BACKGROUND0), image_top(0), image(0), image_bottom(0)
38 {
39   // read position, defaults to (0,0)
40   float px = 0;
41   float py = 0;
42   reader.get("x", px);
43   reader.get("y", py);
44   this->pos = Vector(px,py);
45
46   speed = 1.0;
47   speed_y = 1.0;
48
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);
56   } else {
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));
62   }
63 }
64
65 Background::~Background()
66 {
67   delete image;
68 }
69
70 void
71 Background::write(lisp::Writer& writer)
72 {
73   if(type == INVALID)
74     return;
75     
76   writer.start_list("background");
77
78   if(type == IMAGE) {
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);
94   }
95   writer.write_int("layer", layer);
96   
97   writer.end_list("background");
98 }
99
100 void
101 Background::update(float)
102 {
103 }
104
105 void
106 Background::set_image(const std::string& name, float speed)
107 {
108   this->type = IMAGE;
109   this->imagefile = name;
110   this->speed = speed;
111
112   delete image;
113   image = new Surface(name);
114 }
115
116 void
117 Background::set_gradient(Color top, Color bottom)
118 {
119   type = GRADIENT;
120   gradient_top = top;
121   gradient_bottom = bottom;
122   
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");
129
130   delete image;
131   image = NULL;
132 }
133
134 void
135 Background::draw(DrawingContext& context)
136 {
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) {
143     if(!image)
144       return;
145     
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);
158           continue;
159         } 
160         if (image_bottom && (y >= bottom_image_py)) {
161           context.draw_surface(image_bottom, Vector(x, y), layer);
162           continue;
163         }
164         context.draw_surface(image, Vector(x, y), layer);
165       }
166     }
167     context.pop_transform();
168   }
169 }
170
171 IMPLEMENT_FACTORY(Background, "background");