Adding some components.

This commit is contained in:
2023-11-15 22:14:53 -06:00
parent 6c6203a41d
commit d8bc1d0fe3
18 changed files with 428 additions and 12 deletions

View File

@@ -3,15 +3,58 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "assert/assert.hpp"
#include "util/Flag.hpp"
#include "SceneComponent.hpp"
using namespace Dawn;
void SceneComponent::init(const std::shared_ptr<SceneItem> item) {
assertFlagOff(
sceneComponentState,
SCENE_COMPONENT_STATE_INIT,
"SceneComponent is already initialized!"
);
Flag::turnOn<uint_fast8_t>(
sceneComponentState,
SCENE_COMPONENT_STATE_INIT
);
this->item = item;
this->onInit();
}
void SceneComponent::dispose() {
assertFlagOn(
sceneComponentState,
SCENE_COMPONENT_STATE_INIT,
"SceneComponent is not initialized!"
);
assertFlagOff(
sceneComponentState,
SCENE_COMPONENT_STATE_DISPOSED,
"SceneComponent is already disposed!"
);
Flag::turnOn<uint_fast8_t>(
sceneComponentState,
SCENE_COMPONENT_STATE_DISPOSED
);
this->onDispose();
this->item.reset();
}
std::shared_ptr<SceneItem> SceneComponent::getItem() {
return this->item.lock();
}
SceneComponent::~SceneComponent() {
if(Flag::isOn<uint_fast8_t>(
sceneComponentState,
SCENE_COMPONENT_STATE_INIT
)) {
assertFlagOn(
sceneComponentState,
SCENE_COMPONENT_STATE_DISPOSED,
"SceneComponent is initialized but was not properly disposed!"
);
}
}

View File

@@ -6,12 +6,16 @@
#pragma once
#include "dawnlibs.hpp"
#define SCENE_COMPONENT_STATE_INIT 0x01
#define SCENE_COMPONENT_STATE_DISPOSED 0x02
namespace Dawn {
class SceneItem;
class SceneComponent : std::enable_shared_from_this<SceneComponent> {
private:
std::weak_ptr<SceneItem> item;
uint_fast8_t sceneComponentState = 0;
protected:
/**
@@ -20,6 +24,12 @@ namespace Dawn {
*/
virtual void onInit() = 0;
/**
* Custom component listener that is invoked when the component is meant
* to dispose.
*/
virtual void onDispose() = 0;
public:
/**
* Initializes this scene component.
@@ -28,11 +38,21 @@ namespace Dawn {
*/
void init(const std::shared_ptr<SceneItem> item);
/**
* Disposes this scene component.
*/
void dispose();
/**
* Returns the scene item that this scene component belongs to.
*
* @return Reference to the scene item that this component belongs to.
*/
std::shared_ptr<SceneItem> getItem();
/**
* Disposes this scene component.
*/
virtual ~SceneComponent();
};
}

View File

@@ -24,5 +24,11 @@ std::shared_ptr<Scene> SceneItem::getScene() {
}
SceneItem::~SceneItem() {
std::for_each(
components.begin(),
components.end(),
[](auto &component) {
component->dispose();
}
);
}

View File

@@ -16,6 +16,7 @@ void SceneItemComponents::removeComponent(
) {
auto it = std::find(components.begin(), components.end(), component);
if(it == components.end()) return; //Not found?
it->get()->dispose();
components.erase(it);
}