76 lines
1.7 KiB
C++
76 lines
1.7 KiB
C++
#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){
|
|
_mutex_frames.try_lock();
|
|
_frames.push_back( (SDL_Rect){
|
|
clip.x,
|
|
clip.y,
|
|
clip.w,
|
|
clip.h
|
|
} );
|
|
_mutex_frames.unlock();
|
|
}
|
|
|
|
/** clears all frames */
|
|
void xSpriteAnimation::clearFrames(){
|
|
_mutex_frames.try_lock();
|
|
_frames.erase(_frames.begin(), _frames.end());
|
|
_mutex_frames.unlock();
|
|
}
|
|
|
|
/** 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 */
|
|
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();
|
|
}
|
|
|
|
|
|
/** adds the animation to the render */
|
|
void xSpriteAnimation::start(uint32_t interval){
|
|
_interval = interval;
|
|
xApplication::get()->addOrchestrable(this);
|
|
}
|
|
|
|
/** stops the animation */
|
|
void xSpriteAnimation::stop(){
|
|
xApplication::get()->removeOrchestrable(this);
|
|
} |