SDLPP: add texture flipping/rotation

This commit is contained in:
zvon 2021-04-29 13:06:37 +02:00
parent b611e2479a
commit ff741dd882
3 changed files with 52 additions and 5 deletions

View File

@ -1,4 +1,5 @@
#include "sdlpp_renderobject.hpp"
#include <SDL2/SDL_render.h>
#include <cmath>
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 > &center ) {
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();

View File

@ -8,6 +8,7 @@
#include "sdlpp_vector.hpp"
#include "sdlpp_visitor.hpp"
#include <SDL2/SDL_render.h>
#include <memory>
#include <vector>
@ -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 > &center );
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;

View File

@ -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() )