4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
23 #include "background.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"
33 Background::Background()
34 : layer(LAYER_BACKGROUND0)
38 Background::Background(const lisp::Lisp& reader)
39 : layer(LAYER_BACKGROUND0)
41 // read position, defaults to (0,0)
46 this->pos = Vector(px,py);
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");
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));
60 if (reader.get("image-bottom", imagefile_bottom)) {
61 image_bottom.reset(new Surface(imagefile_bottom));
65 Background::~Background()
70 Background::write(lisp::Writer& writer)
72 writer.start_list("background");
74 if (image_top.get() != NULL)
75 writer.write_string("image-top", imagefile_top);
77 writer.write_string("image", imagefile);
78 if (image_bottom.get() != NULL)
79 writer.write_string("image-bottom", imagefile_bottom);
81 writer.write_float("speed", speed);
82 writer.write_float("speed-y", speed_y);
83 writer.write_int("layer", layer);
85 writer.end_list("background");
89 Background::update(float)
94 Background::set_image(const std::string& name, float speed)
96 this->imagefile = name;
99 image.reset(new Surface(name));
103 Background::draw(DrawingContext& context)
105 if(image.get() == NULL)
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);
122 if (image_bottom.get() != NULL && (y >= bottom_image_py)) {
123 context.draw_surface(image_bottom.get(), Vector(x, y), layer);
126 context.draw_surface(image.get(), Vector(x, y), layer);
129 context.pop_transform();
132 IMPLEMENT_FACTORY(Background, "background");