SDLPP: add texture flipping/rotation
This commit is contained in:
parent
b611e2479a
commit
ff741dd882
@ -1,4 +1,5 @@
|
|||||||
#include "sdlpp_renderobject.hpp"
|
#include "sdlpp_renderobject.hpp"
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
namespace SDLPP {
|
namespace SDLPP {
|
||||||
@ -19,8 +20,13 @@ void RenderObject::render() {
|
|||||||
} else {
|
} else {
|
||||||
src = &animation[animation_index];
|
src = &animation[animation_index];
|
||||||
}
|
}
|
||||||
SDL_RenderCopy( renderer->getRendererPtr(),
|
SDL_Point *rotation_point = NULL;
|
||||||
texture->getTexturePtr(), src, &rect );
|
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() ) {
|
if ( hasCollisions() && renderer->getRenderColiders() ) {
|
||||||
for ( const auto &col : getCollisions() )
|
for ( const auto &col : getCollisions() )
|
||||||
@ -247,6 +253,31 @@ void RenderObject::setAlignment( ObjectAlignment horizontal,
|
|||||||
_horizontal = horizontal;
|
_horizontal = horizontal;
|
||||||
_vertical = vertical;
|
_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() {
|
Vec2D< double > RenderObject::computeAlignmentAdditions() {
|
||||||
double x_addition = 0, y_addition = 0;
|
double x_addition = 0, y_addition = 0;
|
||||||
auto dimensions = renderer->getDoubleDimensions();
|
auto dimensions = renderer->getDoubleDimensions();
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include "sdlpp_vector.hpp"
|
#include "sdlpp_vector.hpp"
|
||||||
#include "sdlpp_visitor.hpp"
|
#include "sdlpp_visitor.hpp"
|
||||||
|
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -111,6 +112,11 @@ public:
|
|||||||
void animate( int ticks );
|
void animate( int ticks );
|
||||||
void visit( Visitor &visitor );
|
void visit( Visitor &visitor );
|
||||||
void setAlignment( ObjectAlignment horizontal, ObjectAlignment vertical );
|
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:
|
protected:
|
||||||
virtual void copyTo( std::shared_ptr< RenderObject > other );
|
virtual void copyTo( std::shared_ptr< RenderObject > other );
|
||||||
@ -140,6 +146,10 @@ protected:
|
|||||||
int animation_next_frame_base = 0;
|
int animation_next_frame_base = 0;
|
||||||
std::vector< SDL_Rect > animation{};
|
std::vector< SDL_Rect > animation{};
|
||||||
bool animating = true;
|
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:
|
private:
|
||||||
friend Scene;
|
friend Scene;
|
||||||
|
@ -57,9 +57,15 @@ void TextRenderer::render() {
|
|||||||
if ( !getHidden() ) {
|
if ( !getHidden() ) {
|
||||||
if ( polygon )
|
if ( polygon )
|
||||||
polygon->render( *renderer );
|
polygon->render( *renderer );
|
||||||
if ( texture != NULL )
|
if ( texture != NULL ) {
|
||||||
SDL_RenderCopy( renderer->getRendererPtr(),
|
SDL_Point *rotation_point = NULL;
|
||||||
texture->getTexturePtr(), NULL, &dst_rect );
|
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() ) {
|
if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) {
|
||||||
for ( const auto &col : getCollisions() )
|
for ( const auto &col : getCollisions() )
|
||||||
|
Loading…
Reference in New Issue
Block a user