#include <sprite.hpp>
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) | |
Sprite & | operator= (const Sprite &) |
Private Attributes | |
SpriteData & | data |
float | frame |
unsigned int | frameidx |
int | animation_loops |
float | last_ticks |
float | angle |
Color | color |
Blend | blend |
const SpriteData::Action * | action |
Definition at line 28 of file sprite.hpp.
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 | ( | ) |
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 }
SpritePtr Sprite::clone | ( | ) | const |
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] |
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] |
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().
const std::string& Sprite::get_name | ( | ) | const [inline] |
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.
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.
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.
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.
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.
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.
00200 { 00201 return Rectf(action->x_offset, action->y_offset, action->x_offset + action->hitbox_w, action->y_offset + action->hitbox_h); 00202 }
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 | ) |
Color Sprite::get_color | ( | ) | const |
void Sprite::set_blend | ( | const Blend & | blend | ) |
Blend Sprite::get_blend | ( | ) | const |
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 }
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] |
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] |
float Sprite::angle [private] |
Color Sprite::color [private] |
Blend Sprite::blend [private] |
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().