X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fobject%2Fbackground.cpp;h=7b0d75f00164b8a7bf85be1acec26f2c4acfe45a;hb=8eff855963e2699763d0d653dabc9b709f0d2ab0;hp=422592b384121124fe189fabfd11da005796bbb6;hpb=fff6a103cf0873ea6e72148500d8259d12d86418;p=supertux.git diff --git a/src/object/background.cpp b/src/object/background.cpp index 422592b38..7b0d75f00 100644 --- a/src/object/background.cpp +++ b/src/object/background.cpp @@ -17,6 +17,9 @@ #include "object/background.hpp" #include +#include +#include + #include "math/sizef.hpp" #include "supertux/globals.hpp" #include "supertux/object_factory.hpp" @@ -24,8 +27,6 @@ #include "util/log.hpp" #include "util/reader.hpp" -#include - Background::Background() : alignment(NO_ALIGNMENT), layer(LAYER_BACKGROUND0), @@ -104,12 +105,7 @@ Background::Background(const Reader& reader) : reader.get("scroll-speed-x", scroll_speed.x); reader.get("scroll-speed-y", scroll_speed.y); - reader.get("layer", layer); - if (layer > (LAYER_GUI - 100)) { - log_warning << "Layer of background (" << layer << ") is too large. " - << "Clipping to " << (LAYER_GUI - 100) << "." << std::endl; - layer = LAYER_GUI - 100; - } + layer = reader_get_layer (reader, /* default = */ LAYER_BACKGROUND0); if(!reader.get("image", imagefile) || !reader.get("speed", speed)) throw std::runtime_error("Must specify image and speed for background"); @@ -139,71 +135,71 @@ Background::update(float delta) } void -Background::set_image(const std::string& name, float speed) +Background::set_image(const std::string& name_, float speed_) { - this->imagefile = name; - this->speed = speed; + this->imagefile = name_; + this->speed = speed_; - image = Surface::create(name); + image = Surface::create(name_); } void -Background::draw_image(DrawingContext& context, const Vector& pos) +Background::draw_image(DrawingContext& context, const Vector& pos_) { 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; + Rectf cliprect = context.get_cliprect(); - // 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; + int start_x = static_cast(floorf((cliprect.get_left() - (pos_.x - image->get_width() /2.0f)) / image->get_width())); + int end_x = static_cast(ceilf((cliprect.get_right() - (pos_.x + image->get_width() /2.0f)) / image->get_width()))+1; + int start_y = static_cast(floorf((cliprect.get_top() - (pos_.y - image->get_height()/2.0f)) / image->get_height())); + int end_y = static_cast(ceilf((cliprect.get_bottom() - (pos_.y + image->get_height()/2.0f)) / image->get_height()))+1; switch(alignment) { case LEFT_ALIGNMENT: - for(int y = start_y; y <= end_y; ++y) + 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); + 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, p, layer); } break; case RIGHT_ALIGNMENT: - for(int y = start_y; y <= end_y; ++y) + 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); + 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, p, layer); } break; case TOP_ALIGNMENT: - for(int x = start_x; x <= end_x; ++x) + 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); + 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, p, layer); } break; case BOTTOM_ALIGNMENT: - for(int x = start_x; x <= end_x; ++x) + 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); + 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, p, layer); } break; case NO_ALIGNMENT: - for(int y = start_y; y <= end_y; ++y) - for(int x = start_x; x <= end_x; ++x) + 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); + 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)) { @@ -232,7 +228,7 @@ Background::draw(DrawingContext& context) 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, + 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'