From 0cf37d2999601e9b5f995c45e049c3e2139915bc Mon Sep 17 00:00:00 2001 From: grumbel Date: Sat, 28 Nov 2009 23:11:36 +0000 Subject: [PATCH] Added support for scrolling backgrounds git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6154 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- data/levels/world1/13 - Above the Arctic Skies.stl | 2 ++ src/object/background.cpp | 17 ++++++++++++++--- src/object/background.hpp | 2 ++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/data/levels/world1/13 - Above the Arctic Skies.stl b/data/levels/world1/13 - Above the Arctic Skies.stl index 12bc1c5af..3572fbb85 100644 --- a/data/levels/world1/13 - Above the Arctic Skies.stl +++ b/data/levels/world1/13 - Above the Arctic Skies.stl @@ -20,12 +20,14 @@ (background (speed 0.75) + (scroll-speed-x -25) (image "images/background/arcticskies3.png") ) (background (speed 1.5) (layer 100) + (scroll-speed-x -50) (image "images/background/arcticskies35.png") ) diff --git a/src/object/background.cpp b/src/object/background.cpp index b403f3fe5..aaf28a157 100644 --- a/src/object/background.cpp +++ b/src/object/background.cpp @@ -27,6 +27,8 @@ Background::Background() : pos(), speed(), speed_y(), + scroll_speed(), + scroll_offset(), image_top(), image(), image_bottom() @@ -41,6 +43,8 @@ Background::Background(const Reader& reader) : pos(), speed(), speed_y(), + scroll_speed(), + scroll_offset(), image_top(), image(), image_bottom() @@ -55,6 +59,12 @@ Background::Background(const Reader& reader) : speed = 1.0; speed_y = 1.0; + 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"); @@ -78,8 +88,9 @@ Background::~Background() } void -Background::update(float) +Background::update(float delta) { + scroll_offset += scroll_speed * delta; } void @@ -99,8 +110,8 @@ Background::draw(DrawingContext& context) 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 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(); diff --git a/src/object/background.hpp b/src/object/background.hpp index ca9735820..426ab52fb 100644 --- a/src/object/background.hpp +++ b/src/object/background.hpp @@ -49,6 +49,8 @@ private: Vector pos; /**< coordinates of upper-left corner of image */ float speed; /**< scroll-speed in horizontal direction */ float speed_y; /**< scroll-speed in vertical direction */ + Vector scroll_speed; + Vector scroll_offset; std::auto_ptr image_top; /**< image to draw above pos */ std::auto_ptr image; /**< image to draw, anchored at pos */ std::auto_ptr image_bottom; /**< image to draw below pos+screenheight */ -- 2.11.0