2019-11-05 20:45:41 +00:00
|
|
|
#include "xSprite.h"
|
|
|
|
|
|
|
|
/** clean SDL objects */
|
|
|
|
xSprite::~xSprite(){
|
2020-02-02 15:18:12 +00:00
|
|
|
_mutex.lock();
|
2019-11-05 20:45:41 +00:00
|
|
|
SDL_FreeSurface( _surface );
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** color sprite */
|
|
|
|
xSprite::xSprite(const int rgba[]){
|
|
|
|
this->setSurface(rgba);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** image sprite */
|
|
|
|
xSprite::xSprite(const char *url){
|
|
|
|
this->setSurface(url);
|
|
|
|
}
|
2020-02-04 18:03:03 +00:00
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
/** image sprite with clip */
|
|
|
|
xSprite::xSprite(const char *url, SDL_Rect clip){
|
|
|
|
this->setSurface(url);
|
|
|
|
this->setClip(clip);
|
2019-11-05 20:45:41 +00:00
|
|
|
}
|
2020-02-04 18:03:03 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-04 17:56:10 +00:00
|
|
|
/** 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);
|
|
|
|
}
|
2020-02-04 18:03:03 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-11-05 20:45:41 +00:00
|
|
|
|
|
|
|
/** update sprite to rhb color */
|
|
|
|
void xSprite::setSurface(const int rgba[]){
|
2020-02-02 15:18:12 +00:00
|
|
|
_mutex.lock();
|
2019-11-05 20:45:41 +00:00
|
|
|
if( _surface != NULL ){
|
|
|
|
SDL_FreeSurface( _surface );
|
|
|
|
_surface = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_surface = SDL_CreateRGBSurface(0, 0, 0, 32, rgba[0], rgba[1], rgba[2], rgba[3]);
|
|
|
|
|
|
|
|
if( _surface == NULL ) {
|
|
|
|
throw runtime_error("[xSprite] setSurface(rgba) -> NULL surface");
|
|
|
|
}
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** update sprite to image */
|
|
|
|
void xSprite::setSurface(const char *url){
|
2020-02-02 15:18:12 +00:00
|
|
|
_mutex.lock();
|
2019-11-05 20:45:41 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
/** copies an existing surface */
|
2019-11-05 20:45:41 +00:00
|
|
|
void xSprite::setSurface(SDL_Surface *s){
|
2020-02-02 15:18:12 +00:00
|
|
|
_mutex.lock();
|
2019-11-05 20:45:41 +00:00
|
|
|
if( _surface != NULL ){
|
|
|
|
SDL_FreeSurface( _surface );
|
|
|
|
_surface = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_surface = s;
|
|
|
|
|
|
|
|
if( _surface == NULL ) {
|
|
|
|
throw runtime_error("[xSprite] setSurface(surface) -> NULL surface");
|
|
|
|
}
|
|
|
|
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
2020-02-04 17:56:10 +00:00
|
|
|
SDL_Surface* xSprite::surface(){
|
|
|
|
_mutex.lock();
|
|
|
|
SDL_Surface* surface = _surface;
|
|
|
|
_mutex.unlock();
|
|
|
|
return surface;
|
|
|
|
}
|
2019-11-05 20:45:41 +00:00
|
|
|
|
2020-02-02 15:18:12 +00:00
|
|
|
/** set sprite clip */
|
2019-11-08 20:28:36 +00:00
|
|
|
void xSprite::setClip(SDL_Rect clip){
|
2020-02-02 15:18:12 +00:00
|
|
|
_mutex.lock();
|
|
|
|
_clip = clip;
|
2019-11-05 20:45:41 +00:00
|
|
|
_mutex.unlock();
|
|
|
|
}
|
2020-02-04 17:56:10 +00:00
|
|
|
void xSprite::setClip(int x, int y, int w, int h){
|
2020-02-04 18:03:03 +00:00
|
|
|
setClip( (SDL_Rect){x, y, w, h} );
|
2020-02-04 17:56:10 +00:00
|
|
|
}
|
2020-02-04 18:03:03 +00:00
|
|
|
|
2020-02-04 17:56:10 +00:00
|
|
|
/** gets sprite clip */
|
|
|
|
SDL_Rect xSprite::clip(){
|
|
|
|
_mutex.lock();
|
|
|
|
const SDL_Rect clip = _clip;
|
|
|
|
_mutex.unlock();
|
|
|
|
return clip;
|
|
|
|
}
|
2019-11-05 20:45:41 +00:00
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
/** set sprite projection */
|
|
|
|
void xSprite::project(SDL_Rect dest){
|
2020-02-02 15:18:12 +00:00
|
|
|
_mutex.lock();
|
2019-11-08 20:28:36 +00:00
|
|
|
_projection = dest;
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
2020-02-04 17:56:10 +00:00
|
|
|
void xSprite::project(int x, int y, int w, int h){
|
2020-02-04 18:03:03 +00:00
|
|
|
project( (SDL_Rect){x, y, w, h} );
|
2020-02-04 17:56:10 +00:00
|
|
|
}
|
|
|
|
/** gets sprite projection */
|
|
|
|
SDL_Rect xSprite::projection(){
|
|
|
|
_mutex.lock();
|
|
|
|
const SDL_Rect projection = _projection;
|
|
|
|
_mutex.unlock();
|
|
|
|
return projection;
|
|
|
|
}
|
2019-11-05 20:45:41 +00:00
|
|
|
|
|
|
|
/** draws to renderer */
|
|
|
|
void xSprite::draw(SDL_Renderer* renderer){
|
2020-02-02 15:18:12 +00:00
|
|
|
_mutex.lock();
|
2019-11-08 20:28:36 +00:00
|
|
|
|
2019-11-05 20:45:41 +00:00
|
|
|
SDL_RenderCopy(
|
|
|
|
renderer,
|
2019-11-08 20:28:36 +00:00
|
|
|
SDL_CreateTextureFromSurface(renderer, _surface),
|
2020-02-02 15:18:12 +00:00
|
|
|
&_clip,
|
2019-11-08 20:28:36 +00:00
|
|
|
&_projection
|
2019-11-05 20:45:41 +00:00
|
|
|
);
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|