Removed trailing whitespace from all *.?pp files
[supertux.git] / src / object / background.cpp
index b403f3f..f49e05c 100644 (file)
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "object/background.hpp"
+
+#include <iostream>
+#include <math.h>
+#include <stdexcept>
+
+#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(),
@@ -27,6 +36,8 @@ Background::Background() :
   pos(),
   speed(),
   speed_y(),
+  scroll_speed(),
+  scroll_offset(),
   image_top(),
   image(),
   image_bottom()
@@ -34,6 +45,7 @@ Background::Background() :
 }
 
 Background::Background(const Reader& reader) :
+  alignment(NO_ALIGNMENT),
   layer(LAYER_BACKGROUND0),
   imagefile_top(),
   imagefile(),
@@ -41,6 +53,8 @@ Background::Background(const Reader& reader) :
   pos(),
   speed(),
   speed_y(),
+  scroll_speed(),
+  scroll_offset(),
   image_top(),
   image(),
   image_bottom()
@@ -55,7 +69,44 @@ Background::Background(const Reader& reader) :
   speed = 1.0;
   speed_y = 1.0;
 
-  reader.get("layer", layer);
+  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);
+
+  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");
 
@@ -78,8 +129,9 @@ Background::~Background()
 }
 
 void
-Background::update(float)
+Background::update(float delta)
 {
+  scroll_offset += scroll_speed * delta;
 }
 
 void
@@ -92,33 +144,95 @@ Background::set_image(const std::string& name, float speed)
 }
 
 void
-Background::draw(DrawingContext& context)
+Background::draw_image(DrawingContext& context, const Vector& pos)
 {
-  if(image.get() == NULL)
-    return;
+  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();
+
+  int start_x = static_cast<int>(floorf((cliprect.get_left()  - (pos.x - image->get_width() /2.0f)) / image->get_width()));
+  int end_x   = static_cast<int>(ceilf((cliprect.get_right()  - (pos.x + image->get_width() /2.0f)) / image->get_width()))+1;
+  int start_y = static_cast<int>(floorf((cliprect.get_top()   - (pos.y - image->get_height()/2.0f)) / image->get_height()));
+  int end_y   = static_cast<int>(ceilf((cliprect.get_bottom() - (pos.y + image->get_height()/2.0f)) / image->get_height()))+1;
 
-  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;
+  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, p, layer);
       }
-      if (image_bottom.get() != NULL && (y >= bottom_image_py)) {
-        context.draw_surface(image_bottom.get(), Vector(x, y), layer);
-        continue;
+      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, p, layer);
       }
-      context.draw_surface(image.get(), Vector(x, y), 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, 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, p, layer);
+      }
+      break;
+
+    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, p, layer);
+          }
+          else if (image_bottom.get() != NULL && (y > 0))
+          {
+            context.draw_surface(image_bottom, p, layer);
+          }
+          else
+          {
+            context.draw_surface(image, p, layer);
+          }
+        }
+      break;
   }
-  context.pop_transform();
+}
+
+void
+Background::draw(DrawingContext& context)
+{
+  if(image.get() == NULL)
+    return;
+
+  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 */