diff --git a/sdlpp/sdlpp_circlerenderer.cpp b/sdlpp/sdlpp_circlerenderer.cpp index c2ea5e8..03fe84b 100644 --- a/sdlpp/sdlpp_circlerenderer.cpp +++ b/sdlpp/sdlpp_circlerenderer.cpp @@ -124,11 +124,6 @@ SDL_Rect CircleRender::getRect() { return rect; } -void CircleRender::centerX() { - centerx = true; - updateSizeAndPosition(); -} - std::shared_ptr< RenderObject > CircleRender::copySelf() { auto ret = std::make_shared< CircleRender >( *this ); copyTo( ret ); @@ -142,23 +137,4 @@ void CircleRender::copyTo( std::shared_ptr< RenderObject > other ) { std::string CircleRender::getColor() const { 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 diff --git a/sdlpp/sdlpp_circlerenderer.hpp b/sdlpp/sdlpp_circlerenderer.hpp index 17d7985..2779d67 100644 --- a/sdlpp/sdlpp_circlerenderer.hpp +++ b/sdlpp/sdlpp_circlerenderer.hpp @@ -44,13 +44,11 @@ public: virtual int collisionHeight() override; virtual void updateSizeAndPosition() override; virtual SDL_Rect getRect() override; - virtual void centerX() override; virtual std::shared_ptr< RenderObject > copySelf() override; std::string getColor() const; private: virtual void copyTo( std::shared_ptr< RenderObject > other ) override; - void updateXY(); double og_r; double r_; std::string color = ""; diff --git a/sdlpp/sdlpp_linerenderer.cpp b/sdlpp/sdlpp_linerenderer.cpp index 3f1a3ae..831cbf3 100644 --- a/sdlpp/sdlpp_linerenderer.cpp +++ b/sdlpp/sdlpp_linerenderer.cpp @@ -97,10 +97,6 @@ void LineRenderer::updateSizeAndPosition() { collisionWidth(), collisionHeight(), getId() ); } } -void LineRenderer::centerX() { - centerx = true; - updateSizeAndPosition(); -} std::shared_ptr< RenderObject > LineRenderer::copySelf() { auto ret = std::make_shared< LineRenderer >( *this ); copyTo( ret ); @@ -120,26 +116,8 @@ LineRenderer::getDoubleRect() const { } void LineRenderer::updateXY() { - if ( !centerx ) { - current = original; - return; - } - 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_ } }; + auto additions = computeAlignmentAdditions(); + current = { original.getStart() + additions, + original.getEnd() + additions }; } } // namespace SDLPP diff --git a/sdlpp/sdlpp_linerenderer.hpp b/sdlpp/sdlpp_linerenderer.hpp index be5dd1f..32f0f28 100644 --- a/sdlpp/sdlpp_linerenderer.hpp +++ b/sdlpp/sdlpp_linerenderer.hpp @@ -38,7 +38,6 @@ public: virtual int collisionWidth() override; virtual int collisionHeight() override; virtual void updateSizeAndPosition() override; - virtual void centerX() override; virtual std::shared_ptr< RenderObject > copySelf() override; virtual SDL_Rect getRect() override; @@ -48,7 +47,7 @@ public: protected: virtual void copyTo( std::shared_ptr< RenderObject > other ) override; - void updateXY(); + virtual void updateXY() override; Line< double > original = { { 0, 0 }, { 0, 0 } }; Line< double > current = { { 0, 0 }, { 0, 0 } }; Line< int > pixel_line = { { 0, 0 }, { 0, 0 } }; diff --git a/sdlpp/sdlpp_rectrenderer.cpp b/sdlpp/sdlpp_rectrenderer.cpp index c2ba730..35fffc5 100644 --- a/sdlpp/sdlpp_rectrenderer.cpp +++ b/sdlpp/sdlpp_rectrenderer.cpp @@ -158,10 +158,6 @@ void RectangleRender::updateSizeAndPosition() { SDL_Rect RectangleRender::getRect() { return rect; } -void RectangleRender::centerX() { - centerx = true; - updateSizeAndPosition(); -} std::shared_ptr< RenderObject > RectangleRender::copySelf() { auto ret = std::make_shared< RectangleRender >( *this ); copyTo( ret ); @@ -173,23 +169,4 @@ void RectangleRender::copyTo( std::shared_ptr< RenderObject > other ) { std::string RectangleRender::getColor() const { 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 diff --git a/sdlpp/sdlpp_rectrenderer.hpp b/sdlpp/sdlpp_rectrenderer.hpp index d4a59aa..623bd5a 100644 --- a/sdlpp/sdlpp_rectrenderer.hpp +++ b/sdlpp/sdlpp_rectrenderer.hpp @@ -74,13 +74,11 @@ public: virtual int collisionHeight() override; virtual void updateSizeAndPosition() override; virtual SDL_Rect getRect() override; - virtual void centerX() override; virtual std::shared_ptr< RenderObject > copySelf() override; std::string getColor() const; protected: virtual void copyTo( std::shared_ptr< RenderObject > other ) override; - void updateXY(); Vec2D< double > original_size; Vec2D< double > size; std::string color = ""; diff --git a/sdlpp/sdlpp_renderer.cpp b/sdlpp/sdlpp_renderer.cpp index 3f22e03..7158c42 100644 --- a/sdlpp/sdlpp_renderer.cpp +++ b/sdlpp/sdlpp_renderer.cpp @@ -24,10 +24,16 @@ Vec2D< int > Renderer::getDimensions() const { } Vec2D< double > Renderer::getDoubleDimensions() const { auto dimensions = getDimensions(); + if ( dimensions == dimensions_cache ) + return double_dimensions_cache; + dimensions_cache = dimensions; double smaller = dimensions.getX() < dimensions.getY() ? dimensions.getX() : dimensions.getY(); - return { static_cast< double >( dimensions.getX() ) / smaller, - static_cast< double >( dimensions.getY() ) / smaller }; + double_dimensions_cache = { + static_cast< double >( dimensions.getX() ) / smaller, + static_cast< double >( dimensions.getY() ) / smaller + }; + return double_dimensions_cache; } int Renderer::getWidth() const { return getDimensions().getX(); diff --git a/sdlpp/sdlpp_renderer.hpp b/sdlpp/sdlpp_renderer.hpp index 4c61541..ff6a272 100644 --- a/sdlpp/sdlpp_renderer.hpp +++ b/sdlpp/sdlpp_renderer.hpp @@ -29,6 +29,8 @@ public: private: SDL_Renderer *renderer = NULL; 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 #endif diff --git a/sdlpp/sdlpp_renderobject.cpp b/sdlpp/sdlpp_renderobject.cpp index 1e43945..e5fb37c 100644 --- a/sdlpp/sdlpp_renderobject.cpp +++ b/sdlpp/sdlpp_renderobject.cpp @@ -242,4 +242,39 @@ void RenderObject::setAnimationSpeed( const double fps ) { void RenderObject::visit( Visitor &visitor ) { 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 diff --git a/sdlpp/sdlpp_renderobject.hpp b/sdlpp/sdlpp_renderobject.hpp index cb8b77a..852c906 100644 --- a/sdlpp/sdlpp_renderobject.hpp +++ b/sdlpp/sdlpp_renderobject.hpp @@ -12,7 +12,14 @@ #include namespace SDLPP { -class SDLPPSCOPE Scene; + +enum ObjectAlignment { + OBJ_START, + OBJ_CENTER, + OBJ_END, +}; + +class Scene; class SDLPPSCOPE RenderObject { public: @@ -82,10 +89,10 @@ public: virtual void move( int ticks ); virtual void custom_move( int ticks ) = 0; virtual void updateSizeAndPosition() = 0; + virtual void updateXY(); virtual SDL_Rect getRect() = 0; void setPermanent( bool perm = true ); bool getPermanent() const; - virtual void centerX() = 0; virtual std::shared_ptr< RenderObject > copySelf() = 0; bool isStatic(); void setStatic( bool stat = true ); @@ -103,6 +110,7 @@ public: void removeAnimation(); void animate( int ticks ); void visit( Visitor &visitor ); + void setAlignment( ObjectAlignment horizontal, ObjectAlignment vertical ); protected: virtual void copyTo( std::shared_ptr< RenderObject > other ); @@ -115,6 +123,7 @@ protected: double movementSpeed = 0; Vec2D< double > movementDirection = { 0, 0 }; std::vector< std::shared_ptr< RenderObject > > colidedWith; + virtual Vec2D< double > computeAlignmentAdditions(); uint64_t id = -1; bool hidden = false; bool kill = false; @@ -133,12 +142,14 @@ protected: bool animating = true; private: - void setSceneID( int id ); friend Scene; + void setSceneID( int id ); protected: Vec2D< double > original; Vec2D< double > current; + ObjectAlignment _horizontal = OBJ_START; + ObjectAlignment _vertical = OBJ_START; }; } // end of namespace SDLPP diff --git a/sdlpp/sdlpp_vector.hpp b/sdlpp/sdlpp_vector.hpp index e567e8a..07d3559 100644 --- a/sdlpp/sdlpp_vector.hpp +++ b/sdlpp/sdlpp_vector.hpp @@ -38,6 +38,9 @@ public: *this = *this - other; return *this; } + bool operator==( const Vec2D &other ) { + return other._x == _x && other._y == _y; + } private: T _x = 0.0;