From bb502b37f433a0d71700657ec722d47c734dcdb0 Mon Sep 17 00:00:00 2001 From: zv0n Date: Sun, 7 Mar 2021 14:06:55 +0100 Subject: [PATCH] Animation --- sdlpp/sdlpp_rectrenderer.cpp | 5 ++-- sdlpp/sdlpp_rectrenderer.hpp | 8 +++--- sdlpp/sdlpp_renderobject.cpp | 49 ++++++++++++++++++++++++++++++++---- sdlpp/sdlpp_renderobject.hpp | 20 ++++++++++++--- sdlpp/sdlpp_scene.cpp | 6 ++++- sdlpp/sdlpp_scene.hpp | 2 +- 6 files changed, 74 insertions(+), 16 deletions(-) diff --git a/sdlpp/sdlpp_rectrenderer.cpp b/sdlpp/sdlpp_rectrenderer.cpp index b4b39de..397781f 100644 --- a/sdlpp/sdlpp_rectrenderer.cpp +++ b/sdlpp/sdlpp_rectrenderer.cpp @@ -22,7 +22,7 @@ RectangleRender::RectangleRender( double x, double y, double w, double h, RectangleRender::RectangleRender( double x, double y, double w, double h, const std::shared_ptr< Renderer > &r, const std::shared_ptr< Texture > &t, - SDL_Rect source_rect ) + const SDL_Rect &source_rect ) : RectangleRender( x, y, w, h, r ) { setTexture( t, source_rect ); } @@ -48,7 +48,8 @@ RectangleRender::RectangleRender( double x, double y, double w, double h, } RectangleRender::RectangleRender( double x, double y, double w, double h, const std::shared_ptr< Renderer > &r, - const std::string &img, SDL_Rect source_rect ) + const std::string &img, + const SDL_Rect &source_rect ) : RectangleRender( x, y, w, h, r ) { setTexture( img, source_rect ); } diff --git a/sdlpp/sdlpp_rectrenderer.hpp b/sdlpp/sdlpp_rectrenderer.hpp index 370d311..aa9d934 100644 --- a/sdlpp/sdlpp_rectrenderer.hpp +++ b/sdlpp/sdlpp_rectrenderer.hpp @@ -20,17 +20,17 @@ public: RectangleRender( double x, double y, double w, double h, const std::shared_ptr< Renderer > &r, const std::shared_ptr< Texture > &t, - SDL_Rect source_rect = { -1, -1, -1, -1 } ); + const SDL_Rect &source_rect = { -1, -1, -1, -1 } ); RectangleRender( double x, double y, double w, double h, const std::shared_ptr< Renderer > &r, const std::string &img_or_color, bool is_polygon = false ); RectangleRender( double x, double y, double w, double h, const std::shared_ptr< Renderer > &r, - const std::string &img, int source_x, - int source_y, int source_width, int source_height ); + const std::string &img, int source_x, int source_y, + int source_width, int source_height ); RectangleRender( double x, double y, double w, double h, const std::shared_ptr< Renderer > &r, - const std::string &img, SDL_Rect source_rect ); + const std::string &img, const SDL_Rect &source_rect ); virtual void setColor( const std::string &color ) override; virtual void setOutlineColor( const std::string &color ) override; virtual void specialAction( int /*UNUSED*/ ) override {} diff --git a/sdlpp/sdlpp_renderobject.cpp b/sdlpp/sdlpp_renderobject.cpp index b6a5e5c..5beec94 100644 --- a/sdlpp/sdlpp_renderobject.cpp +++ b/sdlpp/sdlpp_renderobject.cpp @@ -12,8 +12,12 @@ void RenderObject::render() { polygon->render( *renderer ); if ( texture != NULL ) { SDL_Rect *src = NULL; - if ( !entireTexture() ) - src = &src_rect; + if ( animation.empty() ) { + if ( !entireTexture() ) + src = &src_rect; + } else { + src = &animation[animation_index]; + } SDL_RenderCopy( renderer->getRendererPtr(), texture->getTexturePtr(), src, &rect ); } @@ -55,7 +59,7 @@ RenderObject::getCollisions() const { return collisions; } void RenderObject::setTexture( const std::shared_ptr< Texture > &t, - SDL_Rect source_rect ) { + const SDL_Rect &source_rect ) { texture = t; src_rect = source_rect; } @@ -65,7 +69,7 @@ void RenderObject::setTexture( const std::shared_ptr< Texture > &t, setTexture( t, { source_x, source_y, source_width, source_height } ); } void RenderObject::setTexture( const std::string &img_path, - SDL_Rect source_rect ) { + const SDL_Rect &source_rect ) { texture = std::make_shared< Texture >( renderer, img_path ); src_rect = source_rect; } @@ -133,6 +137,18 @@ bool RenderObject::getKilled() { void RenderObject::setColiderColor( const std::string &color ) { colider_color = getColorsHEX( color ); } + +void RenderObject::animate( int ticks ) { + if ( animating && !animation.empty() ) { + animation_next_frame -= ticks; + if ( animation_next_frame <= 0 ) { + animation_index = ( animation_index + 1 ) % animation.size(); + animation_next_frame = + ( 1000 / animation_fps ) + animation_next_frame; + } + } +} + void RenderObject::move( int ticks ) { if ( permanent ) return; @@ -189,10 +205,33 @@ void RenderObject::copyTo( std::shared_ptr< RenderObject > other ) { void RenderObject::setTextureAlpha( uint8_t alpha ) { texture->setAlpha( alpha ); } -void RenderObject::setTextureSourceRect( SDL_Rect source_rect ) { +void RenderObject::setTextureSourceRect( const SDL_Rect &source_rect ) { src_rect = source_rect; } void RenderObject::setTextureSourceRect( int x, int y, int w, int h ) { setTextureSourceRect( { x, y, w, h } ); } +void RenderObject::setAnimationFrames( const std::vector< SDL_Rect > &frames ) { + animation = frames; +} +void RenderObject::addAnimationFrame( const SDL_Rect &frame ) { + animation.push_back( frame ); +} +void RenderObject::addAnimationFrame( const int x, const int y, const int w, + const int h ) { + addAnimationFrame( { x, y, w, h } ); +} +void RenderObject::pauseAnimation() { + animating = false; +} +void RenderObject::resumeAnimation() { + animating = true; +} +void RenderObject::removeAnimation() { + animation.clear(); +} +void RenderObject::setAnimationSpeed( const int fps ) { + animation_fps = fps; + animation_next_frame = 1000 / fps; +} } // namespace SDLPP diff --git a/sdlpp/sdlpp_renderobject.hpp b/sdlpp/sdlpp_renderobject.hpp index 16184d1..155962a 100644 --- a/sdlpp/sdlpp_renderobject.hpp +++ b/sdlpp/sdlpp_renderobject.hpp @@ -46,12 +46,12 @@ public: int source_y, int source_width, int source_height ); virtual void setTexture( const std::shared_ptr< Texture > &t, - SDL_Rect source_rect = { -1, -1, -1, -1 } ); + const SDL_Rect &source_rect = { -1, -1, -1, -1 } ); virtual void setTexture( const std::string &img_path, int source_x, int source_y, int source_width, int source_height ); virtual void setTexture( const std::string &img_path, - SDL_Rect source_rect = { -1, -1, -1, -1 } ); + const SDL_Rect &source_rect = { -1, -1, -1, -1 } ); virtual void setTexture( Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", @@ -87,8 +87,17 @@ public: void setStatic( bool stat = true ); std::shared_ptr< Renderer > getRenderer() const; void setTextureAlpha( uint8_t alpha ); - void setTextureSourceRect( SDL_Rect source_rect ); + void setTextureSourceRect( const SDL_Rect &source_rect ); void setTextureSourceRect( int x, int y, int w, int h ); + void setAnimationFrames( const std::vector< SDL_Rect > &frames ); + void addAnimationFrame( const SDL_Rect &frame ); + void addAnimationFrame( const int x, const int y, const int w, + const int h ); + void setAnimationSpeed( const int fps ); + void pauseAnimation(); + void resumeAnimation(); + void removeAnimation(); + void animate( int ticks ); protected: virtual void copyTo( std::shared_ptr< RenderObject > other ); @@ -111,6 +120,11 @@ protected: bool centerx = false; SDL_Rect rect; SDL_Rect src_rect = { -1, -1, -1, -1 }; + size_t animation_index = 0; + size_t animation_fps = 1; + int animation_next_frame = 1000; + std::vector< SDL_Rect > animation{}; + bool animating = true; private: void setSceneID( int id ); diff --git a/sdlpp/sdlpp_scene.cpp b/sdlpp/sdlpp_scene.cpp index cb3a6e3..a3df905 100644 --- a/sdlpp/sdlpp_scene.cpp +++ b/sdlpp/sdlpp_scene.cpp @@ -76,12 +76,16 @@ Scene::getObjects( const std::unordered_set< int > &objectIDs ) { } return ret; } -void Scene::movement() { +void Scene::updateScene() { + // check for objects that should be removed checkKilled(); std::lock_guard< std::mutex > lock( render_mutex ); int now_ticks = SDL_GetTicks(); for ( const auto &x : render_objects ) { + // move objects that should be moved x->move( now_ticks - prev_ticks ); + // animate objects that should be animated + x->animate( now_ticks - prev_ticks ); } prev_ticks = now_ticks; } diff --git a/sdlpp/sdlpp_scene.hpp b/sdlpp/sdlpp_scene.hpp index c3d0d58..91b27ea 100644 --- a/sdlpp/sdlpp_scene.hpp +++ b/sdlpp/sdlpp_scene.hpp @@ -23,7 +23,7 @@ public: std::vector< std::shared_ptr< RenderObject > > getObjects(); std::vector< std::shared_ptr< RenderObject > > getObjects( const std::unordered_set< int > &objectIDs ); - void movement(); + void updateScene(); std::vector< std::shared_ptr< RenderObject > > getCollisions( RenderObject &r ); std::vector< std::shared_ptr< RenderObject > >