fade out console
[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 <stdexcept>
22 #include "background.hpp"
23 #include "camera.hpp"
24 #include "video/drawing_context.hpp"
25 #include "lisp/lisp.hpp"
26 #include "lisp/writer.hpp"
27 #include "object_factory.hpp"
28 #include "resources.hpp"
29 #include "main.hpp"
30 #include "log.hpp"
31
32 Background::Background()
33   : layer(LAYER_BACKGROUND0)
34 {
35 }
36
37 Background::Background(const lisp::Lisp& reader)
38   : layer(LAYER_BACKGROUND0)
39 {
40   // read position, defaults to (0,0)
41   float px = 0;
42   float py = 0;
43   reader.get("x", px);
44   reader.get("y", py);
45   this->pos = Vector(px,py);
46
47   speed = 1.0;
48   speed_y = 1.0;
49
50   reader.get("layer", layer);
51   if(!reader.get("image", imagefile) || !reader.get("speed", speed))
52     throw std::runtime_error("Must specify image and speed for background");
53   
54   set_image(imagefile, speed);
55   reader.get("speed-y", speed_y);
56   if (reader.get("image-top", imagefile_top)) {
57     image_top.reset(new Surface(imagefile_top));
58   }
59   if (reader.get("image-bottom", imagefile_bottom)) {
60     image_bottom.reset(new Surface(imagefile_bottom));
61   }
62 }
63
64 Background::~Background()
65 {
66 }
67
68 void
69 Background::write(lisp::Writer& writer)
70 {
71   writer.start_list("background");
72
73   if (image_top.get() != NULL)
74     writer.write_string("image-top", imagefile_top);
75     
76   writer.write_string("image", imagefile);
77   if (image_bottom.get() != NULL)
78     writer.write_string("image-bottom", imagefile_bottom);
79
80   writer.write_float("speed", speed);
81   writer.write_float("speed-y", speed_y);
82   writer.write_int("layer", layer);
83   
84   writer.end_list("background");
85 }
86
87 void
88 Background::update(float)
89 {
90 }
91
92 void
93 Background::set_image(const std::string& name, float speed)
94 {
95   this->imagefile = name;
96   this->speed = speed;
97
98   image.reset(new Surface(name));
99 }
100
101 void
102 Background::draw(DrawingContext& context)
103 {
104   if(image.get() == NULL)
105     return;
106     
107   int w = (int) image->get_width();
108   int h = (int) image->get_height();
109   int sx = int(pos.x-context.get_translation().x * speed) % w - w;
110   int sy = int(pos.y-context.get_translation().y * speed_y) % h - h;
111   int center_image_py = int(pos.y-context.get_translation().y * speed_y);
112   int bottom_image_py = int(pos.y-context.get_translation().y * speed_y) + h;
113   context.push_transform();
114   context.set_translation(Vector(0, 0));
115   for(int x = sx; x < SCREEN_WIDTH; x += w) {
116     for(int y = sy; y < SCREEN_HEIGHT; y += h) {
117       if (image_top.get() != NULL && (y < center_image_py)) {
118         context.draw_surface(image_top.get(), Vector(x, y), layer);
119         continue;
120       } 
121       if (image_bottom.get() != NULL && (y >= bottom_image_py)) {
122         context.draw_surface(image_bottom.get(), Vector(x, y), layer);
123         continue;
124       }
125       context.draw_surface(image.get(), Vector(x, y), layer);
126     }
127   }
128   context.pop_transform();
129 }
130
131 IMPLEMENT_FACTORY(Background, "background");