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, RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r, 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 ) { : 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, RectangleRender::RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r, const std::shared_ptr< Renderer > &r,
@ -29,6 +38,20 @@ RectangleRender::RectangleRender( double x, double y, double w, double h,
color = img_or_color; 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 ) { void RectangleRender::setColor( const std::string &color ) {
if ( !polygon ) { if ( !polygon ) {
polygon = std::make_shared< RectColider >( 0, 0, 1, 1 ); polygon = std::make_shared< RectColider >( 0, 0, 1, 1 );

View File

@ -15,10 +15,22 @@ public:
const std::shared_ptr< Renderer > &r ); const std::shared_ptr< Renderer > &r );
RectangleRender( double x, double y, double w, double h, RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r, 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, RectangleRender( double x, double y, double w, double h,
const std::shared_ptr< Renderer > &r, const std::shared_ptr< Renderer > &r,
const std::string &img_or_color, bool is_polygon = false ); 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 setColor( const std::string &color ) override;
virtual void setOutlineColor( 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 {}

View File

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

View File

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