From a294b1de249c27d23d5cfc97da2d7c7a2c93971f Mon Sep 17 00:00:00 2001 From: Vectozavr <60608292+vectozavr@users.noreply.github.com> Date: Sun, 31 Oct 2021 01:07:14 +0700 Subject: [PATCH] replace Animation* in Timeline.h with a shared_ptr --- PlayerController.cpp | 24 ++++++++++++------------ Shooter.cpp | 6 +++--- engine/animation/Timeline.cpp | 9 +++------ engine/animation/Timeline.h | 5 ++--- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/PlayerController.cpp b/PlayerController.cpp index 26c908d..a441a7c 100644 --- a/PlayerController.cpp +++ b/PlayerController.cpp @@ -44,25 +44,25 @@ void PlayerController::update() { std::shared_ptr camera = _player->attached(ObjectNameTag("Camera")); if(camera != nullptr && _inRunning && _player->inCollision()) { if (!Timeline::isInAnimList(AnimationListTag("camera_hor_oscil"))) { - Timeline::animate(AnimationListTag("camera_hor_oscil"), new ATranslate(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::InterpolationType::Cos)); - Timeline::animate(AnimationListTag("camera_hor_oscil"), new AWait(0)); - Timeline::animate(AnimationListTag("camera_hor_oscil"), new ATranslate(camera, camera->left() / 6, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_hor_oscil"), std::make_shared(camera, -camera->left() / 6, 0.3,Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_hor_oscil"), std::make_shared(0)); + Timeline::animate(AnimationListTag("camera_hor_oscil"), std::make_shared(camera, camera->left() / 6, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), new ATranslate(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), new AWait(0)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), new ATranslate(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), new AWait(0)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), new ATranslate(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), new AWait(0)); - Timeline::animate(AnimationListTag("camera_vert_oscil"), new ATranslate(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(0)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(0)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(camera, -Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(0)); + Timeline::animate(AnimationListTag("camera_vert_oscil"), std::make_shared(camera, Vec3D{0, 1, 0} / 12, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); - Timeline::animate(AnimationListTag("camera_init"), new ATranslateToPoint( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_init"), std::make_shared( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.3, Animation::LoopOut::None, Animation::InterpolationType::Cos)); } } else if(camera != nullptr && inRunning_old && !_inRunning) { Timeline::deleteAnimationList(AnimationListTag("camera_hor_oscil")); Timeline::deleteAnimationList(AnimationListTag("camera_vert_oscil")); Timeline::deleteAnimationList(AnimationListTag("camera_init")); - Timeline::animate(AnimationListTag("camera_init"), new ATranslateToPoint( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); + Timeline::animate(AnimationListTag("camera_init"), std::make_shared( camera, _player->position() + Vec3D{0, 1.8, 0}, 0.15, Animation::LoopOut::None, Animation::InterpolationType::Cos)); } // Left and right diff --git a/Shooter.cpp b/Shooter.cpp index fb4a311..ff8c8b6 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -263,8 +263,8 @@ void Shooter::addFireTrace(const Vec3D &from, const Vec3D &to) { world->addBody(std::make_shared(Mesh::LineTo(ObjectNameTag(traceName), from, to, 0.05))); world->body(ObjectNameTag(traceName))->setCollider(false); - Timeline::animate(AnimationListTag(traceName + "_fadeOut"), new AColor(world->body(ObjectNameTag(traceName)), {150, 150, 150, 0})); - Timeline::animate(AnimationListTag(traceName + "_delete"), new AFunction([this, traceName](){ removeFireTrace(ObjectNameTag(traceName)); }, 1, 2)); + Timeline::animate(AnimationListTag(traceName + "_fadeOut"), std::make_shared(world->body(ObjectNameTag(traceName)), sf::Color{150, 150, 150, 0})); + Timeline::animate(AnimationListTag(traceName + "_delete"), std::make_shared([this, traceName](){ removeFireTrace(ObjectNameTag(traceName)); }, 1, 2)); } void Shooter::removeFireTrace(const ObjectNameTag& traceName) { @@ -279,7 +279,7 @@ void Shooter::addBonus(const string &bonusName, const Vec3D &position) { world->addBody(std::make_shared(ObjectNameTag(bonusName), "obj/" + name + ".obj", Vec3D{3, 3, 3})); world->body(ObjectNameTag(bonusName))->translateToPoint(position); world->body(ObjectNameTag(bonusName))->setCollider(false); - Timeline::animate(AnimationListTag(bonusName + "_rotation"), new ARotate(world->body(ObjectNameTag(bonusName)), Vec3D{0, 2*Consts::PI, 0}, 4, Animation::LoopOut::Continue, Animation::InterpolationType::Linear)); + Timeline::animate(AnimationListTag(bonusName + "_rotation"), std::make_shared(world->body(ObjectNameTag(bonusName)), Vec3D{0, 2*Consts::PI, 0}, 4, Animation::LoopOut::Continue, Animation::InterpolationType::Linear)); } void Shooter::removeBonus(const ObjectNameTag &bonusName) { diff --git a/engine/animation/Timeline.cpp b/engine/animation/Timeline.cpp index e698591..bf498a5 100644 --- a/engine/animation/Timeline.cpp +++ b/engine/animation/Timeline.cpp @@ -5,6 +5,7 @@ #include #include "Animation.h" #include "Timeline.h" +#include Timeline* Timeline::_instance = nullptr; bool Timeline::_validInstance = false; @@ -14,7 +15,7 @@ void Timeline::init() { _validInstance = true; } -void Timeline::animate(const AnimationListTag& listName, Animation* anim) { +void Timeline::animate(const AnimationListTag& listName, std::shared_ptr anim) { if(!_validInstance) { return; } @@ -28,10 +29,6 @@ void Timeline::deleteAllAnimations() { } for (auto& [listName, animationList] : _instance->_animations) { - auto it = animationList.begin(); - while(it != animationList.end()) { - delete *(it++); - } animationList.clear(); } _instance->_animations.clear(); @@ -60,8 +57,8 @@ void Timeline::update() { } for (auto& [listName, animationList] : _instance->_animations) { - if (animationList.empty()) { + _instance->_animations.erase(listName); continue; } auto it = animationList.begin(); diff --git a/engine/animation/Timeline.h b/engine/animation/Timeline.h index ae07b5b..f8c8296 100644 --- a/engine/animation/Timeline.h +++ b/engine/animation/Timeline.h @@ -21,8 +21,7 @@ public: class Timeline { private: - // TODO: replace Animation* with shared_ptr & check for possible memory leaks - std::map> _animations; + std::map>> _animations; static Timeline* _instance; static bool _validInstance; @@ -33,7 +32,7 @@ public: Timeline& operator=(Timeline&) = delete; static void update(); - static void animate(const AnimationListTag& listName, Animation* anim); + static void animate(const AnimationListTag& listName, std::shared_ptr anim); static void deleteAllAnimations(); static void deleteAnimationList(const AnimationListTag& listName);