X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fobject%2Fbackground.cpp;h=0ed917b0a15627df17a9e6ae51ec80c2e4b00a3e;hb=cbb015c68e184f5c19d0483a486325c1ee819086;hp=dc94c3cc9cac51a2a6aa266b39ad2c93ce532208;hpb=e6a940db5904743e8220491ce10b5107e119a44c;p=supertux.git diff --git a/src/object/background.cpp b/src/object/background.cpp index dc94c3cc9..0ed917b0a 100644 --- a/src/object/background.cpp +++ b/src/object/background.cpp @@ -1,12 +1,10 @@ -// $Id$ +// SuperTux +// Copyright (C) 2006 Matthias Braun // -// SuperTux - A Jump'n Run -// Copyright (C) 2004 Matthias Braun - -#include -#include "background.hpp" -#include "camera.hpp" -#include "video/drawing_context.hpp" -#include "lisp/lisp.hpp" -#include "lisp/writer.hpp" -#include "object_factory.hpp" -#include "resources.hpp" -#include "main.hpp" -#include "log.hpp" - -Background::Background() - : layer(LAYER_BACKGROUND0) +// along with this program. If not, see . + +#include "object/background.hpp" + +#include +#include "math/sizef.hpp" +#include "supertux/globals.hpp" +#include "supertux/object_factory.hpp" +#include "supertux/sector.hpp" +#include "util/log.hpp" +#include "util/reader.hpp" + +Background::Background() : + alignment(NO_ALIGNMENT), + layer(LAYER_BACKGROUND0), + imagefile_top(), + imagefile(), + imagefile_bottom(), + pos(), + speed(), + speed_y(), + scroll_speed(), + scroll_offset(), + image_top(), + image(), + image_bottom() { } -Background::Background(const lisp::Lisp& reader) - : layer(LAYER_BACKGROUND0) +Background::Background(const Reader& reader) : + alignment(NO_ALIGNMENT), + layer(LAYER_BACKGROUND0), + imagefile_top(), + imagefile(), + imagefile_bottom(), + pos(), + speed(), + speed_y(), + scroll_speed(), + scroll_offset(), + image_top(), + image(), + image_bottom() { // read position, defaults to (0,0) float px = 0; @@ -47,17 +66,57 @@ Background::Background(const lisp::Lisp& reader) speed = 1.0; speed_y = 1.0; + std::string alignment_str; + if (reader.get("alignment", alignment_str)) + { + if (alignment_str == "left") + { + alignment = LEFT_ALIGNMENT; + } + else if (alignment_str == "right") + { + alignment = RIGHT_ALIGNMENT; + } + else if (alignment_str == "top") + { + alignment = TOP_ALIGNMENT; + } + else if (alignment_str == "bottom") + { + alignment = BOTTOM_ALIGNMENT; + } + else if (alignment_str == "none") + { + alignment = NO_ALIGNMENT; + } + else + { + log_warning << "Background: invalid alignment: '" << alignment_str << "'" << std::endl; + alignment = NO_ALIGNMENT; + } + } + + reader.get("scroll-offset-x", scroll_offset.x); + reader.get("scroll-offset-y", scroll_offset.y); + + reader.get("scroll-speed-x", scroll_speed.x); + reader.get("scroll-speed-y", scroll_speed.y); + reader.get("layer", layer); if(!reader.get("image", imagefile) || !reader.get("speed", speed)) throw std::runtime_error("Must specify image and speed for background"); - + set_image(imagefile, speed); - reader.get("speed-y", speed_y); + if (!reader.get("speed-y", speed_y)) + { + speed_y = speed; + } + if (reader.get("image-top", imagefile_top)) { - image_top.reset(new Surface(imagefile_top)); + image_top = Surface::create(imagefile_top); } if (reader.get("image-bottom", imagefile_bottom)) { - image_bottom.reset(new Surface(imagefile_bottom)); + image_bottom = Surface::create(imagefile_bottom); } } @@ -66,36 +125,93 @@ Background::~Background() } void -Background::write(lisp::Writer& writer) +Background::update(float delta) { - writer.start_list("background"); - - if (image_top.get() != NULL) - writer.write_string("image-top", imagefile_top); - - writer.write_string("image", imagefile); - if (image_bottom.get() != NULL) - writer.write_string("image-bottom", imagefile_bottom); - - writer.write_float("speed", speed); - writer.write_float("speed-y", speed_y); - writer.write_int("layer", layer); - - writer.end_list("background"); + scroll_offset += scroll_speed * delta; } void -Background::update(float) +Background::set_image(const std::string& name, float speed) { + this->imagefile = name; + this->speed = speed; + + image = Surface::create(name); } void -Background::set_image(const std::string& name, float speed) +Background::draw_image(DrawingContext& context, const Vector& pos) { - this->imagefile = name; - this->speed = speed; + Sizef level(Sector::current()->get_width(), Sector::current()->get_height()); + Sizef screen(SCREEN_WIDTH, SCREEN_HEIGHT); + Sizef parallax_image_size = (1.0f - speed) * screen + level * speed; + + // FIXME: Implement proper clipping here + int start_x = -parallax_image_size.width / 2.0f / image->get_width() - 1; + int end_x = parallax_image_size.width / 2.0f / image->get_width() + 1; + int start_y = -parallax_image_size.height / 2.0f / image->get_height() - 1; + int end_y = parallax_image_size.height / 2.0f / image->get_height() + 1; + + switch(alignment) + { + case LEFT_ALIGNMENT: + for(int y = start_y; y < end_y; ++y) + { + Vector p(pos.x - parallax_image_size.width / 2.0f, + pos.y + y * image->get_height() - image->get_height() / 2.0f); + context.draw_surface(image.get(), p, layer); + } + break; + + case RIGHT_ALIGNMENT: + for(int y = start_y; y < end_y; ++y) + { + Vector p(pos.x + parallax_image_size.width / 2.0f - image->get_width(), + pos.y + y * image->get_height() - image->get_height() / 2.0f); + context.draw_surface(image.get(), p, layer); + } + break; + + case TOP_ALIGNMENT: + for(int x = start_x; x < end_x; ++x) + { + Vector p(pos.x + x * image->get_width() - image->get_width() / 2.0f, + pos.y - parallax_image_size.height / 2.0f); + context.draw_surface(image.get(), p, layer); + } + break; + + case BOTTOM_ALIGNMENT: + for(int x = start_x; x < end_x; ++x) + { + Vector p(pos.x + x * image->get_width() - image->get_width() / 2.0f, + pos.y - image->get_height() + parallax_image_size.height / 2.0f); + context.draw_surface(image.get(), p, layer); + } + break; - image.reset(new Surface(name)); + case NO_ALIGNMENT: + for(int y = start_y; y <= end_y; ++y) + for(int x = start_x; x <= end_x; ++x) + { + Vector p(pos.x + x * image->get_width() - image->get_width()/2, + pos.y + y * image->get_height() - image->get_height()/2); + + if (image_top.get() != NULL && (y < 0)) + { + context.draw_surface(image_top.get(), p, layer); + } + else if (image_bottom.get() != NULL && (y > 0)) + { + context.draw_surface(image_bottom.get(), p, layer); + } + else + { + context.draw_surface(image.get(), p, layer); + } + } + break; + } } void @@ -103,29 +219,16 @@ Background::draw(DrawingContext& context) { if(image.get() == NULL) return; - - int w = (int) image->get_width(); - int h = (int) image->get_height(); - int sx = int(pos.x-context.get_translation().x * speed) % w - w; - int sy = int(pos.y-context.get_translation().y * speed_y) % h - h; - int center_image_py = int(pos.y-context.get_translation().y * speed_y); - int bottom_image_py = int(pos.y-context.get_translation().y * speed_y) + h; - context.push_transform(); - context.set_translation(Vector(0, 0)); - for(int x = sx; x < SCREEN_WIDTH; x += w) { - for(int y = sy; y < SCREEN_HEIGHT; y += h) { - if (image_top.get() != NULL && (y < center_image_py)) { - context.draw_surface(image_top.get(), Vector(x, y), layer); - continue; - } - if (image_bottom.get() != NULL && (y >= bottom_image_py)) { - context.draw_surface(image_bottom.get(), Vector(x, y), layer); - continue; - } - context.draw_surface(image.get(), Vector(x, y), layer); - } - } - context.pop_transform(); + + Sizef level_size(Sector::current()->get_width(), + Sector::current()->get_height()); + Sizef screen(SCREEN_WIDTH, SCREEN_HEIGHT); + Sizef translation_range = level_size - screen; + Vector center_offset(context.get_translation().x - translation_range.width / 2.0f, + context.get_translation().y - translation_range.height / 2.0f); + + // FIXME: We are not handling 'pos' + draw_image(context, Vector(level_size.width / 2.0f, level_size.height / 2.0f) + center_offset * (1.0f - speed)); } -IMPLEMENT_FACTORY(Background, "background"); +/* EOF */