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