65d7be70a5ddf4a194daa9f24597e6765e423b00
[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 "globals.h"
22 #include "camera.h"
23 #include "screen/drawing_context.h"
24 #include "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   int tr, tg, tb, br, bg, bb;
40   if(reader.read_int("top_red", tr) && reader.read_int("top_green", tg)
41       && reader.read_int("top_blue", tb) && reader.read_int("bottom_red", br)
42       && reader.read_int("bottom_green", br)
43       && reader.read_int("bottom_blue", bb)) {
44     set_gradient(Color(tr, tg, tb), Color(br, bg, bb));
45   }
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     writer.write_int("top_red", gradient_top.red);
66     writer.write_int("top_green", gradient_top.green);
67     writer.write_int("top_blue", gradient_top.blue);
68     writer.write_int("bottom_red", gradient_bottom.red);
69     writer.write_int("bottom_green", gradient_bottom.green);
70     writer.write_int("bottom_blue", gradient_bottom.blue);
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, IGNORE_ALPHA);
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     context.draw_surface(image, Vector(0, 0), LAYER_BACKGROUND0);
108 //    context.draw_gradient(gradient_top, gradient_bottom, LAYER_BACKGROUND0);
109   } else if(type == IMAGE) {
110     int sx = int(-context.get_translation().x * speed)
111       % image->w - image->w;
112     int sy = int(-context.get_translation().y * speed)
113       % image->h - image->h;
114     context.push_transform();
115     context.set_translation(Vector(0, 0));
116     for(int x = sx; x < screen->w; x += image->w)
117       for(int y = sy; y < screen->h; y += image->h)
118         context.draw_surface(image, Vector(x, y), LAYER_BACKGROUND0);
119     context.pop_transform();
120   }
121 }
122