Unified Messaging Subsystem
[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(0)
33 {
34 }
35
36 Background::Background(const lisp::Lisp& reader)
37   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
38 {
39   reader.get("layer", layer);
40   if(reader.get("image", imagefile) 
41       && reader.get("speed", speed)) {
42     set_image(imagefile, speed);
43   } else {
44     std::vector<float> 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),
48                    Color(bkgd_bottom_color));
49   }
50 }
51
52 Background::~Background()
53 {
54   delete image;
55 }
56
57 void
58 Background::write(lisp::Writer& writer)
59 {
60   if(type == INVALID)
61     return;
62     
63   writer.start_list("background");
64
65   if(type == IMAGE) {
66     writer.write_string("image", imagefile);
67     writer.write_float("speed", speed);
68   } else if(type == GRADIENT) {
69     std::vector<float> bkgd_top_color, bkgd_bottom_color;
70     bkgd_top_color.push_back(gradient_top.red);
71     bkgd_top_color.push_back(gradient_top.green);
72     bkgd_top_color.push_back(gradient_top.blue);
73     bkgd_bottom_color.push_back(gradient_bottom.red);
74     bkgd_bottom_color.push_back(gradient_bottom.green);
75     bkgd_bottom_color.push_back(gradient_bottom.blue);
76     writer.write_float_vector("top_color", bkgd_top_color);
77     writer.write_float_vector("bottom_color", bkgd_bottom_color);
78   }
79   writer.write_int("layer", layer);
80   
81   writer.end_list("background");
82 }
83
84 void
85 Background::update(float)
86 {
87 }
88
89 void
90 Background::set_image(const std::string& name, float speed)
91 {
92   this->type = IMAGE;
93   this->imagefile = name;
94   this->speed = speed;
95
96   delete image;
97   image = new Surface(name);
98 }
99
100 void
101 Background::set_gradient(Color top, Color bottom)
102 {
103   type = GRADIENT;
104   gradient_top = top;
105   gradient_bottom = bottom;
106   
107   if (gradient_top.red > 1.0 || gradient_top.green > 1.0
108    || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
109     msg_warning("top gradient color has values above 1.0");
110   if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
111    || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
112     msg_warning("bottom gradient color has values above 1.0");
113
114   delete image;
115   image = NULL;
116 }
117
118 void
119 Background::draw(DrawingContext& context)
120 {
121   if(type == GRADIENT) {
122     context.push_transform();
123     context.set_translation(Vector(0, 0));
124     context.draw_gradient(gradient_top, gradient_bottom, layer);
125     context.pop_transform();
126   } else if(type == IMAGE) {
127     if(!image)
128       return;
129     
130     int w = (int) image->get_width();
131     int h = (int) image->get_height();
132     int sx = int(-context.get_translation().x * speed) % w - w;
133     int sy = int(-context.get_translation().y * speed) % h - h;
134     context.push_transform();
135     context.set_translation(Vector(0, 0));
136     for(int x = sx; x < SCREEN_WIDTH; x += w)
137       for(int y = sy; y < SCREEN_HEIGHT; y += h)
138         context.draw_surface(image, Vector(x, y), layer);
139     context.pop_transform();
140   }
141 }
142
143 IMPLEMENT_FACTORY(Background, "background");