#include "xSprite.h" /** clean SDL objects */ xSprite::~xSprite(){ _mutex.try_lock(); SDL_FreeSurface( _surface ); _mutex.unlock(); } /** empty sprite */ xSprite::xSprite(){ _type = "basic"; } /** color sprite */ xSprite::xSprite(const int rgba[]){ _type = "basic"; this->setSurface(rgba); } /** image sprite */ xSprite::xSprite(const char *url){ _type = "basic"; this->setSurface(url); } /** texture copy sprite */ xSprite::xSprite(SDL_Surface *s){ _type = "basic"; this->setSurface(s); } /** update sprite to rhb color */ void xSprite::setSurface(const int rgba[]){ _mutex.try_lock(); if( _surface != NULL ){ SDL_FreeSurface( _surface ); _surface = NULL; } _surface = SDL_CreateRGBSurface(0, 0, 0, 32, rgba[0], rgba[1], rgba[2], rgba[3]); // On cree la texture a partir de la surface // _surface = SDL_CreateTextureFromSurface(_manager->renderer(), surf); if( _surface == NULL ) { throw runtime_error("[xSprite] setSurface(rgba) -> NULL surface"); } _mutex.unlock(); } /** update sprite to image */ void xSprite::setSurface(const char *url){ _mutex.try_lock(); if( _surface != NULL ){ SDL_FreeSurface( _surface ); _surface = NULL; } _surface = IMG_Load(url); if( _surface == NULL ) { throw runtime_error("[xSprite] setSurface(url) -> NULL surface"); } _mutex.unlock(); } /* [SETTEXTURE] Modification de la texture texture =========================================================*/ void xSprite::setSurface(SDL_Surface *s){ _mutex.try_lock(); if( _surface != NULL ){ SDL_FreeSurface( _surface ); _surface = NULL; } _surface = s; if( _surface == NULL ) { throw runtime_error("[xSprite] setSurface(surface) -> NULL surface"); } _mutex.unlock(); } /** move sprite with a (x,y) velocity * - int[0]: new * - int[1]: can move on y */ // bool* xSprite::move(double xSpeed, double ySpeed){ // _mutex.try_lock(); // bool result[2] = {false, false}; // /* logic variables */ // int incrx = x; // int incry = y; // bool moveFasterOnY = abs(x) <= abs(y); // int signofx = (x==0) ? 0 : x / abs(x); // int signofy = (y==0) ? 0 : y / abs(y); // // while velocity not null // while( incrx != 0 || incry != 0 ){ // result[0] = incrx; // result[1] = incry; // /* (3) Si on peut aller a la destination */ // // if( !_manager->hit(this, incrx, incry) ){ // // _dst.x += incrx; // // _dst.y += incry; // // // cerr << ">>> not locked <<<" << endl; // // result[0] = incrx; // // result[1] = incry; // // _mutex.unlock(); // // return result; // // } // /* (4) Sinon, on decremente les deplacements 'alternativement' */ // if( moveFasterOnY ){ // Si on a plus de mouvement horizontal // if( signofx != 0 && incrx != 0 ) // si la vel. sur x n'est pas nulle // incrx -= signofx; // on diminue la vel. sur x // else if( signofy != 0 && incry != 0 ) // sinon si vel. sur y n'est pas nulle // incry -= signofy; // on diminue la vel. sur y // else // sinon, velocite nulle // return result; // On arrete de chercher // }else{ // Si on a plus de mouvement vertical // if( signofy != 0 && incry != 0 ) // si la vel. sur y n'est pas nulle // incry -= signofy; // on diminue la vel. sur y // else if( signofx != 0 && incrx != 0 ) // sinon si vel. sur x n'est pas nulle // incrx -= signofx; // on diminue la vel. sur x // else // sinon, velocite nulle // return result; // On arrete de chercher // } // // if( !_manager->hit(this, 0, 1) ){ // // cerr << "locked from (" << _dst.x << ", " << _dst.y << ") to (" << incrx << ", " << incry << ")" << endl; // // } // } // // retour // _mutex.unlock(); // return result; // } /** get/set sprite type */ string xSprite::getType(){ return _type; } void xSprite::setType(string newtype){ _type = newtype; } /** set default dimensions */ void xSprite::dimensions(){ _mutex.try_lock(); /* extract surface lengths*/ int w = _surface->w; int h = _surface->h; _dst = (SDL_Rect){0, 0, w, h}; _src = (SDL_Rect){0, 0, w, h}; _mutex.unlock(); } /** set surface dimensions */ void xSprite::dimensions(SDL_Rect r){ _mutex.try_lock(); /* extract surface lengths */ int w = _surface->w; int h = _surface->h; _src = (SDL_Rect){0, 0, w, h}; /* store destination dimensions */ _dst = (SDL_Rect){r.x, r.y, r.w, r.h}; _mutex.unlock(); } /** set surface dimensions + clip */ void xSprite::dimensions(SDL_Rect r, SDL_Rect clip){ _mutex.try_lock(); _src = (SDL_Rect){clip.x, clip.y, clip.w, clip.h}; _dst = (SDL_Rect){r.x, r.y, r.w, r.h}; _mutex.unlock(); } /** returns the surface */ SDL_Surface *xSprite::surface(){ return _surface; } /** return destination dimensions */ SDL_Rect *xSprite::dst(){ return &_dst; } /** return source dimensions */ SDL_Rect *xSprite::src(){ return &_src; } /** draws to renderer */ void xSprite::draw(SDL_Renderer* renderer){ _mutex.try_lock(); SDL_RenderCopy( renderer, SDL_CreateTextureFromSurface(renderer, this->surface()), this->src(), this->dst() ); _mutex.unlock(); }