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