Background Class Reference

#include <background.hpp>

Inherits GameObject.

List of all members.

Public Member Functions

 Background ()
 Background (const Reader &reader)
virtual ~Background ()
void set_image (const std::string &name, float bkgd_speed)
std::string get_image () const
float get_speed () const
virtual void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
virtual void draw (DrawingContext &context)
 The GameObject should draw itself onto the provided DrawingContext if this function is called.
void draw_image (DrawingContext &context, const Vector &pos)

Private Types

enum  Alignment {
  NO_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT,
  BOTTOM_ALIGNMENT
}

Private Attributes

Alignment alignment
 Backgrounds with NO_ALIGNMENT are repeated over the whole screen, backgrounds with left, right, top, bottom alignment are only repeated in one direction and attached to the level edge.
int layer
std::string imagefile_top
std::string imagefile
std::string imagefile_bottom
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
SurfacePtr image_top
 image to draw above pos
SurfacePtr image
 image to draw, anchored at pos
SurfacePtr image_bottom
 image to draw below pos+screenheight


Detailed Description

Definition at line 26 of file background.hpp.


Member Enumeration Documentation

enum Background::Alignment [private]

Enumerator:
NO_ALIGNMENT 
LEFT_ALIGNMENT 
RIGHT_ALIGNMENT 
TOP_ALIGNMENT 
BOTTOM_ALIGNMENT 

Definition at line 46 of file background.hpp.

00046                  { 
00047     NO_ALIGNMENT,
00048     LEFT_ALIGNMENT,
00049     RIGHT_ALIGNMENT,
00050     TOP_ALIGNMENT,
00051     BOTTOM_ALIGNMENT
00052   };


Constructor & Destructor Documentation

Background::Background (  ) 

Definition at line 30 of file background.cpp.

00030                        :
00031   alignment(NO_ALIGNMENT),
00032   layer(LAYER_BACKGROUND0),
00033   imagefile_top(),
00034   imagefile(),
00035   imagefile_bottom(),
00036   pos(),
00037   speed(),
00038   speed_y(),
00039   scroll_speed(),
00040   scroll_offset(),
00041   image_top(),
00042   image(),
00043   image_bottom()
00044 {
00045 }

Background::Background ( const Reader reader  ) 

Definition at line 47 of file background.cpp.

References alignment, BOTTOM_ALIGNMENT, Surface::create(), lisp::Lisp::get(), image_bottom, image_top, imagefile, imagefile_bottom, imagefile_top, layer, LAYER_BACKGROUND0, LEFT_ALIGNMENT, log_warning, NO_ALIGNMENT, pos, reader_get_layer(), RIGHT_ALIGNMENT, scroll_offset, scroll_speed, set_image(), speed, speed_y, TOP_ALIGNMENT, Vector::x, and Vector::y.

00047                                            :
00048   alignment(NO_ALIGNMENT),
00049   layer(LAYER_BACKGROUND0),
00050   imagefile_top(),
00051   imagefile(),
00052   imagefile_bottom(),
00053   pos(),
00054   speed(),
00055   speed_y(),
00056   scroll_speed(),
00057   scroll_offset(),
00058   image_top(),
00059   image(),
00060   image_bottom()
00061 {
00062   // read position, defaults to (0,0)
00063   float px = 0;
00064   float py = 0;
00065   reader.get("x", px);
00066   reader.get("y", py);
00067   this->pos = Vector(px,py);
00068 
00069   speed = 1.0;
00070   speed_y = 1.0;
00071 
00072   std::string alignment_str;
00073   if (reader.get("alignment", alignment_str))
00074   {
00075     if (alignment_str == "left")
00076     {
00077       alignment = LEFT_ALIGNMENT;
00078     }
00079     else if (alignment_str == "right")
00080     {
00081       alignment = RIGHT_ALIGNMENT;
00082     }
00083     else if (alignment_str == "top")
00084     {
00085       alignment = TOP_ALIGNMENT;
00086     }
00087     else if (alignment_str == "bottom")
00088     {
00089       alignment = BOTTOM_ALIGNMENT;
00090     }
00091     else if (alignment_str == "none")
00092     {
00093       alignment = NO_ALIGNMENT;
00094     }
00095     else
00096     {
00097       log_warning << "Background: invalid alignment: '" << alignment_str << "'" << std::endl;
00098       alignment = NO_ALIGNMENT;
00099     }
00100   }
00101 
00102   reader.get("scroll-offset-x", scroll_offset.x);
00103   reader.get("scroll-offset-y", scroll_offset.y);
00104 
00105   reader.get("scroll-speed-x", scroll_speed.x);
00106   reader.get("scroll-speed-y", scroll_speed.y);
00107 
00108   layer = reader_get_layer (reader, /* default = */ LAYER_BACKGROUND0);
00109 
00110   if(!reader.get("image", imagefile) || !reader.get("speed", speed))
00111     throw std::runtime_error("Must specify image and speed for background");
00112 
00113   set_image(imagefile, speed);
00114   if (!reader.get("speed-y", speed_y))
00115   {
00116     speed_y = speed;
00117   }
00118 
00119   if (reader.get("image-top", imagefile_top)) {
00120     image_top = Surface::create(imagefile_top);
00121   }
00122   if (reader.get("image-bottom", imagefile_bottom)) {
00123     image_bottom = Surface::create(imagefile_bottom);
00124   }
00125 }

Background::~Background (  )  [virtual]

Definition at line 127 of file background.cpp.

00128 {
00129 }


Member Function Documentation

void Background::set_image ( const std::string &  name,
float  bkgd_speed 
)

Definition at line 138 of file background.cpp.

References Surface::create(), image, and imagefile.

Referenced by Background(), and Sector::parse_old_format().

00139 {
00140   this->imagefile = name;
00141   this->speed = speed;
00142 
00143   image = Surface::create(name);
00144 }

std::string Background::get_image (  )  const [inline]

Definition at line 35 of file background.hpp.

References imagefile.

00036   { return imagefile; }

float Background::get_speed (  )  const [inline]

Definition at line 37 of file background.hpp.

References speed.

00038   { return speed; }

void Background::update ( float  elapsed_time  )  [virtual]

This function is called once per frame and allows the object to update it's state.

The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)

Implements GameObject.

Definition at line 132 of file background.cpp.

References scroll_offset, and scroll_speed.

00133 {
00134   scroll_offset += scroll_speed * delta;
00135 }

void Background::draw ( DrawingContext context  )  [virtual]

The GameObject should draw itself onto the provided DrawingContext if this function is called.

Implements GameObject.

Definition at line 222 of file background.cpp.

References Sector::current(), draw_image(), Rectf::get_height(), DrawingContext::get_translation(), Rectf::get_width(), Sizef::height, image, SCREEN_HEIGHT, SCREEN_WIDTH, speed, Sizef::width, Vector::x, and Vector::y.

00223 {
00224   if(image.get() == NULL)
00225     return;
00226 
00227   Sizef level_size(Sector::current()->get_width(),
00228                    Sector::current()->get_height());
00229   Sizef screen(SCREEN_WIDTH, SCREEN_HEIGHT);
00230   Sizef translation_range = level_size - screen;
00231   Vector center_offset(context.get_translation().x - translation_range.width  / 2.0f, 
00232                        context.get_translation().y - translation_range.height / 2.0f);
00233 
00234   // FIXME: We are not handling 'pos'
00235   draw_image(context, Vector(level_size.width / 2.0f, level_size.height / 2.0f) + center_offset * (1.0f - speed));
00236 }

void Background::draw_image ( DrawingContext context,
const Vector pos 
)

Definition at line 147 of file background.cpp.

References alignment, BOTTOM_ALIGNMENT, Sector::current(), DrawingContext::draw_surface(), Rectf::get_bottom(), DrawingContext::get_cliprect(), Rectf::get_height(), Rectf::get_left(), Rectf::get_right(), Rectf::get_top(), Rectf::get_width(), Sizef::height, image, image_bottom, image_top, layer, LEFT_ALIGNMENT, NO_ALIGNMENT, pos, RIGHT_ALIGNMENT, SCREEN_HEIGHT, SCREEN_WIDTH, speed, TOP_ALIGNMENT, Sizef::width, Vector::x, and Vector::y.

Referenced by draw().

00148 {
00149   Sizef level(Sector::current()->get_width(), Sector::current()->get_height());
00150   Sizef screen(SCREEN_WIDTH, SCREEN_HEIGHT);
00151   Sizef parallax_image_size = (1.0f - speed) * screen + level * speed;
00152   Rectf cliprect = context.get_cliprect();
00153 
00154   int start_x = static_cast<int>(floorf((cliprect.get_left()  - (pos.x - image->get_width() /2.0f)) / image->get_width()));
00155   int end_x   = static_cast<int>(ceilf((cliprect.get_right()  - (pos.x + image->get_width() /2.0f)) / image->get_width()))+1;
00156   int start_y = static_cast<int>(floorf((cliprect.get_top()   - (pos.y - image->get_height()/2.0f)) / image->get_height()));
00157   int end_y   = static_cast<int>(ceilf((cliprect.get_bottom() - (pos.y + image->get_height()/2.0f)) / image->get_height()))+1;
00158 
00159   switch(alignment)
00160   {
00161     case LEFT_ALIGNMENT:
00162       for(int y = start_y; y < end_y; ++y)
00163       {
00164         Vector p(pos.x - parallax_image_size.width / 2.0f,
00165                  pos.y + y * image->get_height()  - image->get_height() / 2.0f);
00166         context.draw_surface(image, p, layer);
00167       }
00168       break;
00169 
00170     case RIGHT_ALIGNMENT:
00171       for(int y = start_y; y < end_y; ++y)
00172       {
00173         Vector p(pos.x + parallax_image_size.width / 2.0f - image->get_width(),
00174                  pos.y + y * image->get_height() - image->get_height() / 2.0f);
00175         context.draw_surface(image, p, layer);
00176       }
00177       break;
00178 
00179     case TOP_ALIGNMENT:
00180       for(int x = start_x; x < end_x; ++x)
00181       {
00182         Vector p(pos.x + x * image->get_width() - image->get_width() / 2.0f, 
00183                  pos.y - parallax_image_size.height / 2.0f);       
00184         context.draw_surface(image, p, layer);
00185       }
00186       break;
00187 
00188     case BOTTOM_ALIGNMENT:
00189       for(int x = start_x; x < end_x; ++x)
00190       {
00191         Vector p(pos.x + x * image->get_width()  - image->get_width() / 2.0f, 
00192                  pos.y - image->get_height() + parallax_image_size.height / 2.0f);       
00193         context.draw_surface(image, p, layer);
00194       }
00195       break;
00196 
00197     case NO_ALIGNMENT:
00198       for(int y = start_y; y < end_y; ++y)
00199         for(int x = start_x; x < end_x; ++x)
00200         {
00201           Vector p(pos.x + x * image->get_width()  - image->get_width()/2, 
00202                    pos.y + y * image->get_height() - image->get_height()/2);
00203 
00204           if (image_top.get() != NULL && (y < 0))
00205           {
00206             context.draw_surface(image_top, p, layer);
00207           }
00208           else if (image_bottom.get() != NULL && (y > 0))
00209           {
00210             context.draw_surface(image_bottom, p, layer);
00211           }
00212           else
00213           {
00214             context.draw_surface(image, p, layer);
00215           }
00216         }
00217       break;
00218   }
00219 }


Member Data Documentation

Alignment Background::alignment [private]

Backgrounds with NO_ALIGNMENT are repeated over the whole screen, backgrounds with left, right, top, bottom alignment are only repeated in one direction and attached to the level edge.

Definition at line 57 of file background.hpp.

Referenced by Background(), and draw_image().

int Background::layer [private]

Definition at line 59 of file background.hpp.

Referenced by Background(), and draw_image().

std::string Background::imagefile_top [private]

Definition at line 60 of file background.hpp.

Referenced by Background().

std::string Background::imagefile [private]

Definition at line 61 of file background.hpp.

Referenced by Background(), get_image(), and set_image().

std::string Background::imagefile_bottom [private]

Definition at line 62 of file background.hpp.

Referenced by Background().

Vector Background::pos [private]

coordinates of upper-left corner of image

Definition at line 63 of file background.hpp.

Referenced by Background(), and draw_image().

float Background::speed [private]

scroll-speed in horizontal direction

Definition at line 64 of file background.hpp.

Referenced by Background(), draw(), draw_image(), and get_speed().

float Background::speed_y [private]

scroll-speed in vertical direction

Definition at line 65 of file background.hpp.

Referenced by Background().

Vector Background::scroll_speed [private]

Definition at line 66 of file background.hpp.

Referenced by Background(), and update().

Vector Background::scroll_offset [private]

Definition at line 67 of file background.hpp.

Referenced by Background(), and update().

SurfacePtr Background::image_top [private]

image to draw above pos

Definition at line 68 of file background.hpp.

Referenced by Background(), and draw_image().

SurfacePtr Background::image [private]

image to draw, anchored at pos

Definition at line 69 of file background.hpp.

Referenced by draw(), draw_image(), and set_image().

SurfacePtr Background::image_bottom [private]

image to draw below pos+screenheight

Definition at line 70 of file background.hpp.

Referenced by Background(), and draw_image().


The documentation for this class was generated from the following files:
Generated on Mon Apr 14 03:38:32 2014 for SuperTux by  doxygen 1.5.1