From d09465cf4ee3990c9552a3b08d142469d0143c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Wed, 30 Mar 2016 14:02:31 +0200 Subject: [PATCH] Utilisation de pointeurs intelligents pour la liste d'objets --- include/object.hpp | 8 +++++--- src/object.cpp | 10 +++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/object.hpp b/include/object.hpp index 7edfdac..21a13ed 100644 --- a/include/object.hpp +++ b/include/object.hpp @@ -7,6 +7,8 @@ #include "manager.hpp" #include "resource_manager.hpp" +typedef std::shared_ptr ObjectPtr; + class Object { private: sf::Vector2f acceleration; @@ -28,7 +30,7 @@ protected: /** * Calcule les forces appliquées à l'objet */ - virtual sf::Vector2f getForces(const Manager& manager, const std::vector& objects) const; + virtual sf::Vector2f getForces(const Manager& manager, const std::vector& objects) const; public: Object(float x, float y); @@ -43,7 +45,7 @@ public: * Met à jour la vitesse de l'objet selon les * forces qui lui sont appliquées */ - void updateVelocity(const Manager& manager, const std::vector& objects, float delta); + void updateVelocity(const Manager& manager, const std::vector& objects, float delta); /** * Met à jour la position de l'objet selon sa @@ -183,7 +185,7 @@ public: * qui doit être dessinée avant celle du second */ struct ObjectCompare { - bool operator()(Object* const &t1, Object* const &t2) const; + bool operator()(ObjectPtr const &t1, ObjectPtr const &t2) const; }; #endif diff --git a/src/object.cpp b/src/object.cpp index 102c36c..a1ce098 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -18,7 +18,7 @@ Object::Object(float x, float y) : Object::~Object() {} sf::Vector2f Object::getForces( - const Manager& manager, const std::vector& objects + const Manager& manager, const std::vector& objects ) const { sf::Vector2f forces(0, 0); @@ -28,9 +28,9 @@ sf::Vector2f Object::getForces( // force d'attraction entre objets chargés if (getCharge() != 0) { for (unsigned int j = 0; j < objects.size(); j++) { - Object *attractive = objects[j]; + ObjectPtr attractive = objects[j]; - if (attractive == this || attractive->getCharge() == 0) { + if (attractive.get() == this || attractive->getCharge() == 0) { continue; } @@ -79,7 +79,7 @@ void Object::draw(Manager& manager) { } void Object::updateVelocity( - const Manager& manager, const std::vector& objects, float delta + const Manager& manager, const std::vector& objects, float delta ) { acceleration = getForces(manager, objects) * getMassInvert(); velocity += acceleration * delta; @@ -267,6 +267,6 @@ void Object::setLayer(int set_layer) { layer = set_layer; } -bool ObjectCompare::operator()(Object* const &t1, Object* const &t2) const { +bool ObjectCompare::operator()(ObjectPtr const &t1, ObjectPtr const &t2) const { return t1->getLayer() > t2->getLayer(); }