move over rewritten lispreader from tuxkart (with additional fixes), generalized...
[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
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 "lisp/lisp.h"
27 #include "lisp/writer.h"
28
29 Background::Background()
30   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
31 {
32 }
33
34 Background::Background(const lisp::Lisp& reader)
35   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
36 {
37   reader.get("layer", layer);
38   if(reader.get("image", imagefile) 
39       && reader.get("speed", speed)) {
40     set_image(imagefile, speed);
41   }
42
43   std::vector <unsigned int> bkgd_top_color, bkgd_bottom_color;
44   if(reader.get_vector("top_color", bkgd_top_color) &&
45      reader.get_vector("bottom_color", bkgd_bottom_color))
46     set_gradient(Color(bkgd_top_color), Color(bkgd_bottom_color));
47 }
48
49 Background::~Background()
50 {
51   delete image;
52 }
53
54 void
55 Background::write(lisp::Writer& writer)
56 {
57   if(type == INVALID)
58     return;
59     
60   writer.start_list("background");
61
62   if(type == IMAGE) {
63     writer.write_string("image", imagefile);
64     writer.write_float("speed", speed);
65   } else if(type == GRADIENT) {
66     std::vector <unsigned int> bkgd_top_color, bkgd_bottom_color;
67     bkgd_top_color.push_back(gradient_top.red);
68     bkgd_top_color.push_back(gradient_top.green);
69     bkgd_top_color.push_back(gradient_top.blue);
70     bkgd_bottom_color.push_back(gradient_top.red);
71     bkgd_bottom_color.push_back(gradient_top.green);
72     bkgd_bottom_color.push_back(gradient_top.blue);
73     writer.write_int_vector("top_color", bkgd_top_color);
74     writer.write_int_vector("bottom_color", bkgd_bottom_color);
75   }
76   writer.write_int("layer", layer);
77   
78   writer.end_list("background");
79 }
80
81 void
82 Background::action(float)
83 {
84 }
85
86 void
87 Background::set_image(const std::string& name, float speed)
88 {
89   this->type = IMAGE;
90   this->imagefile = name;
91   this->speed = speed;
92
93   delete image;
94   image = new Surface(datadir + "/images/background/" + name, false);
95 }
96
97 void
98 Background::set_gradient(Color top, Color bottom)
99 {
100   type = GRADIENT;
101   gradient_top = top;
102   gradient_bottom = bottom;
103
104   delete image;
105   image = new Surface(top, bottom, screen->w, screen->h);
106 }
107
108 void
109 Background::draw(DrawingContext& context)
110 {
111   if(type == GRADIENT) {
112     context.push_transform();
113     context.set_translation(Vector(0, 0));
114     context.draw_surface(image, Vector(0, 0), layer);
115     context.pop_transform();
116   } else if(type == IMAGE) {
117     if(!image)
118       return;
119     
120     int sx = int(-context.get_translation().x * speed) % image->w - image->w;
121     int sy = int(-context.get_translation().y * speed) % image->h - image->h;
122     context.push_transform();
123     context.set_translation(Vector(0, 0));
124     for(int x = sx; x < screen->w; x += image->w)
125       for(int y = sy; y < screen->h; y += image->h)
126         context.draw_surface(image, Vector(x, y), layer);
127     context.pop_transform();
128   }
129 }
130