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