Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / background.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/background.hpp"
18 #include "supertux/main.hpp"
19 #include "supertux/object_factory.hpp"
20 #include "util/reader.hpp"
21
22 Background::Background()
23   : layer(LAYER_BACKGROUND0)
24 {
25 }
26
27 Background::Background(const Reader& reader)
28   : layer(LAYER_BACKGROUND0)
29 {
30   // read position, defaults to (0,0)
31   float px = 0;
32   float py = 0;
33   reader.get("x", px);
34   reader.get("y", py);
35   this->pos = Vector(px,py);
36
37   speed = 1.0;
38   speed_y = 1.0;
39
40   reader.get("layer", layer);
41   if(!reader.get("image", imagefile) || !reader.get("speed", speed))
42     throw std::runtime_error("Must specify image and speed for background");
43
44   set_image(imagefile, speed);
45   reader.get("speed-y", speed_y);
46   if (reader.get("image-top", imagefile_top)) {
47     image_top.reset(new Surface(imagefile_top));
48   }
49   if (reader.get("image-bottom", imagefile_bottom)) {
50     image_bottom.reset(new Surface(imagefile_bottom));
51   }
52 }
53
54 Background::~Background()
55 {
56 }
57
58 void
59 Background::update(float)
60 {
61 }
62
63 void
64 Background::set_image(const std::string& name, float speed)
65 {
66   this->imagefile = name;
67   this->speed = speed;
68
69   image.reset(new Surface(name));
70 }
71
72 void
73 Background::draw(DrawingContext& context)
74 {
75   if(image.get() == NULL)
76     return;
77
78   int w = (int) image->get_width();
79   int h = (int) image->get_height();
80   int sx = int(pos.x-context.get_translation().x * speed) % w - w;
81   int sy = int(pos.y-context.get_translation().y * speed_y) % h - h;
82   int center_image_py = int(pos.y-context.get_translation().y * speed_y);
83   int bottom_image_py = int(pos.y-context.get_translation().y * speed_y) + h;
84   context.push_transform();
85   context.set_translation(Vector(0, 0));
86   for(int x = sx; x < SCREEN_WIDTH; x += w) {
87     for(int y = sy; y < SCREEN_HEIGHT; y += h) {
88       if (image_top.get() != NULL && (y < center_image_py)) {
89         context.draw_surface(image_top.get(), Vector(x, y), layer);
90         continue;
91       }
92       if (image_bottom.get() != NULL && (y >= bottom_image_py)) {
93         context.draw_surface(image_bottom.get(), Vector(x, y), layer);
94         continue;
95       }
96       context.draw_surface(image.get(), Vector(x, y), layer);
97     }
98   }
99   context.pop_transform();
100 }
101
102 IMPLEMENT_FACTORY(Background, "background");
103
104 /* EOF */