remove const methods | add copy constructor

This commit is contained in:
Adrien Marquès 2020-02-04 20:45:42 +01:00
parent 47429e5081
commit 2b81a72542
2 changed files with 19 additions and 6 deletions

View File

@ -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);
@ -89,7 +99,7 @@ void xSprite::setSurface(SDL_Surface *s){
_mutex.unlock(); _mutex.unlock();
} }
SDL_Surface* xSprite::surface(){ const SDL_Surface* xSprite::surface(){
_mutex.lock(); _mutex.lock();
SDL_Surface* surface = _surface; SDL_Surface* surface = _surface;
_mutex.unlock(); _mutex.unlock();
@ -107,7 +117,7 @@ void xSprite::setClip(int x, int y, int w, int h){
} }
/** gets sprite clip */ /** gets sprite clip */
SDL_Rect xSprite::clip(){ const SDL_Rect xSprite::clip(){
_mutex.lock(); _mutex.lock();
const SDL_Rect clip = _clip; const SDL_Rect clip = _clip;
_mutex.unlock(); _mutex.unlock();
@ -124,7 +134,7 @@ void xSprite::project(int x, int y, int w, int h){
project( (SDL_Rect){x, y, w, h} ); project( (SDL_Rect){x, y, w, h} );
} }
/** gets sprite projection */ /** gets sprite projection */
SDL_Rect xSprite::projection(){ const SDL_Rect xSprite::projection(){
_mutex.lock(); _mutex.lock();
const SDL_Rect projection = _projection; const SDL_Rect projection = _projection;
_mutex.unlock(); _mutex.unlock();

View File

@ -13,6 +13,9 @@
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
@ -31,17 +34,17 @@
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
SDL_Surface* 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); void setClip(int x, int y, int w, int h);
SDL_Rect clip(); 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); void project(int x, int y, int w, int h);
SDL_Rect projection(); const SDL_Rect projection();
// implement xDrawable // implement xDrawable
virtual void draw(SDL_Renderer* renderer) override; virtual void draw(SDL_Renderer* renderer) override;