From 49deb9e868c450d5776dba6812a76a6d1dedafa1 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 4 Feb 2020 18:56:10 +0100 Subject: [PATCH] add xSprite constructors and SDL_Rect primitive shorthands --- xSDL/xSprite.cpp | 38 ++++++++++++++++++++++++++++++++++++++ xSDL/xSprite.h | 7 +++++++ 2 files changed, 45 insertions(+) diff --git a/xSDL/xSprite.cpp b/xSDL/xSprite.cpp index 0b76c0e..7536805 100644 --- a/xSDL/xSprite.cpp +++ b/xSDL/xSprite.cpp @@ -21,6 +21,12 @@ xSprite::xSprite(const char *url, SDL_Rect clip){ this->setSurface(url); this->setClip(clip); } +/** 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); +} /** update sprite to rhb color */ void xSprite::setSurface(const int rgba[]){ @@ -72,6 +78,12 @@ void xSprite::setSurface(SDL_Surface *s){ _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){ @@ -79,6 +91,19 @@ void xSprite::setClip(SDL_Rect clip){ _clip = clip; _mutex.unlock(); } +/** set sprite clip */ +void xSprite::setClip(int x, int y, int w, int h){ + _mutex.lock(); + _clip = (SDL_Rect){x, y, w, h}; + _mutex.unlock(); +} +/** 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){ @@ -86,6 +111,19 @@ void xSprite::project(SDL_Rect dest){ _projection = dest; _mutex.unlock(); } +/** set sprite projection */ +void xSprite::project(int x, int y, int w, int h){ + _mutex.lock(); + _projection = (SDL_Rect){x, y, w, h}; + _mutex.unlock(); +} +/** 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){ diff --git a/xSDL/xSprite.h b/xSDL/xSprite.h index 5189dd1..d9fd977 100644 --- a/xSDL/xSprite.h +++ b/xSDL/xSprite.h @@ -19,18 +19,25 @@ xSprite(const char *url); // image with default clip xSprite(const char *url, SDL_Rect clip); + // image with default clip and projection + xSprite(const char *url, SDL_Rect clip, SDL_Rect projection); virtual ~xSprite(); // replace surface void setSurface(const int rgba[]); // color sprite void setSurface(const char *url); // image sprite void setSurface(SDL_Surface *t); // copy surface + SDL_Surface* surface(); // sets the sprite clip void setClip(SDL_Rect clip); + void setClip(int x, int y, int w, int h); + SDL_Rect clip(); // set projection into scene void project(SDL_Rect dest); + void project(int x, int y, int w, int h); + SDL_Rect projection(); // implement xDrawable virtual void draw(SDL_Renderer* renderer) override;