fix memory leak in background object
[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)
33 {
34 }
35
36 Background::Background(const lisp::Lisp& reader)
37   : type(INVALID), layer(LAYER_BACKGROUND0)
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) && reader.get("speed", speed)) {
51     set_image(imagefile, speed);
52     reader.get("speed-y", speed_y);
53     if (reader.get("image-top", imagefile_top)) {
54       image_top.reset(new Surface(imagefile_top));
55     }
56     if (reader.get("image-bottom", imagefile_bottom)) {
57       image_bottom.reset(new Surface(imagefile_bottom));
58     }
59   } else {
60     std::vector<float> bkgd_top_color, bkgd_bottom_color;
61     if(reader.get_vector("top_color", bkgd_top_color) &&
62         reader.get_vector("bottom_color", bkgd_bottom_color))
63       set_gradient(Color(bkgd_top_color),
64                    Color(bkgd_bottom_color));
65   }
66 }
67
68 Background::~Background()
69 {
70 }
71
72 void
73 Background::write(lisp::Writer& writer)
74 {
75   if(type == INVALID)
76     return;
77     
78   writer.start_list("background");
79
80   if(type == IMAGE) {
81     if (image_top.get() != NULL)
82       writer.write_string("image-top", imagefile_top);
83     
84     writer.write_string("image", imagefile);
85     if (image_bottom.get() != NULL)
86       writer.write_string("image-bottom", imagefile_bottom);
87
88     writer.write_float("speed", speed);
89     writer.write_float("speed-y", speed_y);
90   } else if(type == GRADIENT) {
91     std::vector<float> bkgd_top_color, bkgd_bottom_color;
92     bkgd_top_color.push_back(gradient_top.red);
93     bkgd_top_color.push_back(gradient_top.green);
94     bkgd_top_color.push_back(gradient_top.blue);
95     bkgd_bottom_color.push_back(gradient_bottom.red);
96     bkgd_bottom_color.push_back(gradient_bottom.green);
97     bkgd_bottom_color.push_back(gradient_bottom.blue);
98     writer.write_float_vector("top_color", bkgd_top_color);
99     writer.write_float_vector("bottom_color", bkgd_bottom_color);
100   }
101   writer.write_int("layer", layer);
102   
103   writer.end_list("background");
104 }
105
106 void
107 Background::update(float)
108 {
109 }
110
111 void
112 Background::set_image(const std::string& name, float speed)
113 {
114   this->type = IMAGE;
115   this->imagefile = name;
116   this->speed = speed;
117
118   image.reset(new Surface(name));
119 }
120
121 void
122 Background::set_gradient(Color top, Color bottom)
123 {
124   type = GRADIENT;
125   gradient_top = top;
126   gradient_bottom = bottom;
127   
128   if (gradient_top.red > 1.0 || gradient_top.green > 1.0
129    || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
130     msg_warning("top gradient color has values above 1.0");
131   if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
132    || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
133     msg_warning("bottom gradient color has values above 1.0");
134
135   image.release();
136 }
137
138 void
139 Background::draw(DrawingContext& context)
140 {
141   if(type == GRADIENT) {
142     context.push_transform();
143     context.set_translation(Vector(0, 0));
144     context.draw_gradient(gradient_top, gradient_bottom, layer);
145     context.pop_transform();
146   } else if(type == IMAGE) {
147     if(image.get() == NULL)
148       return;
149     
150     int w = (int) image->get_width();
151     int h = (int) image->get_height();
152     int sx = int(pos.x-context.get_translation().x * speed) % w - w;
153     int sy = int(pos.y-context.get_translation().y * speed_y) % h - h;
154     int center_image_py = int(pos.y-context.get_translation().y * speed_y);
155     int bottom_image_py = int(pos.y-context.get_translation().y * speed_y) + h;
156     context.push_transform();
157     context.set_translation(Vector(0, 0));
158     for(int x = sx; x < SCREEN_WIDTH; x += w) {
159       for(int y = sy; y < SCREEN_HEIGHT; y += h) {
160         if (image_top.get() != NULL && (y < center_image_py)) {
161           context.draw_surface(image_top.get(), Vector(x, y), layer);
162           continue;
163         } 
164         if (image_bottom.get() != NULL && (y >= bottom_image_py)) {
165           context.draw_surface(image_bottom.get(), Vector(x, y), layer);
166           continue;
167         }
168         context.draw_surface(image.get(), Vector(x, y), layer);
169       }
170     }
171     context.pop_transform();
172   }
173 }
174
175 IMPLEMENT_FACTORY(Background, "background");