diff --git a/sdlpp/sdlpp_renderobject.cpp b/sdlpp/sdlpp_renderobject.cpp index e5fb37c..a0978d7 100644 --- a/sdlpp/sdlpp_renderobject.cpp +++ b/sdlpp/sdlpp_renderobject.cpp @@ -1,4 +1,5 @@ #include "sdlpp_renderobject.hpp" +#include #include namespace SDLPP { @@ -19,8 +20,13 @@ void RenderObject::render() { } else { src = &animation[animation_index]; } - SDL_RenderCopy( renderer->getRendererPtr(), - texture->getTexturePtr(), src, &rect ); + SDL_Point *rotation_point = NULL; + if ( rotation_center.getX() != -1 ) { + rotation_point = &rotation_center_point; + } + SDL_RenderCopyEx( renderer->getRendererPtr(), + texture->getTexturePtr(), src, &rect, + rotation_angle, rotation_point, flip ); } if ( hasCollisions() && renderer->getRenderColiders() ) { for ( const auto &col : getCollisions() ) @@ -247,6 +253,31 @@ void RenderObject::setAlignment( ObjectAlignment horizontal, _horizontal = horizontal; _vertical = vertical; } +void RenderObject::flipHorizontally() { + if ( flip & SDL_FLIP_HORIZONTAL ) { + flip = static_cast< SDL_RendererFlip >( flip & ~SDL_FLIP_HORIZONTAL ); + } else { + flip = static_cast< SDL_RendererFlip >( flip | SDL_FLIP_HORIZONTAL ); + } +} +void RenderObject::flipVertically() { + if ( flip & SDL_FLIP_VERTICAL ) { + flip = static_cast< SDL_RendererFlip >( flip & ~SDL_FLIP_VERTICAL ); + } else { + flip = static_cast< SDL_RendererFlip >( flip | SDL_FLIP_VERTICAL ); + } +} +void RenderObject::setRotationCenter( const Vec2D< double > ¢er ) { + rotation_center = center; + rotation_center_point = { static_cast< int >( center.getX() * rect.w ), + static_cast< int >( center.getY() * rect.h ) }; +} +void RenderObject::rotateClockwise( int angle ) { + rotation_angle = ( rotation_angle + angle ) % 360; +} +void RenderObject::rotateCounterClockwise( int angle ) { + rotateClockwise( 360 - ( angle % 360 ) ); +} Vec2D< double > RenderObject::computeAlignmentAdditions() { double x_addition = 0, y_addition = 0; auto dimensions = renderer->getDoubleDimensions(); diff --git a/sdlpp/sdlpp_renderobject.hpp b/sdlpp/sdlpp_renderobject.hpp index 852c906..b168aa7 100644 --- a/sdlpp/sdlpp_renderobject.hpp +++ b/sdlpp/sdlpp_renderobject.hpp @@ -8,6 +8,7 @@ #include "sdlpp_vector.hpp" #include "sdlpp_visitor.hpp" +#include #include #include @@ -111,6 +112,11 @@ public: void animate( int ticks ); void visit( Visitor &visitor ); void setAlignment( ObjectAlignment horizontal, ObjectAlignment vertical ); + void flipHorizontally(); + void flipVertically(); + void setRotationCenter( const Vec2D< double > ¢er ); + void rotateClockwise( int angle ); + void rotateCounterClockwise( int angle ); protected: virtual void copyTo( std::shared_ptr< RenderObject > other ); @@ -140,6 +146,10 @@ protected: int animation_next_frame_base = 0; std::vector< SDL_Rect > animation{}; bool animating = true; + SDL_RendererFlip flip = SDL_FLIP_NONE; + Vec2D< double > rotation_center = { -1, -1 }; + SDL_Point rotation_center_point; + int rotation_angle = 0; private: friend Scene; diff --git a/sdlpp/sdlpp_textrenderer.cpp b/sdlpp/sdlpp_textrenderer.cpp index ab97db4..bdca275 100644 --- a/sdlpp/sdlpp_textrenderer.cpp +++ b/sdlpp/sdlpp_textrenderer.cpp @@ -57,9 +57,15 @@ void TextRenderer::render() { if ( !getHidden() ) { if ( polygon ) polygon->render( *renderer ); - if ( texture != NULL ) - SDL_RenderCopy( renderer->getRendererPtr(), - texture->getTexturePtr(), NULL, &dst_rect ); + if ( texture != NULL ) { + SDL_Point *rotation_point = NULL; + if ( rotation_center.getX() != -1 ) { + rotation_point = &rotation_center_point; + } + SDL_RenderCopyEx( renderer->getRendererPtr(), + texture->getTexturePtr(), NULL, &dst_rect, + rotation_angle, rotation_point, flip ); + } } if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) { for ( const auto &col : getCollisions() )