145 lines
3.0 KiB
C++
145 lines
3.0 KiB
C++
#include "xSprite.h"
|
|
|
|
/** clean SDL objects */
|
|
xSprite::~xSprite(){
|
|
_mutex.lock();
|
|
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);
|
|
}
|
|
|
|
/** 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[]){
|
|
_mutex.lock();
|
|
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){
|
|
_mutex.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();
|
|
}
|
|
|
|
/** copies an existing surface */
|
|
void xSprite::setSurface(SDL_Surface *s){
|
|
_mutex.lock();
|
|
if( _surface != NULL ){
|
|
SDL_FreeSurface( _surface );
|
|
_surface = NULL;
|
|
}
|
|
|
|
_surface = s;
|
|
|
|
if( _surface == NULL ) {
|
|
throw runtime_error("[xSprite] setSurface(surface) -> NULL surface");
|
|
}
|
|
|
|
_mutex.unlock();
|
|
}
|
|
SDL_Surface* xSprite::surface(){
|
|
_mutex.lock();
|
|
SDL_Surface* surface = _surface;
|
|
_mutex.unlock();
|
|
return surface;
|
|
}
|
|
|
|
/** set sprite clip */
|
|
void xSprite::setClip(SDL_Rect clip){
|
|
_mutex.lock();
|
|
_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 */
|
|
SDL_Rect xSprite::clip(){
|
|
_mutex.lock();
|
|
const SDL_Rect clip = _clip;
|
|
_mutex.unlock();
|
|
return clip;
|
|
}
|
|
|
|
/** set sprite projection */
|
|
void xSprite::project(SDL_Rect dest){
|
|
_mutex.lock();
|
|
_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 */
|
|
SDL_Rect xSprite::projection(){
|
|
_mutex.lock();
|
|
const SDL_Rect projection = _projection;
|
|
_mutex.unlock();
|
|
return projection;
|
|
}
|
|
|
|
/** draws to renderer */
|
|
void xSprite::draw(SDL_Renderer* renderer){
|
|
_mutex.lock();
|
|
|
|
SDL_RenderCopy(
|
|
renderer,
|
|
SDL_CreateTextureFromSurface(renderer, _surface),
|
|
&_clip,
|
|
&_projection
|
|
);
|
|
_mutex.unlock();
|
|
} |