nolok dies properly now :)
[supertux.git] / src / 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
20 #include <config.h>
21
22 #include "background.h"
23 #include "app/globals.h"
24 #include "camera.h"
25 #include "video/drawing_context.h"
26 #include "utils/lispwriter.h"
27
28 Background::Background()
29   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
30 {
31 }
32
33 Background::Background(LispReader& reader)
34   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
35 {
36   reader.read_int("layer", layer);
37   if(reader.read_string("image", imagefile) 
38       && reader.read_float("speed", speed)) {
39     set_image(imagefile, speed);
40   }
41
42   std::vector <unsigned int> bkgd_top_color, bkgd_bottom_color;
43   if(reader.read_int_vector("top_color", bkgd_top_color) &&
44      reader.read_int_vector("bottom_color", bkgd_bottom_color))
45     set_gradient(Color(bkgd_top_color), Color(bkgd_bottom_color));
46 }
47
48 Background::~Background()
49 {
50   delete image;
51 }
52
53 void
54 Background::write(LispWriter& writer)
55 {
56   if(type == INVALID)
57     return;
58     
59   writer.start_list("background");
60
61   if(type == IMAGE) {
62     writer.write_string("image", imagefile);
63     writer.write_float("speed", speed);
64   } else if(type == GRADIENT) {
65     std::vector <unsigned int> bkgd_top_color, bkgd_bottom_color;
66     bkgd_top_color.push_back(gradient_top.red);
67     bkgd_top_color.push_back(gradient_top.green);
68     bkgd_top_color.push_back(gradient_top.blue);
69     bkgd_bottom_color.push_back(gradient_top.red);
70     bkgd_bottom_color.push_back(gradient_top.green);
71     bkgd_bottom_color.push_back(gradient_top.blue);
72     writer.write_int_vector("top_color", bkgd_top_color);
73     writer.write_int_vector("bottom_color", bkgd_bottom_color);
74   }
75   writer.write_int("layer", layer);
76   
77   writer.end_list("background");
78 }
79
80 void
81 Background::action(float)
82 {
83 }
84
85 void
86 Background::set_image(const std::string& name, float speed)
87 {
88   this->type = IMAGE;
89   this->imagefile = name;
90   this->speed = speed;
91
92   delete image;
93   image = new Surface(datadir + "/images/background/" + name, false);
94 }
95
96 void
97 Background::set_gradient(Color top, Color bottom)
98 {
99   type = GRADIENT;
100   gradient_top = top;
101   gradient_bottom = bottom;
102
103   delete image;
104   image = new Surface(top, bottom, screen->w, screen->h);
105 }
106
107 void
108 Background::draw(DrawingContext& context)
109 {
110   if(type == GRADIENT) {
111     /* In case we are using OpenGL just draw the gradient, else (software mode)
112         use the cache. */
113     if(use_gl)
114       context.draw_gradient(gradient_top, gradient_bottom, layer);
115     else
116       {
117       context.push_transform();
118       context.set_translation(Vector(0, 0));
119       context.draw_surface(image, Vector(0, 0), layer);
120       context.pop_transform();
121       }
122   } else if(type == IMAGE) {
123     if(!image)
124       return;
125     
126     int sx = int(-context.get_translation().x * speed) % image->w - image->w;
127     int sy = int(-context.get_translation().y * speed) % image->h - image->h;
128     context.push_transform();
129     context.set_translation(Vector(0, 0));
130     for(int x = sx; x < screen->w; x += image->w)
131       for(int y = sy; y < screen->h; y += image->h)
132         context.draw_surface(image, Vector(x, y), layer);
133     context.pop_transform();
134   }
135 }
136