SDLPP: add texture flipping/rotation

This commit is contained in:
2021-04-29 13:06:37 +02:00
parent b611e2479a
commit ff741dd882
3 changed files with 52 additions and 5 deletions
+33 -2
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();