From 9617499ab863933dcf9c6e6cded05785528b6b34 Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 8 Apr 2023 20:19:22 +0200 Subject: [PATCH] SDLPP: add constant rotation --- mario/blocks.cpp | 3 ++- sdlpp/sdlpp_circlerenderer.hpp | 4 +++- sdlpp/sdlpp_linerenderer.hpp | 4 +++- sdlpp/sdlpp_rectrenderer.hpp | 4 +++- sdlpp/sdlpp_renderobject.cpp | 16 ++++++++++++++++ sdlpp/sdlpp_renderobject.hpp | 8 ++++++++ 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/mario/blocks.cpp b/mario/blocks.cpp index 3b7b3c9..5fa182f 100644 --- a/mario/blocks.cpp +++ b/mario/blocks.cpp @@ -149,6 +149,7 @@ bool MarioBlock::isTraveling() const { } void MarioBlock::custom_move(int ticks) { + SDLPP::RectangleRender::custom_move(ticks); if (!_bouncing && !_traveling) { return; } @@ -699,4 +700,4 @@ void MarioBlock::harden() { const std::string &MarioBlock::getTeleportLevel() { return _teleport_level; -} \ No newline at end of file +} diff --git a/sdlpp/sdlpp_circlerenderer.hpp b/sdlpp/sdlpp_circlerenderer.hpp index 2779d67..b9c1489 100644 --- a/sdlpp/sdlpp_circlerenderer.hpp +++ b/sdlpp/sdlpp_circlerenderer.hpp @@ -31,7 +31,9 @@ public: virtual void setColor( const std::string &color ) override; virtual void setOutlineColor( const std::string &color ) override; virtual void specialAction( int /*UNUSED*/ ) override {} - virtual void custom_move( int /*UNUSED*/ ) override {} + virtual void custom_move( int ticks ) override { + RenderObject::custom_move(ticks); + } virtual std::pair< Vec2D< double >, Vec2D< double > > getDoubleRect() const override; virtual int leftmost() override; diff --git a/sdlpp/sdlpp_linerenderer.hpp b/sdlpp/sdlpp_linerenderer.hpp index 32f0f28..dd90626 100644 --- a/sdlpp/sdlpp_linerenderer.hpp +++ b/sdlpp/sdlpp_linerenderer.hpp @@ -24,7 +24,9 @@ public: virtual void setColor( const std::string &color ) override; virtual void specialAction( int /*UNUSED*/ ) override{}; virtual void render() override; - virtual void custom_move( int /*UNUSED*/ ) override {} + virtual void custom_move( int ticks ) override { + RenderObject::custom_move(ticks); + } virtual void setPos( double x, double y ) override; virtual void setPos( const std::pair< double, double > &pos ) override; virtual void setPos( const Vec2D< double > &vec ) override; diff --git a/sdlpp/sdlpp_rectrenderer.hpp b/sdlpp/sdlpp_rectrenderer.hpp index 0a882a5..a38260a 100644 --- a/sdlpp/sdlpp_rectrenderer.hpp +++ b/sdlpp/sdlpp_rectrenderer.hpp @@ -61,7 +61,9 @@ public: virtual void setColor( const std::string &color ) override; virtual void setOutlineColor( const std::string &color ) override; virtual void specialAction( int /*UNUSED*/ ) override {} - virtual void custom_move( int /*UNUSED*/ ) override {} + virtual void custom_move( int ticks ) override { + RenderObject::custom_move(ticks); + } virtual std::pair< Vec2D< double >, Vec2D< double > > getDoubleRect() const override; virtual int leftmost() override; diff --git a/sdlpp/sdlpp_renderobject.cpp b/sdlpp/sdlpp_renderobject.cpp index 28c27bb..6989de7 100644 --- a/sdlpp/sdlpp_renderobject.cpp +++ b/sdlpp/sdlpp_renderobject.cpp @@ -291,6 +291,16 @@ void RenderObject::rotateClockwise( int angle ) { void RenderObject::rotateCounterClockwise( int angle ) { rotateClockwise( 360 - ( angle % 360 ) ); } +void RenderObject::setRotationSpeed(int speed) { + rotation_speed = speed; +} +void RenderObject::startRotation() { + rotating = true; +} +void RenderObject::stopRotation() { + rotating = false; +} + Vec2D< double > RenderObject::computeAlignmentAdditions() { double x_addition = 0; double y_addition = 0; @@ -322,4 +332,10 @@ void RenderObject::updateXY() { current = { original.getX() + additions.getX(), original.getY() + additions.getY() }; } +void RenderObject::custom_move(int ticks) { + if(isRotating()) { + auto angle = ticks * rotation_speed / 1000; // tick = millisecond + rotateCounterClockwise(angle); + } +} } // namespace SDLPP diff --git a/sdlpp/sdlpp_renderobject.hpp b/sdlpp/sdlpp_renderobject.hpp index 8fa9aac..eb484a9 100644 --- a/sdlpp/sdlpp_renderobject.hpp +++ b/sdlpp/sdlpp_renderobject.hpp @@ -131,10 +131,16 @@ public: void setRotationCenter( const Vec2D< double > ¢er ); void rotateClockwise( int angle ); void rotateCounterClockwise( int angle ); + void setRotationSpeed(int speed); + void startRotation(); + void stopRotation(); protected: virtual void copyTo( std::shared_ptr< RenderObject > other ); bool entireTexture(); + bool isRotating() { + return rotating; + } std::vector< std::shared_ptr< CollisionPolygon > > collisions; std::shared_ptr< Texture > texture; std::shared_ptr< Texture > cur_texture; @@ -164,6 +170,8 @@ protected: Vec2D< double > rotation_center = { -1, -1 }; SDL_Point rotation_center_point; int rotation_angle = 0; + int rotation_speed = 720; + bool rotating = false; private: friend Scene;