Compare commits
7 Commits
7a6022e694
...
2b81a72542
Author | SHA1 | Date |
---|---|---|
Adrien Marquès | 2b81a72542 | |
Adrien Marquès | 47429e5081 | |
Adrien Marquès | cb1a862b6f | |
Adrien Marquès | 0b7e7a2948 | |
Adrien Marquès | d1e6a1e9dc | |
Adrien Marquès | cc276e1c2a | |
Adrien Marquès | 49deb9e868 |
|
@ -281,8 +281,7 @@ void xApplication::render(){
|
||||||
|
|
||||||
|
|
||||||
/* 4. draw every sprite */
|
/* 4. draw every sprite */
|
||||||
set<xDrawable*>::iterator it;
|
for( set<xDrawable*>::iterator it = _sprites.begin() ; it != _sprites.end() ; it++ ){
|
||||||
for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){
|
|
||||||
xDrawable* sprite = *it;
|
xDrawable* sprite = *it;
|
||||||
sprite->draw(_renderer);
|
sprite->draw(_renderer);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,16 @@ xSprite::~xSprite(){
|
||||||
_mutex.unlock();
|
_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 */
|
/** color sprite */
|
||||||
xSprite::xSprite(const int rgba[]){
|
xSprite::xSprite(const int rgba[]){
|
||||||
this->setSurface(rgba);
|
this->setSurface(rgba);
|
||||||
|
@ -16,11 +26,28 @@ xSprite::xSprite(const int rgba[]){
|
||||||
xSprite::xSprite(const char *url){
|
xSprite::xSprite(const char *url){
|
||||||
this->setSurface(url);
|
this->setSurface(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** image sprite with clip */
|
/** image sprite with clip */
|
||||||
xSprite::xSprite(const char *url, SDL_Rect clip){
|
xSprite::xSprite(const char *url, SDL_Rect clip){
|
||||||
this->setSurface(url);
|
this->setSurface(url);
|
||||||
this->setClip(clip);
|
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 */
|
/** update sprite to rhb color */
|
||||||
void xSprite::setSurface(const int rgba[]){
|
void xSprite::setSurface(const int rgba[]){
|
||||||
|
@ -72,6 +99,12 @@ void xSprite::setSurface(SDL_Surface *s){
|
||||||
|
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
const SDL_Surface* xSprite::surface(){
|
||||||
|
_mutex.lock();
|
||||||
|
SDL_Surface* surface = _surface;
|
||||||
|
_mutex.unlock();
|
||||||
|
return surface;
|
||||||
|
}
|
||||||
|
|
||||||
/** set sprite clip */
|
/** set sprite clip */
|
||||||
void xSprite::setClip(SDL_Rect clip){
|
void xSprite::setClip(SDL_Rect clip){
|
||||||
|
@ -79,6 +112,17 @@ void xSprite::setClip(SDL_Rect clip){
|
||||||
_clip = clip;
|
_clip = clip;
|
||||||
_mutex.unlock();
|
_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 */
|
/** set sprite projection */
|
||||||
void xSprite::project(SDL_Rect dest){
|
void xSprite::project(SDL_Rect dest){
|
||||||
|
@ -86,6 +130,16 @@ void xSprite::project(SDL_Rect dest){
|
||||||
_projection = dest;
|
_projection = dest;
|
||||||
_mutex.unlock();
|
_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 */
|
/** draws to renderer */
|
||||||
void xSprite::draw(SDL_Renderer* renderer){
|
void xSprite::draw(SDL_Renderer* renderer){
|
||||||
|
|
|
@ -13,31 +13,45 @@
|
||||||
class xSprite : public xDrawable{
|
class xSprite : public xDrawable{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
// copy constructor
|
||||||
|
xSprite(const xSprite& other);
|
||||||
|
|
||||||
// color sprite
|
// color sprite
|
||||||
xSprite(const int rgba[]);
|
xSprite(const int rgba[]);
|
||||||
// image sprite
|
// image sprite
|
||||||
xSprite(const char *url);
|
xSprite(const char *url);
|
||||||
// image with default clip
|
// image with default clip
|
||||||
xSprite(const char *url, SDL_Rect 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();
|
virtual ~xSprite();
|
||||||
|
|
||||||
// replace surface
|
// replace surface
|
||||||
void setSurface(const int rgba[]); // color sprite
|
void setSurface(const int rgba[]); // color sprite
|
||||||
void setSurface(const char *url); // image sprite
|
void setSurface(const char *url); // image sprite
|
||||||
void setSurface(SDL_Surface *t); // copy surface
|
void setSurface(SDL_Surface *t); // copy surface
|
||||||
|
const SDL_Surface* surface();
|
||||||
|
|
||||||
// sets the sprite clip
|
// sets the sprite clip
|
||||||
void setClip(SDL_Rect clip);
|
void setClip(SDL_Rect clip);
|
||||||
|
void setClip(int x, int y, int w, int h);
|
||||||
|
const SDL_Rect clip();
|
||||||
|
|
||||||
// set projection into scene
|
// set projection into scene
|
||||||
void project(SDL_Rect dest);
|
void project(SDL_Rect dest);
|
||||||
|
void project(int x, int y, int w, int h);
|
||||||
|
const SDL_Rect projection();
|
||||||
|
|
||||||
// implement xDrawable
|
// implement xDrawable
|
||||||
virtual void draw(SDL_Renderer* renderer) override;
|
virtual void draw(SDL_Renderer* renderer) override;
|
||||||
private:
|
private:
|
||||||
SDL_Surface* _surface = NULL;
|
SDL_Surface* _surface = NULL;
|
||||||
SDL_Rect _clip;
|
SDL_Rect _clip { 0, 0, 0, 0 };
|
||||||
SDL_Rect _projection;
|
SDL_Rect _projection { 0, 0, 0, 0 };
|
||||||
|
|
||||||
mutex _mutex;
|
mutex _mutex;
|
||||||
|
|
||||||
|
|
|
@ -25,11 +25,24 @@ void xSpriteGroup::remove(xSprite* sprite){
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** set absolute move for each sprite */
|
/** apply relative displace for each sprite */
|
||||||
void xSpriteGroup::moveTo(int x, int y){
|
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();
|
_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();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,6 +50,29 @@ void xSpriteGroup::moveTo(int x, int y){
|
||||||
void xSpriteGroup::draw(SDL_Renderer* renderer){
|
void xSpriteGroup::draw(SDL_Renderer* renderer){
|
||||||
_mutex.lock();
|
_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;
|
set<xSprite*>::iterator it;
|
||||||
for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){
|
for( it = _sprites.begin() ; it != _sprites.end() ; it++ ){
|
||||||
(*it)->draw(renderer);
|
(*it)->draw(renderer);
|
||||||
|
@ -47,18 +83,24 @@ void xSpriteGroup::draw(SDL_Renderer* renderer){
|
||||||
/** animation process */
|
/** animation process */
|
||||||
void xSpriteGroup::tick(const uint32_t ticks){
|
void xSpriteGroup::tick(const uint32_t ticks){
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
uint32_t time_index = ticks / _animation_interval;
|
const uint32_t time_index = ticks / _animation_interval;
|
||||||
_active_sprite = time_index % _sprites.size();
|
_active_sprite = time_index % _sprites.size();
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** orchestrate the animation */
|
/** orchestrate the animation */
|
||||||
void xSpriteGroup::animate(uint32_t interval){
|
void xSpriteGroup::animate(uint32_t interval){
|
||||||
|
_mutex.lock();
|
||||||
|
_animated = true;
|
||||||
_animation_interval = interval;
|
_animation_interval = interval;
|
||||||
|
_active_sprite = 0;
|
||||||
xApplication::get()->addOrchestrable(this);
|
xApplication::get()->addOrchestrable(this);
|
||||||
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** stops orchestrating the animation */
|
/** stops orchestrating the animation */
|
||||||
void xSpriteGroup::freeze(){
|
void xSpriteGroup::freeze(){
|
||||||
|
_mutex.lock();
|
||||||
xApplication::get()->removeOrchestrable(this);
|
xApplication::get()->removeOrchestrable(this);
|
||||||
|
_mutex.unlock();
|
||||||
}
|
}
|
|
@ -23,8 +23,9 @@
|
||||||
void remove(xSprite* sprite);
|
void remove(xSprite* sprite);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
// set absolute move for each sprite
|
// apply relative displace for each sprite
|
||||||
void moveTo(int x, int y);
|
void displace(SDL_Rect relative_displace);
|
||||||
|
void displace(int x, int y, int w, int h);
|
||||||
|
|
||||||
// implement xDrawable
|
// implement xDrawable
|
||||||
virtual void draw(SDL_Renderer* renderer) override;
|
virtual void draw(SDL_Renderer* renderer) override;
|
||||||
|
@ -37,13 +38,13 @@
|
||||||
void freeze();
|
void freeze();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SDL_Rect _move;
|
|
||||||
|
|
||||||
set<xSprite*> _sprites;
|
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;
|
size_t _active_sprite = 0;
|
||||||
uint32_t _animation_interval;
|
uint32_t _animation_interval = 0;
|
||||||
|
|
||||||
mutex _mutex;
|
mutex _mutex;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue