Compare commits

..

7 Commits

5 changed files with 125 additions and 15 deletions

View File

@ -281,8 +281,7 @@ void xApplication::render(){
/* 4. draw every sprite */
set<xDrawable*>::iterator it;
for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){
for( set<xDrawable*>::iterator it = _sprites.begin() ; it != _sprites.end() ; it++ ){
xDrawable* sprite = *it;
sprite->draw(_renderer);
}

View File

@ -7,6 +7,16 @@ xSprite::~xSprite(){
_mutex.unlock();
}
/** copy constructor */
xSprite::xSprite(const xSprite& other){
// copy surface
if( other._surface != NULL ){
this->setSurface( SDL_ConvertSurface(other._surface, other._surface->format, SDL_SWSURFACE) );
}
this->setClip(other._clip);
this->project(other._projection);
}
/** color sprite */
xSprite::xSprite(const int rgba[]){
this->setSurface(rgba);
@ -16,11 +26,28 @@ xSprite::xSprite(const int rgba[]){
xSprite::xSprite(const char *url){
this->setSurface(url);
}
/** image sprite with clip */
xSprite::xSprite(const char *url, SDL_Rect clip){
this->setSurface(url);
this->setClip(clip);
}
xSprite::xSprite(const char *url, int clip_x, int clip_y, int clip_w, int clip_h){
this->setSurface(url);
this->setClip(clip_x, clip_y, clip_w, clip_h);
}
/** image sprite with clip and projection */
xSprite::xSprite(const char *url, SDL_Rect clip, SDL_Rect projection){
this->setSurface(url);
this->setClip(clip);
this->project(projection);
}
xSprite::xSprite(const char *url, int clip_x, int clip_y, int clip_w, int clip_h, int project_x, int project_y, int project_w, int project_h){
this->setSurface(url);
this->setClip(clip_x, clip_y, clip_w, clip_h);
this->project(project_x, project_y, project_w, project_h);
}
/** update sprite to rhb color */
void xSprite::setSurface(const int rgba[]){
@ -72,6 +99,12 @@ void xSprite::setSurface(SDL_Surface *s){
_mutex.unlock();
}
const SDL_Surface* xSprite::surface(){
_mutex.lock();
SDL_Surface* surface = _surface;
_mutex.unlock();
return surface;
}
/** set sprite clip */
void xSprite::setClip(SDL_Rect clip){
@ -79,6 +112,17 @@ void xSprite::setClip(SDL_Rect clip){
_clip = clip;
_mutex.unlock();
}
void xSprite::setClip(int x, int y, int w, int h){
setClip( (SDL_Rect){x, y, w, h} );
}
/** gets sprite clip */
const SDL_Rect xSprite::clip(){
_mutex.lock();
const SDL_Rect clip = _clip;
_mutex.unlock();
return clip;
}
/** set sprite projection */
void xSprite::project(SDL_Rect dest){
@ -86,6 +130,16 @@ void xSprite::project(SDL_Rect dest){
_projection = dest;
_mutex.unlock();
}
void xSprite::project(int x, int y, int w, int h){
project( (SDL_Rect){x, y, w, h} );
}
/** gets sprite projection */
const SDL_Rect xSprite::projection(){
_mutex.lock();
const SDL_Rect projection = _projection;
_mutex.unlock();
return projection;
}
/** draws to renderer */
void xSprite::draw(SDL_Renderer* renderer){

View File

@ -13,31 +13,45 @@
class xSprite : public xDrawable{
public:
// copy constructor
xSprite(const xSprite& other);
// color sprite
xSprite(const int rgba[]);
// image sprite
xSprite(const char *url);
// image with default clip
xSprite(const char *url, SDL_Rect clip);
// image with default clip
xSprite(const char *url, int clip_x, int clip_y, int clip_w, int clip_h);
// image with default clip and projection
xSprite(const char *url, SDL_Rect clip, SDL_Rect projection);
// image with default clip and projection
xSprite(const char *url, int clip_x, int clip_y, int clip_w, int clip_h, int project_x, int project_y, int project_w, int project_h);
virtual ~xSprite();
// replace surface
void setSurface(const int rgba[]); // color sprite
void setSurface(const char *url); // image sprite
void setSurface(SDL_Surface *t); // copy surface
const SDL_Surface* surface();
// sets the sprite clip
void setClip(SDL_Rect clip);
void setClip(int x, int y, int w, int h);
const SDL_Rect clip();
// set projection into scene
void project(SDL_Rect dest);
void project(int x, int y, int w, int h);
const SDL_Rect projection();
// implement xDrawable
virtual void draw(SDL_Renderer* renderer) override;
private:
SDL_Surface* _surface = NULL;
SDL_Rect _clip;
SDL_Rect _projection;
SDL_Rect _clip { 0, 0, 0, 0 };
SDL_Rect _projection { 0, 0, 0, 0 };
mutex _mutex;

View File

@ -25,11 +25,24 @@ void xSpriteGroup::remove(xSprite* sprite){
_mutex.unlock();
}
/** set absolute move for each sprite */
void xSpriteGroup::moveTo(int x, int y){
/** apply relative displace for each sprite */
void xSpriteGroup::displace(int x, int y, int w, int h){
displace( (SDL_Rect){x,y,w,h} );
}
void xSpriteGroup::displace(SDL_Rect relative_displace){
_mutex.lock();
_move.x = x;
_move.y = y;
for( set<xSprite*>::iterator it = _sprites.begin() ; it != _sprites.end() ; it++ ) {
xSprite* sprite = (*it);
const SDL_Rect actual_projection = sprite->projection();
sprite->project(
actual_projection.x + relative_displace.x,
actual_projection.y + relative_displace.y,
actual_projection.w + relative_displace.w,
actual_projection.h + relative_displace.h
);
}
_mutex.unlock();
}
@ -37,6 +50,29 @@ void xSpriteGroup::moveTo(int x, int y){
void xSpriteGroup::draw(SDL_Renderer* renderer){
_mutex.lock();
// only draw active sprite if animated
if( _animated ){
// invalid active index
if( _active_sprite >= _sprites.size() ) {
_active_sprite = 0;
_mutex.unlock();
return;
}
set<xSprite*>::iterator at_index = _sprites.begin();
advance(at_index, _active_sprite);
if( at_index == _sprites.end() ){
_mutex.unlock();
return;
}
(*at_index)->draw(renderer);
_mutex.unlock();
return;
}
// else draw every sprite
set<xSprite*>::iterator it;
for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){
(*it)->draw(renderer);
@ -47,18 +83,24 @@ void xSpriteGroup::draw(SDL_Renderer* renderer){
/** animation process */
void xSpriteGroup::tick(const uint32_t ticks){
_mutex.lock();
uint32_t time_index = ticks / _animation_interval;
const uint32_t time_index = ticks / _animation_interval;
_active_sprite = time_index % _sprites.size();
_mutex.unlock();
}
/** orchestrate the animation */
void xSpriteGroup::animate(uint32_t interval){
_mutex.lock();
_animated = true;
_animation_interval = interval;
_active_sprite = 0;
xApplication::get()->addOrchestrable(this);
_mutex.unlock();
}
/** stops orchestrating the animation */
void xSpriteGroup::freeze(){
_mutex.lock();
xApplication::get()->removeOrchestrable(this);
_mutex.unlock();
}

View File

@ -23,8 +23,9 @@
void remove(xSprite* sprite);
void clear();
// set absolute move for each sprite
void moveTo(int x, int y);
// apply relative displace for each sprite
void displace(SDL_Rect relative_displace);
void displace(int x, int y, int w, int h);
// implement xDrawable
virtual void draw(SDL_Renderer* renderer) override;
@ -37,13 +38,13 @@
void freeze();
protected:
SDL_Rect _move;
set<xSprite*> _sprites;
// active sprite for animation ; equal to 0 if not an animated sprite
// if not animated, draw all sprites, else draw one at a time
bool _animated = false;
// active sprite for animation
size_t _active_sprite = 0;
uint32_t _animation_interval;
uint32_t _animation_interval = 0;
mutex _mutex;