SDLPP: add option to specify render object alignment

This commit is contained in:
zvon 2021-04-27 15:54:30 +02:00
parent fc1d06a2b8
commit e5d0610f6d
11 changed files with 66 additions and 83 deletions

View File

@ -124,11 +124,6 @@ SDL_Rect CircleRender::getRect() {
return rect; return rect;
} }
void CircleRender::centerX() {
centerx = true;
updateSizeAndPosition();
}
std::shared_ptr< RenderObject > CircleRender::copySelf() { std::shared_ptr< RenderObject > CircleRender::copySelf() {
auto ret = std::make_shared< CircleRender >( *this ); auto ret = std::make_shared< CircleRender >( *this );
copyTo( ret ); copyTo( ret );
@ -142,23 +137,4 @@ void CircleRender::copyTo( std::shared_ptr< RenderObject > other ) {
std::string CircleRender::getColor() const { std::string CircleRender::getColor() const {
return color; return color;
} }
void CircleRender::updateXY() {
if ( !centerx ) {
current = original;
return;
}
auto width = renderer->getWidth();
auto height = renderer->getHeight();
double x_, y_;
if ( width > height ) {
auto multiplier =
static_cast< double >( width ) / static_cast< double >( height );
x_ = original.getX() + static_cast< double >( multiplier - 1 ) / 2;
} else {
x_ = original.getX();
}
y_ = original.getY();
current = { x_, y_ };
}
} // namespace SDLPP } // namespace SDLPP

View File

@ -44,13 +44,11 @@ public:
virtual int collisionHeight() override; virtual int collisionHeight() override;
virtual void updateSizeAndPosition() override; virtual void updateSizeAndPosition() override;
virtual SDL_Rect getRect() override; virtual SDL_Rect getRect() override;
virtual void centerX() override;
virtual std::shared_ptr< RenderObject > copySelf() override; virtual std::shared_ptr< RenderObject > copySelf() override;
std::string getColor() const; std::string getColor() const;
private: private:
virtual void copyTo( std::shared_ptr< RenderObject > other ) override; virtual void copyTo( std::shared_ptr< RenderObject > other ) override;
void updateXY();
double og_r; double og_r;
double r_; double r_;
std::string color = ""; std::string color = "";

View File

@ -97,10 +97,6 @@ void LineRenderer::updateSizeAndPosition() {
collisionWidth(), collisionHeight(), getId() ); collisionWidth(), collisionHeight(), getId() );
} }
} }
void LineRenderer::centerX() {
centerx = true;
updateSizeAndPosition();
}
std::shared_ptr< RenderObject > LineRenderer::copySelf() { std::shared_ptr< RenderObject > LineRenderer::copySelf() {
auto ret = std::make_shared< LineRenderer >( *this ); auto ret = std::make_shared< LineRenderer >( *this );
copyTo( ret ); copyTo( ret );
@ -120,26 +116,8 @@ LineRenderer::getDoubleRect() const {
} }
void LineRenderer::updateXY() { void LineRenderer::updateXY() {
if ( !centerx ) { auto additions = computeAlignmentAdditions();
current = original; current = { original.getStart() + additions,
return; original.getEnd() + additions };
}
auto width = renderer->getWidth();
auto height = renderer->getHeight();
double x1_, x2_, y1_, y2_;
if ( width > height ) {
auto multiplier =
static_cast< double >( width ) / static_cast< double >( height );
x1_ = original.getStart().getX() +
static_cast< double >( multiplier - 1 ) / 2;
x2_ = original.getEnd().getX() +
static_cast< double >( multiplier - 1 ) / 2;
} else {
x1_ = original.getStart().getX();
x2_ = original.getEnd().getX();
}
y1_ = original.getStart().getY();
y2_ = original.getEnd().getY();
current = { { x1_, y1_ }, { x2_, y2_ } };
} }
} // namespace SDLPP } // namespace SDLPP

View File

@ -38,7 +38,6 @@ public:
virtual int collisionWidth() override; virtual int collisionWidth() override;
virtual int collisionHeight() override; virtual int collisionHeight() override;
virtual void updateSizeAndPosition() override; virtual void updateSizeAndPosition() override;
virtual void centerX() override;
virtual std::shared_ptr< RenderObject > copySelf() override; virtual std::shared_ptr< RenderObject > copySelf() override;
virtual SDL_Rect getRect() override; virtual SDL_Rect getRect() override;
@ -48,7 +47,7 @@ public:
protected: protected:
virtual void copyTo( std::shared_ptr< RenderObject > other ) override; virtual void copyTo( std::shared_ptr< RenderObject > other ) override;
void updateXY(); virtual void updateXY() override;
Line< double > original = { { 0, 0 }, { 0, 0 } }; Line< double > original = { { 0, 0 }, { 0, 0 } };
Line< double > current = { { 0, 0 }, { 0, 0 } }; Line< double > current = { { 0, 0 }, { 0, 0 } };
Line< int > pixel_line = { { 0, 0 }, { 0, 0 } }; Line< int > pixel_line = { { 0, 0 }, { 0, 0 } };

View File

@ -158,10 +158,6 @@ void RectangleRender::updateSizeAndPosition() {
SDL_Rect RectangleRender::getRect() { SDL_Rect RectangleRender::getRect() {
return rect; return rect;
} }
void RectangleRender::centerX() {
centerx = true;
updateSizeAndPosition();
}
std::shared_ptr< RenderObject > RectangleRender::copySelf() { std::shared_ptr< RenderObject > RectangleRender::copySelf() {
auto ret = std::make_shared< RectangleRender >( *this ); auto ret = std::make_shared< RectangleRender >( *this );
copyTo( ret ); copyTo( ret );
@ -173,23 +169,4 @@ void RectangleRender::copyTo( std::shared_ptr< RenderObject > other ) {
std::string RectangleRender::getColor() const { std::string RectangleRender::getColor() const {
return color; return color;
} }
void RectangleRender::updateXY() {
if ( !centerx ) {
current = original;
return;
}
auto width = renderer->getWidth();
auto height = renderer->getHeight();
double x_, y_;
if ( width > height ) {
auto multiplier =
static_cast< double >( width ) / static_cast< double >( height );
x_ = original.getX() + static_cast< double >( multiplier - 1 ) / 2;
} else {
x_ = original.getX();
}
y_ = original.getY();
current = { x_, y_ };
}
} // namespace SDLPP } // namespace SDLPP

View File

@ -74,13 +74,11 @@ public:
virtual int collisionHeight() override; virtual int collisionHeight() override;
virtual void updateSizeAndPosition() override; virtual void updateSizeAndPosition() override;
virtual SDL_Rect getRect() override; virtual SDL_Rect getRect() override;
virtual void centerX() override;
virtual std::shared_ptr< RenderObject > copySelf() override; virtual std::shared_ptr< RenderObject > copySelf() override;
std::string getColor() const; std::string getColor() const;
protected: protected:
virtual void copyTo( std::shared_ptr< RenderObject > other ) override; virtual void copyTo( std::shared_ptr< RenderObject > other ) override;
void updateXY();
Vec2D< double > original_size; Vec2D< double > original_size;
Vec2D< double > size; Vec2D< double > size;
std::string color = ""; std::string color = "";

View File

@ -24,10 +24,16 @@ Vec2D< int > Renderer::getDimensions() const {
} }
Vec2D< double > Renderer::getDoubleDimensions() const { Vec2D< double > Renderer::getDoubleDimensions() const {
auto dimensions = getDimensions(); auto dimensions = getDimensions();
if ( dimensions == dimensions_cache )
return double_dimensions_cache;
dimensions_cache = dimensions;
double smaller = dimensions.getX() < dimensions.getY() ? dimensions.getX() double smaller = dimensions.getX() < dimensions.getY() ? dimensions.getX()
: dimensions.getY(); : dimensions.getY();
return { static_cast< double >( dimensions.getX() ) / smaller, double_dimensions_cache = {
static_cast< double >( dimensions.getY() ) / smaller }; static_cast< double >( dimensions.getX() ) / smaller,
static_cast< double >( dimensions.getY() ) / smaller
};
return double_dimensions_cache;
} }
int Renderer::getWidth() const { int Renderer::getWidth() const {
return getDimensions().getX(); return getDimensions().getX();

View File

@ -29,6 +29,8 @@ public:
private: private:
SDL_Renderer *renderer = NULL; SDL_Renderer *renderer = NULL;
bool render_coliders = false; bool render_coliders = false;
mutable Vec2D< int > dimensions_cache = { 0, 0 };
mutable Vec2D< double > double_dimensions_cache = { 0.0, 0.0 };
}; };
} // end of namespace SDLPP } // end of namespace SDLPP
#endif #endif

View File

@ -242,4 +242,39 @@ void RenderObject::setAnimationSpeed( const double fps ) {
void RenderObject::visit( Visitor &visitor ) { void RenderObject::visit( Visitor &visitor ) {
visitor.visit( *this ); visitor.visit( *this );
} }
void RenderObject::setAlignment( ObjectAlignment horizontal,
ObjectAlignment vertical ) {
_horizontal = horizontal;
_vertical = vertical;
}
Vec2D< double > RenderObject::computeAlignmentAdditions() {
double x_addition = 0, y_addition = 0;
auto dimensions = renderer->getDoubleDimensions();
auto width_diff = dimensions.getX() - 1;
auto height_diff = dimensions.getY() - 1;
switch ( _horizontal ) {
case OBJ_CENTER:
x_addition = width_diff / 2;
break;
case OBJ_END:
x_addition = width_diff;
default:
break;
}
switch ( _vertical ) {
case OBJ_CENTER:
y_addition = height_diff / 2;
break;
case OBJ_END:
y_addition = height_diff;
default:
break;
}
return { x_addition, y_addition };
}
void RenderObject::updateXY() {
auto additions = computeAlignmentAdditions();
current = { original.getX() + additions.getX(),
original.getY() + additions.getY() };
}
} // namespace SDLPP } // namespace SDLPP

View File

@ -12,7 +12,14 @@
#include <vector> #include <vector>
namespace SDLPP { namespace SDLPP {
class SDLPPSCOPE Scene;
enum ObjectAlignment {
OBJ_START,
OBJ_CENTER,
OBJ_END,
};
class Scene;
class SDLPPSCOPE RenderObject { class SDLPPSCOPE RenderObject {
public: public:
@ -82,10 +89,10 @@ public:
virtual void move( int ticks ); virtual void move( int ticks );
virtual void custom_move( int ticks ) = 0; virtual void custom_move( int ticks ) = 0;
virtual void updateSizeAndPosition() = 0; virtual void updateSizeAndPosition() = 0;
virtual void updateXY();
virtual SDL_Rect getRect() = 0; virtual SDL_Rect getRect() = 0;
void setPermanent( bool perm = true ); void setPermanent( bool perm = true );
bool getPermanent() const; bool getPermanent() const;
virtual void centerX() = 0;
virtual std::shared_ptr< RenderObject > copySelf() = 0; virtual std::shared_ptr< RenderObject > copySelf() = 0;
bool isStatic(); bool isStatic();
void setStatic( bool stat = true ); void setStatic( bool stat = true );
@ -103,6 +110,7 @@ public:
void removeAnimation(); void removeAnimation();
void animate( int ticks ); void animate( int ticks );
void visit( Visitor &visitor ); void visit( Visitor &visitor );
void setAlignment( ObjectAlignment horizontal, ObjectAlignment vertical );
protected: protected:
virtual void copyTo( std::shared_ptr< RenderObject > other ); virtual void copyTo( std::shared_ptr< RenderObject > other );
@ -115,6 +123,7 @@ protected:
double movementSpeed = 0; double movementSpeed = 0;
Vec2D< double > movementDirection = { 0, 0 }; Vec2D< double > movementDirection = { 0, 0 };
std::vector< std::shared_ptr< RenderObject > > colidedWith; std::vector< std::shared_ptr< RenderObject > > colidedWith;
virtual Vec2D< double > computeAlignmentAdditions();
uint64_t id = -1; uint64_t id = -1;
bool hidden = false; bool hidden = false;
bool kill = false; bool kill = false;
@ -133,12 +142,14 @@ protected:
bool animating = true; bool animating = true;
private: private:
void setSceneID( int id );
friend Scene; friend Scene;
void setSceneID( int id );
protected: protected:
Vec2D< double > original; Vec2D< double > original;
Vec2D< double > current; Vec2D< double > current;
ObjectAlignment _horizontal = OBJ_START;
ObjectAlignment _vertical = OBJ_START;
}; };
} // end of namespace SDLPP } // end of namespace SDLPP

View File

@ -38,6 +38,9 @@ public:
*this = *this - other; *this = *this - other;
return *this; return *this;
} }
bool operator==( const Vec2D &other ) {
return other._x == _x && other._y == _y;
}
private: private:
T _x = 0.0; T _x = 0.0;