Sprite Class Reference

#include <sprite.hpp>

List of all members.

Public Member Functions

 Sprite (SpriteData &data)
 ~Sprite ()
SpritePtr clone () const
void draw (DrawingContext &context, const Vector &pos, int layer, DrawingEffect effect=NO_EFFECT)
 Draw sprite, automatically calculates next frame.
void draw_part (DrawingContext &context, const Vector &source, const Vector &size, const Vector &pos, int layer)
void set_action (const std::string &name, int loops=-1)
 Set action (or state).
void set_action_continued (const std::string &name)
 Set action (or state), but keep current frame number, loop counter, etc.
void set_animation_loops (int loops=-1)
 Set number of animation cycles until animation stops.
void stop_animation ()
bool animation_done ()
 Check if animation is stopped or not.
float get_fps () const
unsigned int get_frames () const
 Get current action total frames.
const std::string & get_name () const
 Get sprite's name.
const std::string & get_action () const
 Get current action name.
int get_width () const
int get_height () const
float get_current_hitbox_x_offset () const
 return x-offset of current action's hitbox, relative to start of image
float get_current_hitbox_y_offset () const
 return y-offset of current action's hitbox, relative to start of image
float get_current_hitbox_width () const
 return width of current action's hitbox
float get_current_hitbox_height () const
 return height of current action's hitbox
Rectf get_current_hitbox () const
 return current action's hitbox, relative to 0,0
void set_angle (float angle)
 Set the angle of the sprite rotation in degree.
float get_angle () const
 Get the angle of the sprite rotation in degree.
void set_color (const Color &color)
Color get_color () const
void set_blend (const Blend &blend)
Blend get_blend () const
unsigned int get_frame () const
 Get current frame.
void set_frame (int frame_)
 Set current frame.
SurfacePtr get_frame (unsigned int frame_)
bool has_action (const std::string &name)

Private Member Functions

void update ()
 Sprite (const Sprite &other)
Spriteoperator= (const Sprite &)

Private Attributes

SpriteDatadata
float frame
unsigned int frameidx
int animation_loops
float last_ticks
float angle
Color color
Blend blend
const SpriteData::Actionaction


Detailed Description

Definition at line 28 of file sprite.hpp.


Constructor & Destructor Documentation

Sprite::Sprite ( SpriteData data  ) 

Definition at line 24 of file sprite.cpp.

References action, SpriteData::actions, data, game_time, and last_ticks.

Referenced by clone().

00024                                   :
00025   data(newdata),
00026   frame(0),
00027   frameidx(0),
00028   animation_loops(-1),
00029   last_ticks(),
00030   angle(0.0f),
00031   color(1.0f, 1.0f, 1.0f, 1.0f),
00032   blend(),
00033   action(data.get_action("normal"))
00034 {
00035   if(!action)
00036     action = data.actions.begin()->second;
00037   last_ticks = game_time;
00038 }

Sprite::~Sprite (  ) 

Definition at line 53 of file sprite.cpp.

00054 {
00055 }

Sprite::Sprite ( const Sprite other  )  [private]

Definition at line 40 of file sprite.cpp.

00040                                   :
00041   data(other.data), 
00042   frame(other.frame),
00043   frameidx(other.frameidx),
00044   animation_loops(other.animation_loops),
00045   last_ticks(game_time),
00046   angle(0.0f), // FIXME: this can't be right
00047   color(1.0f, 1.0f, 1.0f, 1.0f),
00048   blend(),
00049   action(other.action)
00050 {
00051 }


Member Function Documentation

SpritePtr Sprite::clone (  )  const

Definition at line 58 of file sprite.cpp.

References Sprite().

00059 {
00060   return SpritePtr(new Sprite(*this));
00061 }

void Sprite::draw ( DrawingContext context,
const Vector pos,
int  layer,
DrawingEffect  effect = NO_EFFECT 
)

Draw sprite, automatically calculates next frame.

Definition at line 133 of file sprite.cpp.

References action, angle, blend, color, DrawingContext::draw_surface(), frameidx, DrawingContext::set_drawing_effect(), SpriteData::Action::surfaces, update(), SpriteData::Action::x_offset, SpriteData::Action::y_offset, and SpriteData::Action::z_order.

00135 {
00136   assert(action != 0);
00137   update();
00138 
00139   context.set_drawing_effect(effect);
00140   context.draw_surface(action->surfaces[frameidx],
00141                        pos - Vector(action->x_offset, action->y_offset),
00142                        angle,
00143                        color,
00144                        blend,
00145                        layer + action->z_order);
00146 }

void Sprite::draw_part ( DrawingContext context,
const Vector source,
const Vector size,
const Vector pos,
int  layer 
)

Definition at line 149 of file sprite.cpp.

References action, DrawingContext::draw_surface_part(), frameidx, SpriteData::Action::surfaces, update(), SpriteData::Action::x_offset, SpriteData::Action::y_offset, and SpriteData::Action::z_order.

00151 {
00152   assert(action != 0);
00153   update();
00154 
00155   context.draw_surface_part(action->surfaces[frameidx], source, size,
00156                             pos - Vector(action->x_offset, action->y_offset),
00157                             layer + action->z_order);
00158 }

void Sprite::set_action ( const std::string &  name,
int  loops = -1 
)

Set action (or state).

Definition at line 64 of file sprite.cpp.

References action, animation_loops, data, frame, frameidx, SpriteData::get_action(), log_debug, and SpriteData::Action::name.

00065 {
00066   if(action && action->name == name)
00067     return;
00068 
00069   const SpriteData::Action* newaction = data.get_action(name);
00070   if(!newaction) {
00071     log_debug << "Action '" << name << "' not found." << std::endl;
00072     return;
00073   }
00074 
00075   action = newaction;
00076   animation_loops = loops;
00077   frame = 0;
00078   frameidx = 0;
00079 }

void Sprite::set_action_continued ( const std::string &  name  ) 

Set action (or state), but keep current frame number, loop counter, etc.

Definition at line 82 of file sprite.cpp.

References action, data, SpriteData::get_action(), log_debug, SpriteData::Action::name, and update().

00083 {
00084   if(action && action->name == name)
00085     return;
00086 
00087   const SpriteData::Action* newaction = data.get_action(name);
00088   if(!newaction) {
00089     log_debug << "Action '" << name << "' not found." << std::endl;
00090     return;
00091   }
00092 
00093   action = newaction;
00094   update();
00095 }

void Sprite::set_animation_loops ( int  loops = -1  )  [inline]

Set number of animation cycles until animation stops.

Definition at line 50 of file sprite.hpp.

References animation_loops.

00051   { animation_loops = loops; }

void Sprite::stop_animation (  )  [inline]

Definition at line 54 of file sprite.hpp.

References animation_loops.

00055   { animation_loops = 0; }

bool Sprite::animation_done (  ) 

Check if animation is stopped or not.

Definition at line 98 of file sprite.cpp.

References animation_loops.

Referenced by update().

00099 {
00100   return animation_loops == 0;
00101 }

float Sprite::get_fps (  )  const [inline]

Definition at line 59 of file sprite.hpp.

References action, and SpriteData::Action::fps.

00060   { return action->fps; }

unsigned int Sprite::get_frames (  )  const [inline]

Get current action total frames.

Definition at line 62 of file sprite.hpp.

References action, and SpriteData::Action::surfaces.

Referenced by get_height(), get_width(), set_frame(), and update().

00063   { return action->surfaces.size(); }

const std::string& Sprite::get_name (  )  const [inline]

Get sprite's name.

Definition at line 65 of file sprite.hpp.

References data, and SpriteData::name.

00066   { return data.name; }

const std::string& Sprite::get_action (  )  const [inline]

Get current action name.

Definition at line 68 of file sprite.hpp.

References action, and SpriteData::Action::name.

00069   { return action->name; }

int Sprite::get_width (  )  const

Definition at line 161 of file sprite.cpp.

References action, frameidx, get_frame(), get_frames(), and SpriteData::Action::surfaces.

00162 {
00163   assert(frameidx < get_frames());
00164   return (int) action->surfaces[get_frame()]->get_width();
00165 }

int Sprite::get_height (  )  const

Definition at line 168 of file sprite.cpp.

References action, frameidx, get_frame(), get_frames(), and SpriteData::Action::surfaces.

00169 {
00170   assert(frameidx < get_frames());
00171   return (int) action->surfaces[get_frame()]->get_height();
00172 }

float Sprite::get_current_hitbox_x_offset (  )  const

return x-offset of current action's hitbox, relative to start of image

Definition at line 175 of file sprite.cpp.

References action, and SpriteData::Action::x_offset.

00176 {
00177   return action->x_offset;
00178 }

float Sprite::get_current_hitbox_y_offset (  )  const

return y-offset of current action's hitbox, relative to start of image

Definition at line 181 of file sprite.cpp.

References action, and SpriteData::Action::y_offset.

00182 {
00183   return action->y_offset;
00184 }

float Sprite::get_current_hitbox_width (  )  const

return width of current action's hitbox

Definition at line 187 of file sprite.cpp.

References action, and SpriteData::Action::hitbox_w.

00188 {
00189   return action->hitbox_w;
00190 }

float Sprite::get_current_hitbox_height (  )  const

return height of current action's hitbox

Definition at line 193 of file sprite.cpp.

References action, and SpriteData::Action::hitbox_h.

00194 {
00195   return action->hitbox_h;
00196 }

Rectf Sprite::get_current_hitbox (  )  const

return current action's hitbox, relative to 0,0

Definition at line 199 of file sprite.cpp.

References action, SpriteData::Action::hitbox_h, SpriteData::Action::hitbox_w, SpriteData::Action::x_offset, and SpriteData::Action::y_offset.

void Sprite::set_angle ( float  angle  ) 

Set the angle of the sprite rotation in degree.

Definition at line 205 of file sprite.cpp.

References angle.

00206 {
00207   angle = a;
00208 }

float Sprite::get_angle (  )  const

Get the angle of the sprite rotation in degree.

Definition at line 211 of file sprite.cpp.

References angle.

00212 {
00213   return angle;
00214 }

void Sprite::set_color ( const Color color  ) 

Definition at line 217 of file sprite.cpp.

References color.

00218 {
00219   color = c;
00220 }

Color Sprite::get_color (  )  const

Definition at line 223 of file sprite.cpp.

References color.

00224 {
00225   return color;
00226 }

void Sprite::set_blend ( const Blend blend  ) 

Definition at line 229 of file sprite.cpp.

References blend.

00230 {
00231   blend = b;
00232 }

Blend Sprite::get_blend (  )  const

Definition at line 235 of file sprite.cpp.

References blend.

00236 {
00237   return blend;
00238 }

unsigned int Sprite::get_frame (  )  const [inline]

Get current frame.

Definition at line 100 of file sprite.hpp.

References frameidx.

Referenced by get_height(), and get_width().

00101   { return frameidx; }

void Sprite::set_frame ( int  frame_  )  [inline]

Set current frame.

Definition at line 103 of file sprite.hpp.

References frame, frameidx, and get_frames().

00104   {
00105     this->frame = 0;
00106     this->frameidx = frame_ % get_frames();
00107   }

SurfacePtr Sprite::get_frame ( unsigned int  frame_  )  [inline]

Definition at line 108 of file sprite.hpp.

References action, and SpriteData::Action::surfaces.

00109   {
00110     assert(frame_ < action->surfaces.size());
00111     return action->surfaces[frame_];
00112   }

bool Sprite::has_action ( const std::string &  name  )  [inline]

Definition at line 114 of file sprite.hpp.

References data, and SpriteData::get_action().

00115   {
00116     return (data.get_action(name) != NULL);
00117   }

void Sprite::update (  )  [private]

Definition at line 104 of file sprite.cpp.

References action, animation_done(), animation_loops, SpriteData::Action::fps, frame, frameidx, game_time, get_frames(), and last_ticks.

Referenced by draw(), draw_part(), and set_action_continued().

00105 {
00106   float frame_inc = action->fps * (game_time - last_ticks);
00107   last_ticks = game_time;
00108 
00109   frame += frame_inc;
00110 
00111   while(frame >= 1.0f) {
00112     frame -= 1.0f;
00113     frameidx++;
00114   }
00115 
00116   while(frameidx >= get_frames()) {
00117     frameidx -= get_frames();
00118     animation_loops--;
00119     if(animation_done()) {
00120       break;
00121     }
00122   }
00123 
00124   if(animation_done()) {
00125     frame = 0;
00126     frameidx = get_frames()-1;
00127   }
00128 
00129   assert(frameidx < get_frames());
00130 }

Sprite& Sprite::operator= ( const Sprite  )  [private]


Member Data Documentation

SpriteData& Sprite::data [private]

Definition at line 122 of file sprite.hpp.

Referenced by get_name(), has_action(), set_action(), set_action_continued(), and Sprite().

float Sprite::frame [private]

Definition at line 125 of file sprite.hpp.

Referenced by set_action(), set_frame(), and update().

unsigned int Sprite::frameidx [private]

Definition at line 127 of file sprite.hpp.

Referenced by draw(), draw_part(), get_frame(), get_height(), get_width(), set_action(), set_frame(), and update().

int Sprite::animation_loops [private]

Definition at line 128 of file sprite.hpp.

Referenced by animation_done(), set_action(), set_animation_loops(), stop_animation(), and update().

float Sprite::last_ticks [private]

Definition at line 129 of file sprite.hpp.

Referenced by Sprite(), and update().

float Sprite::angle [private]

Definition at line 130 of file sprite.hpp.

Referenced by draw(), get_angle(), and set_angle().

Color Sprite::color [private]

Definition at line 131 of file sprite.hpp.

Referenced by draw(), get_color(), and set_color().

Blend Sprite::blend [private]

Definition at line 132 of file sprite.hpp.

Referenced by draw(), get_blend(), and set_blend().

const SpriteData::Action* Sprite::action [private]

Definition at line 134 of file sprite.hpp.

Referenced by draw(), draw_part(), get_action(), get_current_hitbox(), get_current_hitbox_height(), get_current_hitbox_width(), get_current_hitbox_x_offset(), get_current_hitbox_y_offset(), get_fps(), get_frame(), get_frames(), get_height(), get_width(), set_action(), set_action_continued(), Sprite(), and update().


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