From 4751769fa541c3f987ebaeed5b36dc6f78cdb51d Mon Sep 17 00:00:00 2001 From: grumbel Date: Mon, 30 Nov 2009 17:34:31 +0000 Subject: [PATCH] Background drawing now starts at the center of the level, instead of the top/left, it also happens in WorldCO instead of ScreenCO, making it a little bit more robust against resolutions changes, it is however still not resolution independed git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6158 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- src/object/background.cpp | 68 ++++++++++++++++++++++++++++++++--------------- src/object/background.hpp | 1 + 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/object/background.cpp b/src/object/background.cpp index aaf28a157..8729ba527 100644 --- a/src/object/background.cpp +++ b/src/object/background.cpp @@ -15,8 +15,12 @@ // 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/reader.hpp" Background::Background() : @@ -103,33 +107,53 @@ Background::set_image(const std::string& name, float speed) } void +Background::draw_image(DrawingContext& context, const Vector& pos) +{ + Sizef level(Sector::current()->get_width(), + Sector::current()->get_height()); + + // FIXME: Implement proper clipping here + int start_x = -level.width / image->get_width() / 2; + int end_x = level.width / image->get_width() / 2; + int start_y = -level.height / image->get_height() / 2; + int end_y = level.height / image->get_height() / 2; + + 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); + } + } +} + +void Background::draw(DrawingContext& context) { if(image.get() == NULL) return; - int w = (int) image->get_width(); - int h = (int) image->get_height(); - int sx = int(scroll_offset.x + pos.x-context.get_translation().x * speed) % w - w; - int sy = int(scroll_offset.y + 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)); } /* EOF */ diff --git a/src/object/background.hpp b/src/object/background.hpp index 426ab52fb..2b4e2ebb0 100644 --- a/src/object/background.hpp +++ b/src/object/background.hpp @@ -40,6 +40,7 @@ public: virtual void update(float elapsed_time); virtual void draw(DrawingContext& context); + void draw_image(DrawingContext& context, const Vector& pos); private: int layer; -- 2.11.0