Allowed scene items cleanup to happen earlier to prevent access null weak pointers. Also added Sphere Mesh and Sphere Collider(s)

This commit is contained in:
2024-11-26 18:44:52 -06:00
parent e91b1983c8
commit f4120095ed
22 changed files with 380 additions and 79 deletions

View File

@@ -16,11 +16,22 @@ Scene::Scene(
{
}
void Scene::stage() {
void Scene::init() {
Scene &selfReference = *this;
sceneInitializer(selfReference);
}
void Scene::deinit() {
if(!this->hasInitialized) return;
this->hasInitialized = false;
auto items = this->sceneItems;
for(auto &item : items) {
item->deinit();
}
sceneItems.clear();
}
void Scene::update() {
// Initialize new scene items
if(!hasInitialized) {
@@ -34,8 +45,11 @@ void Scene::update() {
auto itRemove = sceneItemsToRemove.begin();
while(itRemove != sceneItemsToRemove.end()) {
auto item = *itRemove;
item->deinit();
auto it = std::find(sceneItems.begin(), sceneItems.end(), item);
if(it != sceneItems.end()) sceneItems.erase(it);
if(it != sceneItems.end()) {
sceneItems.erase(it);
}
itRemove++;
}
sceneItemsToRemove.clear();
@@ -63,8 +77,12 @@ std::shared_ptr<SceneItem> Scene::createSceneItem() {
}
void Scene::removeItem(const std::shared_ptr<SceneItem> item) {
auto index = std::find(sceneItems.begin(), sceneItems.end(), item);
if(index == sceneItems.end()) return;
sceneItemsToRemove.push_back(item);
}
Scene::~Scene() {
this->deinit();
}

View File

@@ -44,7 +44,12 @@ namespace Dawn {
/**
* Stages all of the scene items on the scene.
*/
void stage();
void init();
/**
* Called when the scene is supposed to be deinitialized.
*/
void deinit();
/**
* Called by the game every frame that the scene is set as the currently

View File

@@ -57,17 +57,17 @@ bool_t SceneComponent::isInitialized() {
}
std::shared_ptr<SceneItem> SceneComponent::getItem() {
return this->item.lock();
auto item = this->item.lock();
assertNotNull(item, "SceneItem has unloaded?");
return item;
}
std::shared_ptr<Scene> SceneComponent::getScene() {
auto item = this->getItem();
return item->getScene();
return this->getItem()->getScene();
}
std::shared_ptr<Game> SceneComponent::getGame() {
auto scene = this->getScene();
return scene->getGame();
return this->getScene()->getGame();
}
SceneComponent::~SceneComponent() {

View File

@@ -20,7 +20,9 @@ std::shared_ptr<SceneItem> SceneItem::sceneItemComponentsSelf() {
}
std::shared_ptr<Scene> SceneItem::getScene() {
return scene.lock();
auto s = scene.lock();
assertNotNull(s, "Scene has unloaded?");
return s;
}
void SceneItem::init() {
@@ -50,6 +52,19 @@ void SceneItem::init() {
}
}
void SceneItem::deinit() {
// Create copy of the components, components may chose to add more components
// but those sub components will not be disposed at this time.
auto components = this->components;
for(auto &component : components) {
if(!component->isInitialized()) continue;
component->dispose();
}
this->components.clear();
}
void SceneItem::remove() {
auto scene = getScene();
if(!scene) return;
@@ -57,11 +72,5 @@ void SceneItem::remove() {
}
SceneItem::~SceneItem() {
std::for_each(
components.begin(),
components.end(),
[](auto &component) {
component->dispose();
}
);
this->deinit();
}

View File

@@ -35,6 +35,12 @@ namespace Dawn {
*/
void init();
/**
* Called when the scene item is supposed to deinitialize. Should happen
* when the scene item is removed from the scene.
*/
void deinit();
/**
* Returns the scene that this scene item belongs to.
*