2019-11-05 20:45:41 +00:00
|
|
|
#include "xSpriteAnimation.h"
|
|
|
|
|
|
|
|
/** clean SDL objects and frames */
|
|
|
|
xSpriteAnimation::~xSpriteAnimation(){
|
|
|
|
_mutex.try_lock();
|
|
|
|
SDL_FreeSurface( _surface );
|
|
|
|
_frames.erase( _frames.begin(), _frames.end() );
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** builds an animation */
|
|
|
|
xSpriteAnimation::xSpriteAnimation(const char *url, SDL_Rect dest)
|
|
|
|
: xSprite( url ){
|
|
|
|
this->dimensions(dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** builds an animation from an existing surface */
|
|
|
|
xSpriteAnimation::xSpriteAnimation(SDL_Surface *s, SDL_Rect dest)
|
|
|
|
: xSprite(s) {
|
|
|
|
this->dimensions(dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** adds an animation frame */
|
|
|
|
void xSpriteAnimation::addFrame(SDL_Rect clip){
|
2019-11-06 21:48:25 +00:00
|
|
|
_mutex_frames.try_lock();
|
2019-11-05 20:45:41 +00:00
|
|
|
_frames.push_back( (SDL_Rect){
|
|
|
|
clip.x,
|
|
|
|
clip.y,
|
|
|
|
clip.w,
|
|
|
|
clip.h
|
|
|
|
} );
|
2019-11-06 21:48:25 +00:00
|
|
|
_mutex_frames.unlock();
|
2019-11-05 20:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** clears all frames */
|
|
|
|
void xSpriteAnimation::clearFrames(){
|
2019-11-06 21:48:25 +00:00
|
|
|
_mutex_frames.try_lock();
|
2019-11-05 20:45:41 +00:00
|
|
|
_frames.erase(_frames.begin(), _frames.end());
|
2019-11-06 21:48:25 +00:00
|
|
|
_mutex_frames.unlock();
|
2019-11-05 20:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** draw to scene */
|
|
|
|
void xSpriteAnimation::draw(SDL_Renderer* renderer){
|
|
|
|
_mutex.try_lock();
|
|
|
|
SDL_RenderCopy(
|
|
|
|
renderer,
|
|
|
|
SDL_CreateTextureFromSurface(renderer, this->surface()),
|
|
|
|
this->src(),
|
|
|
|
this->dst()
|
|
|
|
);
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** animation process */
|
2019-11-06 21:48:25 +00:00
|
|
|
void xSpriteAnimation::tick(const uint32_t ticks){
|
|
|
|
_mutex_frames.try_lock();
|
|
|
|
uint32_t time_index = ticks / _interval;
|
|
|
|
size_t frame_index = time_index % _frames.size();
|
|
|
|
|
|
|
|
// update current frame clip
|
|
|
|
_src = _frames.at(frame_index);
|
|
|
|
_mutex_frames.unlock();
|
2019-11-05 20:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** adds the animation to the render */
|
2019-11-06 21:48:25 +00:00
|
|
|
void xSpriteAnimation::start(uint32_t interval){
|
|
|
|
_interval = interval;
|
|
|
|
xApplication::get()->addOrchestrable(this);
|
2019-11-05 20:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** stops the animation */
|
|
|
|
void xSpriteAnimation::stop(){
|
2019-11-06 21:48:25 +00:00
|
|
|
xApplication::get()->removeOrchestrable(this);
|
2019-11-05 20:45:41 +00:00
|
|
|
}
|