Added source rectangle option for texture setting

This commit is contained in:
2021-03-07 13:06:08 +01:00
parent fa10620901
commit fbc1fdd6f7
4 changed files with 82 additions and 14 deletions
+27 -4
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();