2020-02-02 15:18:12 +00:00
|
|
|
#include "xSpriteGroup.h"
|
|
|
|
|
|
|
|
/** clean SDL objects */
|
|
|
|
xSpriteGroup::~xSpriteGroup(){
|
|
|
|
_mutex.lock();
|
|
|
|
_sprites.clear();
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** default constructor */
|
|
|
|
xSpriteGroup::xSpriteGroup(){
|
|
|
|
}
|
|
|
|
|
|
|
|
/** adds a sprite to the group */
|
|
|
|
void xSpriteGroup::add(xSprite* sprite){
|
|
|
|
_mutex.lock();
|
|
|
|
_sprites.insert(sprite);
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** removes a sprite from the group */
|
|
|
|
void xSpriteGroup::remove(xSprite* sprite){
|
|
|
|
_mutex.lock();
|
|
|
|
_sprites.erase(sprite);
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
2020-02-04 17:57:31 +00:00
|
|
|
/** set relative move for each sprite */
|
|
|
|
void xSpriteGroup::displace(int x, int y, int w, int h){
|
2020-02-02 15:18:12 +00:00
|
|
|
_mutex.lock();
|
2020-02-04 17:57:31 +00:00
|
|
|
_displace.x = x;
|
|
|
|
_displace.y = y;
|
|
|
|
_displace.w = w;
|
|
|
|
_displace.h = h;
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
/** set relative move for each sprite */
|
|
|
|
void xSpriteGroup::displace(SDL_Rect displace){
|
|
|
|
_mutex.lock();
|
|
|
|
_displace = displace;
|
2020-02-02 15:18:12 +00:00
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** draws to renderer */
|
|
|
|
void xSpriteGroup::draw(SDL_Renderer* renderer){
|
|
|
|
_mutex.lock();
|
|
|
|
|
2020-02-04 17:57:31 +00:00
|
|
|
// only draw active sprite if animated
|
|
|
|
if( _animated ){
|
|
|
|
set<xSprite*>::iterator at_index = _sprites.begin();
|
|
|
|
advance(at_index, _active_sprite);
|
|
|
|
if( at_index == _sprites.end() ){
|
|
|
|
_mutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
draw_sprite(renderer, *at_index);
|
|
|
|
_mutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else draw every sprite
|
2020-02-02 15:18:12 +00:00
|
|
|
set<xSprite*>::iterator it;
|
|
|
|
for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){
|
2020-02-04 17:57:31 +00:00
|
|
|
draw_sprite(renderer, *it);
|
2020-02-02 15:18:12 +00:00
|
|
|
}
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** animation process */
|
|
|
|
void xSpriteGroup::tick(const uint32_t ticks){
|
|
|
|
_mutex.lock();
|
2020-02-04 17:57:31 +00:00
|
|
|
const uint32_t time_index = ticks / _animation_interval;
|
2020-02-02 15:18:12 +00:00
|
|
|
_active_sprite = time_index % _sprites.size();
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** orchestrate the animation */
|
|
|
|
void xSpriteGroup::animate(uint32_t interval){
|
2020-02-04 17:57:31 +00:00
|
|
|
_mutex.lock();
|
|
|
|
_animated = true;
|
2020-02-02 15:18:12 +00:00
|
|
|
_animation_interval = interval;
|
|
|
|
xApplication::get()->addOrchestrable(this);
|
2020-02-04 17:57:31 +00:00
|
|
|
_mutex.unlock();
|
2020-02-02 15:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** stops orchestrating the animation */
|
|
|
|
void xSpriteGroup::freeze(){
|
2020-02-04 17:57:31 +00:00
|
|
|
_mutex.lock();
|
2020-02-02 15:18:12 +00:00
|
|
|
xApplication::get()->removeOrchestrable(this);
|
2020-02-04 17:57:31 +00:00
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* draws a sprite using the relative displace */
|
|
|
|
void xSpriteGroup::draw_sprite(SDL_Renderer* renderer, xSprite* sprite) {
|
|
|
|
const SDL_Rect projection = sprite->projection();
|
|
|
|
const SDL_Rect clip = sprite->clip();
|
|
|
|
|
|
|
|
// displace sprite projection with group move
|
|
|
|
const SDL_Rect displace {
|
|
|
|
projection.x + _displace.x,
|
|
|
|
projection.y + _displace.y,
|
|
|
|
projection.w + _displace.w,
|
|
|
|
projection.h + _displace.h
|
|
|
|
};
|
|
|
|
|
|
|
|
SDL_RenderCopy(
|
|
|
|
renderer,
|
|
|
|
SDL_CreateTextureFromSurface(renderer, sprite->surface()),
|
|
|
|
&clip,
|
|
|
|
&displace
|
|
|
|
);
|
2020-02-02 15:18:12 +00:00
|
|
|
}
|