Compare commits

..

No commits in common. "2b81a725427fd4bf388c7b525adb4df205c0a7ae" and "7a6022e69410e37a1c76422bdffd01f70c3e121c" have entirely different histories.

5 changed files with 15 additions and 125 deletions

View File

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

View File

@ -7,16 +7,6 @@ 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);
@ -26,28 +16,11 @@ 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[]){
@ -99,12 +72,6 @@ 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){
@ -112,17 +79,6 @@ 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){
@ -130,16 +86,6 @@ 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,45 +13,31 @@
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 { 0, 0, 0, 0 };
SDL_Rect _projection { 0, 0, 0, 0 };
SDL_Rect _clip;
SDL_Rect _projection;
mutex _mutex;

View File

@ -25,24 +25,11 @@ void xSpriteGroup::remove(xSprite* sprite){
_mutex.unlock();
}
/** 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){
/** set absolute move for each sprite */
void xSpriteGroup::moveTo(int x, int y){
_mutex.lock();
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
);
}
_move.x = x;
_move.y = y;
_mutex.unlock();
}
@ -50,29 +37,6 @@ void xSpriteGroup::displace(SDL_Rect relative_displace){
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);
@ -83,24 +47,18 @@ void xSpriteGroup::draw(SDL_Renderer* renderer){
/** animation process */
void xSpriteGroup::tick(const uint32_t ticks){
_mutex.lock();
const uint32_t time_index = ticks / _animation_interval;
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,9 +23,8 @@
void remove(xSprite* sprite);
void clear();
// apply relative displace for each sprite
void displace(SDL_Rect relative_displace);
void displace(int x, int y, int w, int h);
// set absolute move for each sprite
void moveTo(int x, int y);
// implement xDrawable
virtual void draw(SDL_Renderer* renderer) override;
@ -38,13 +37,13 @@
void freeze();
protected:
SDL_Rect _move;
set<xSprite*> _sprites;
// if not animated, draw all sprites, else draw one at a time
bool _animated = false;
// active sprite for animation
// active sprite for animation ; equal to 0 if not an animated sprite
size_t _active_sprite = 0;
uint32_t _animation_interval = 0;
uint32_t _animation_interval;
mutex _mutex;