Changed scene item components to smart pointers
This commit is contained in:
@ -13,7 +13,7 @@ namespace Dawn {
|
||||
|
||||
class SceneItem : public StateOwner {
|
||||
private:
|
||||
std::vector<SceneItemComponent*> components;
|
||||
std::vector<std::shared_ptr<SceneItemComponent>> components;
|
||||
|
||||
public:
|
||||
Scene *scene;
|
||||
@ -52,9 +52,8 @@ namespace Dawn {
|
||||
* @return A shared pointer to the newly added component.
|
||||
*/
|
||||
template<class T>
|
||||
T * addComponent() {
|
||||
auto component = new T(this);
|
||||
assertNotNull(component, "SceneItem::addComponent: Component could not be created (Memory filled?)");
|
||||
std::shared_ptr<T> addComponent() {
|
||||
auto component = std::make_shared<T>(this);
|
||||
this->components.push_back(component);
|
||||
return component;
|
||||
}
|
||||
@ -68,10 +67,10 @@ namespace Dawn {
|
||||
* @return A shared pointer to the component, or nullptr if not found.
|
||||
*/
|
||||
template<class T>
|
||||
T * getComponent() {
|
||||
std::shared_ptr<T> getComponent() {
|
||||
auto it = this->components.begin();
|
||||
while(it != this->components.end()) {
|
||||
T *castedAs = dynamic_cast<T*>(*it);
|
||||
std::shared_ptr<T> castedAs = std::dynamic_pointer_cast<T>(*it);
|
||||
if(castedAs != nullptr) return castedAs;
|
||||
++it;
|
||||
}
|
||||
@ -88,11 +87,11 @@ namespace Dawn {
|
||||
* @return An array of pointers to the components.
|
||||
*/
|
||||
template<class T>
|
||||
std::vector<T*> getComponents() {
|
||||
std::vector<T*> components;
|
||||
std::vector<std::shared_ptr<T>> getComponents() {
|
||||
std::vector<std::shared_ptr<T>> components;
|
||||
auto it = this->components.begin();
|
||||
while(it != this->components.end()) {
|
||||
T *castedAs = dynamic_cast<T*>(*it);
|
||||
T *castedAs = std::dynamic_pointer_cast<T>(it);
|
||||
if(castedAs != nullptr) components.push_back(castedAs);
|
||||
++it;
|
||||
}
|
||||
@ -106,7 +105,7 @@ namespace Dawn {
|
||||
* @return Pointer to the child, or nullptr if not found.
|
||||
*/
|
||||
template<class T>
|
||||
T * findChild() {
|
||||
std::shared_ptr<T> findChild() {
|
||||
auto it = this->transform.children.begin();
|
||||
while(it != this->transform.children.end()) {
|
||||
auto child = (*it)->item->getComponent<T>();
|
||||
@ -124,9 +123,9 @@ namespace Dawn {
|
||||
* @return Array of pointers to matching children.
|
||||
*/
|
||||
template<class T>
|
||||
std::vector<T*> findChildren() {
|
||||
std::vector<std::shared_ptr<T>> findChildren() {
|
||||
auto it = this->transform.children.begin();
|
||||
std::vector<T*> children;
|
||||
std::vector<std::shared_ptr<T>> children;
|
||||
while(it != this->transform.children.end()) {
|
||||
auto child = (*it)->item->getComponent<T>();
|
||||
if(child != nullptr) children.push_back(child);
|
||||
@ -143,9 +142,9 @@ namespace Dawn {
|
||||
* @return Array of pointers to matching children components.
|
||||
*/
|
||||
template<class T>
|
||||
std::vector<T*> findChildrenDeep() {
|
||||
std::vector<std::shared_ptr<T>> findChildrenDeep() {
|
||||
std::vector<Transform*> transformsToCheck = this->transform.children;
|
||||
std::vector<T*> itemsFound;
|
||||
std::vector<std::shared_ptr<T>> itemsFound;
|
||||
|
||||
while(transformsToCheck.size() > 0) {
|
||||
Transform *tras = *transformsToCheck.begin();
|
||||
|
Reference in New Issue
Block a user