Scene.cpp
Scene.cpp
CPP1/* Copyright Statement:2 *3 * (C) 2021-2024 Cubios Inc. All rights reserved.4 */56#include "Scene.h"78namespace Cubios9{1011Scene::Scene()12{13}1415Scene::~Scene()16{17 this->DisposeAllObjects();18}1920SceneObject* Scene::operator[](uint32_t objectKey)21 {22 return this->root[objectKey];23 }2425uint32_t Scene::CreateObject(SceneObject* obj)26{27 if(obj==nullptr) return false;2829 uint32_t s = (uint32_t)this->root.size();30 obj->SetId(s);31 this->root[s] = obj;3233 return s;34}3536bool Scene::CreateObjectWithID(uint32_t id, SceneObject* obj)37{38 bool ret = true;39 if(obj==nullptr) return false;4041 if(this->root.find(id)==this->root.end())42 {43 obj->SetId(id);44 this->root[id] = obj;45 }46 else47 {48 LOG_e("CreateObjectWithID: Resource with id %d already exists\n",id);49 ret = false;50 }5152 return ret;53}5455uint32_t Scene::CreateSound(Sound* obj)56{57 if(obj==nullptr) return false;5859 uint32_t s = (uint32_t)this->sounds.size();60 obj->SetId(s);61 this->sounds[s] = obj;6263 return s;64}6566bool Scene::CreateSoundWithID(uint32_t id, Sound* obj)67{68 bool ret = true;69 if(obj==nullptr) return false;7071 if(this->sounds.find(id)==this->sounds.end())72 {73 obj->SetId(id);74 this->sounds[id] = obj;75 }76 else77 {78 LOG_e("CreateSoundWithID: Resource with id %d already exists\n",id);79 ret = false;80 }8182 return ret;83}8485bool Scene::DisposeObjectWithID(uint32_t id)86{87 bool ret = true;88 std::map<uint32_t, SceneObject*>::iterator it = this->root.find(id);8990 if(it!=this->root.end())91 {92 if(this->root[id]!=nullptr)93 {94 delete this->root[id];95 }9697 this->root.erase(it);98 }99 else100 {101 ret = false;102 }103104 return ret;105}106107bool Scene::DisposeSoundWithID(uint32_t id)108{109 bool ret = true;110 std::map<uint32_t, Sound*>::iterator it = this->sounds.find(id);111112 if(it!=this->sounds.end())113 {114 if(this->sounds[id]!=nullptr)115 {116 delete this->sounds[id];117 }118119 this->sounds.erase(it);120 }121 else122 {123 ret = false;124 }125126 return ret;127}128129void Scene::DisposeAllObjects()130{131 for (auto it = this->root.begin(); it != this->root.end(); it++)132 {133 if((*it).second!=nullptr)134 {135 delete (*it).second;136 }137 }138139 for (auto it = this->sounds.begin(); it != this->sounds.end(); it++)140 {141 if((*it).second!=nullptr)142 {143 delete (*it).second;144 }145 }146147 this->root.clear();148 this->sounds.clear();149}150151void Scene::Play(uint32_t id,uint8_t volume)152{153 if(this->sounds.count(id)!=0)154 {155 this->sounds[id]->Play(volume);156 }157}158159}160
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.