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