supertux is using physfs now, this simplifies the code and generalises file handling
[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 #include <config.h>
20
21 #include "background.h"
22 #include "camera.h"
23 #include "video/drawing_context.h"
24 #include "lisp/lisp.h"
25 #include "lisp/writer.h"
26 #include "object_factory.h"
27 #include "resources.h"
28 #include "main.h"
29
30 Background::Background()
31   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
32 {
33 }
34
35 Background::Background(const lisp::Lisp& reader)
36   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
37 {
38   reader.get("layer", layer);
39   if(reader.get("image", imagefile) 
40       && reader.get("speed", speed)) {
41     set_image(imagefile, speed);
42   } else {
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
50 Background::~Background()
51 {
52   delete image;
53 }
54
55 void
56 Background::write(lisp::Writer& writer)
57 {
58   if(type == INVALID)
59     return;
60     
61   writer.start_list("background");
62
63   if(type == IMAGE) {
64     writer.write_string("image", imagefile);
65     writer.write_float("speed", speed);
66   } else if(type == GRADIENT) {
67     std::vector <unsigned int> bkgd_top_color, bkgd_bottom_color;
68     bkgd_top_color.push_back(gradient_top.red);
69     bkgd_top_color.push_back(gradient_top.green);
70     bkgd_top_color.push_back(gradient_top.blue);
71     bkgd_bottom_color.push_back(gradient_top.red);
72     bkgd_bottom_color.push_back(gradient_top.green);
73     bkgd_bottom_color.push_back(gradient_top.blue);
74     writer.write_int_vector("top_color", bkgd_top_color);
75     writer.write_int_vector("bottom_color", bkgd_bottom_color);
76   }
77   writer.write_int("layer", layer);
78   
79   writer.end_list("background");
80 }
81
82 void
83 Background::update(float)
84 {
85 }
86
87 void
88 Background::set_image(const std::string& name, float speed)
89 {
90   this->type = IMAGE;
91   this->imagefile = name;
92   this->speed = speed;
93
94   delete image;
95   image = new Surface("images/background/" + name, false);
96 }
97
98 void
99 Background::set_gradient(Color top, Color bottom)
100 {
101   type = GRADIENT;
102   gradient_top = top;
103   gradient_bottom = bottom;
104
105   delete image;
106   image = new Surface(top, bottom, SCREEN_WIDTH, SCREEN_HEIGHT);
107 }
108
109 void
110 Background::draw(DrawingContext& context)
111 {
112   if(type == GRADIENT) {
113     context.push_transform();
114     context.set_translation(Vector(0, 0));
115     context.draw_surface(image, Vector(0, 0), layer);
116     context.pop_transform();
117   } else if(type == IMAGE) {
118     if(!image)
119       return;
120     
121     int sx = int(-context.get_translation().x * speed) % image->w - image->w;
122     int sy = int(-context.get_translation().y * speed) % image->h - image->h;
123     context.push_transform();
124     context.set_translation(Vector(0, 0));
125     for(int x = sx; x < SCREEN_WIDTH; x += image->w)
126       for(int y = sy; y < SCREEN_HEIGHT; y += image->h)
127         context.draw_surface(image, Vector(x, y), layer);
128     context.pop_transform();
129   }
130 }
131
132 IMPLEMENT_FACTORY(Background, "background");