Added source rectangle option for texture setting

This commit is contained in:
zv0n 2021-03-07 13:06:08 +01:00
parent fa10620901
commit fbc1fdd6f7
4 changed files with 82 additions and 14 deletions

View File

@ -13,9 +13,18 @@ RectangleRender::RectangleRender( double x, double y, double w, double h,
}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t )
const std::shared_ptr< Texture > &t,
int source_x, int source_y, int source_width,
int source_height )
: RectangleRender( x, y, w, h, r ) {
setTexture( t );
setTexture( t, source_x, source_y, source_width, source_height );
}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t,
SDL_Rect source_rect )
: RectangleRender( x, y, w, h, r ) {
setTexture( t, source_rect );
}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
@ -29,6 +38,20 @@ RectangleRender::RectangleRender( double x, double y, double w, double h,
color = img_or_color;
}
}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img, int source_x,
int source_y, int source_width,
int source_height )
: RectangleRender( x, y, w, h, r ) {
setTexture( img, source_x, source_y, source_width, source_height );
}
RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img, SDL_Rect source_rect )
: RectangleRender( x, y, w, h, r ) {
setTexture( img, source_rect );
}
void RectangleRender::setColor( const std::string &color ) {
if ( !polygon ) {
polygon = std::make_shared< RectColider >( 0, 0, 1, 1 );
@ -97,11 +120,11 @@ void RectangleRender::centerX() {
}
std::shared_ptr< RenderObject > RectangleRender::copySelf() {
auto ret = std::make_shared< RectangleRender >( *this );
copyTo(ret);
copyTo( ret );
return ret;
}
void RectangleRender::copyTo(std::shared_ptr<RenderObject> other) {
RenderObject::copyTo(other);
void RectangleRender::copyTo( std::shared_ptr< RenderObject > other ) {
RenderObject::copyTo( other );
}
std::string RectangleRender::getColor() const {
return color;

View File

@ -15,13 +15,25 @@ public:
const std::shared_ptr< Renderer > &r );
RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t );
const std::shared_ptr< Texture > &t, int source_x,
int source_y, int source_width, int source_height );
RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t,
SDL_Rect source_rect = { -1, -1, -1, -1 } );
RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img_or_color, bool is_polygon = false );
RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img, int source_x,
int source_y, int source_width, int source_height );
RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r,
const std::string &img, SDL_Rect source_rect );
virtual void setColor( const std::string &color ) override;
virtual void setOutlineColor( const std::string &color ) override;
virtual void specialAction( int /*UNUSED*/ ) override{}
virtual void specialAction( int /*UNUSED*/ ) override {}
virtual void custom_move( int /*UNUSED*/ ) override {}
virtual std::pair< std::pair< double, double >,
std::pair< double, double > >
@ -41,7 +53,7 @@ public:
std::string getColor() const;
protected:
virtual void copyTo(std::shared_ptr<RenderObject> other) override;
virtual void copyTo( std::shared_ptr< RenderObject > other ) override;
void updateXY();
double og_w;
double og_h;

View File

@ -2,13 +2,21 @@
#include <cmath>
namespace SDLPP {
bool RenderObject::entireTexture() {
return src_rect.x == -1 || src_rect.y == -1 || src_rect.w == -1 ||
src_rect.h == -1;
}
void RenderObject::render() {
if ( !getHidden() ) {
if ( polygon )
polygon->render( *renderer );
if ( texture != NULL )
if ( texture != NULL ) {
SDL_Rect *src = NULL;
if(!entireTexture())
src = &src_rect;
SDL_RenderCopy( renderer->getRendererPtr(),
texture->getTexturePtr(), NULL, &rect );
texture->getTexturePtr(), src, &rect );
}
if ( hasCollisions() && renderer->getRenderColiders() ) {
for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color );
@ -46,11 +54,25 @@ const std::vector< std::shared_ptr< CollisionPolygon > > &
RenderObject::getCollisions() const {
return collisions;
}
void RenderObject::setTexture( const std::shared_ptr< Texture > &t ) {
void RenderObject::setTexture( const std::shared_ptr< Texture > &t,
SDL_Rect source_rect ) {
texture = t;
src_rect = source_rect;
}
void RenderObject::setTexture( const std::string &img_path ) {
void RenderObject::setTexture( const std::shared_ptr< Texture > &t,
int source_x, int source_y, int source_width,
int source_height ) {
setTexture( t, { source_x, source_y, source_width, source_height } );
}
void RenderObject::setTexture( const std::string &img_path,
SDL_Rect source_rect ) {
texture = std::make_shared< Texture >( renderer, img_path );
src_rect = source_rect;
}
void RenderObject::setTexture( const std::string &img_path, int source_x,
int source_y, int source_width,
int source_height ) {
setTexture( img_path, { source_x, source_y, source_width, source_height } );
}
void RenderObject::setTexture( Font &font, const std::string &text,
const std::string &color,
@ -58,6 +80,7 @@ void RenderObject::setTexture( Font &font, const std::string &text,
int outline_size ) {
texture = std::make_shared< Texture >( renderer, font, text, color,
outline_color, outline_size );
src_rect = { -1, -1, -1, -1 };
}
void RenderObject::unsetTexture() {
texture.reset();

View File

@ -42,8 +42,16 @@ public:
bool hasCollisions() const;
const std::vector< std::shared_ptr< CollisionPolygon > > &
getCollisions() const;
virtual void setTexture( const std::shared_ptr< Texture > &t );
virtual void setTexture( const std::string &img_path );
virtual void setTexture( const std::shared_ptr< Texture > &t, int source_x,
int source_y, int source_width,
int source_height );
virtual void setTexture( const std::shared_ptr< Texture > &t,
SDL_Rect source_rect = { -1, -1, -1, -1 } );
virtual void setTexture( const std::string &img_path, int source_x,
int source_y, int source_width,
int source_height );
virtual void setTexture( const std::string &img_path,
SDL_Rect source_rect = { -1, -1, -1, -1 } );
virtual void setTexture( Font &font, const std::string &text,
const std::string &color = "FFFFFF",
const std::string &outline_color = "000000",
@ -81,6 +89,7 @@ public:
protected:
virtual void copyTo( std::shared_ptr< RenderObject > other );
bool entireTexture();
std::vector< std::shared_ptr< CollisionPolygon > > collisions;
std::shared_ptr< Texture > texture;
std::shared_ptr< Texture > cur_texture;
@ -98,6 +107,7 @@ protected:
bool is_static = true;
bool centerx = false;
SDL_Rect rect;
SDL_Rect src_rect = { -1, -1, -1, -1 };
private:
void setSceneID( int id );