Changed scene item components to smart pointers

This commit is contained in:
2023-11-10 23:21:59 -06:00
parent bed6e3e1f4
commit 3ab98bf2ea
33 changed files with 84 additions and 97 deletions

View File

@ -59,7 +59,7 @@ void RenderPipeline::renderScene(std::shared_ptr<Scene> scene) {
// Now render backbuffers.
auto backBuffer = rm->getBackBuffer();
auto cameras = scene->findComponents<Camera>();
Camera *backBufferCamera = nullptr;
std::shared_ptr<Camera> backBufferCamera;
// First, render all non-backbuffer cameras.
auto it = cameras.begin();
@ -74,7 +74,7 @@ void RenderPipeline::renderScene(std::shared_ptr<Scene> scene) {
} else if(cameraTarget == backBuffer) {
backBufferCamera = *it;
} else {
renderSceneCamera(&(*scene), *it);
renderSceneCamera(scene, *it);
}
++it;
@ -82,10 +82,10 @@ void RenderPipeline::renderScene(std::shared_ptr<Scene> scene) {
// Now render the backbuffer camera.
if(backBufferCamera == nullptr) return;
renderSceneCamera(&(*scene), backBufferCamera);
renderSceneCamera(scene, backBufferCamera);
}
void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
void RenderPipeline::renderSceneCamera(std::shared_ptr<Scene> scene, std::shared_ptr<Camera> camera) {
auto rm = renderManager.lock();
assertNotNull(rm, "RenderPipeline::renderSceneCamera: RenderManager cannot be null");
@ -114,7 +114,7 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
struct IRenderableContext context = {
scene,
camera,
this
shared_from_this()
};
// Get the list of things to render first.

View File

@ -13,7 +13,7 @@
namespace Dawn {
class RenderManager;
class RenderPipeline {
class RenderPipeline : public std::enable_shared_from_this<RenderPipeline> {
private:
int_fast16_t renderId = -1;
@ -54,7 +54,7 @@ namespace Dawn {
* @param scene Scene to render.
* @param camera Camera within the scene to render.
*/
virtual void renderSceneCamera(Scene *scene, Camera *camera);
virtual void renderSceneCamera(std::shared_ptr<Scene> scene, std::shared_ptr<Camera> camera);
/**
* Cleanup a render pipeline that has been initialized.

View File

@ -17,7 +17,7 @@ namespace Dawn {
public SceneItemPrefab<SimpleSpinningCubePrefab>
{
public:
SimpleTexturedMaterial *material;
std::shared_ptr<SimpleTexturedMaterial> material;
static std::vector<std::shared_ptr<Asset>> prefabAssets(AssetManager *man) {
return std::vector<std::shared_ptr<Asset>>();

View File

@ -20,10 +20,10 @@ namespace Dawn {
typedef int32_t sceneitemid_t;
template<class T>
T * _sceneForwardGetComponent(SceneItem *item);
std::shared_ptr<T> _sceneForwardGetComponent(SceneItem *item);
template<class T>
std::vector<T*> _sceneForwardGetComponents(SceneItem *item);
std::vector<std::shared_ptr<T>> _sceneForwardGetComponents(SceneItem *item);
class Scene : public StateOwner {
private:
@ -95,7 +95,7 @@ namespace Dawn {
* @return Pointer to the found component (and by extension the item).
*/
template<class T>
T * findComponent() {
std::shared_ptr<T> findComponent() {
auto it = this->itemsNotInitialized.begin();
while(it != this->itemsNotInitialized.end()) {
auto component = _sceneForwardGetComponent<T>(it->second);
@ -119,8 +119,8 @@ namespace Dawn {
* @return List of matching compnoents.
*/
template<class T>
std::vector<T*> findComponents() {
std::vector<T*> components;
std::vector<std::shared_ptr<T>> findComponents() {
std::vector<std::shared_ptr<T>> components;
auto it = this->itemsNotInitialized.begin();
while(it != this->itemsNotInitialized.end()) {

View File

@ -60,18 +60,8 @@ void SceneItem::init() {
}
void SceneItem::destroy() {
auto it = this->components.begin();
while(it != this->components.end()) {
(*it)->onDispose();
++it;
}
this->components.clear();
}
SceneItem::~SceneItem() {
auto it = this->components.begin();
it = this->components.begin();
while(it != this->components.end()) {
delete *it;
++it;
}
}

View File

@ -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();

View File

@ -21,8 +21,8 @@ void SceneItemComponent::init() {
this->hasInitialized = true;
}
std::vector<SceneItemComponent*> SceneItemComponent::getDependencies() {
return std::vector<SceneItemComponent*>();
std::vector<std::shared_ptr<SceneItemComponent>> SceneItemComponent::getDependencies() {
return {};
}
Scene * SceneItemComponent::getScene() {

View File

@ -39,7 +39,7 @@ namespace Dawn {
*
* @return Array of dependencies.
*/
virtual std::vector<SceneItemComponent*> getDependencies();
virtual std::vector<std::shared_ptr<SceneItemComponent>> getDependencies();
/**
* Shorthand to return the scene that this component's item belongs to.
@ -80,12 +80,12 @@ namespace Dawn {
};
template<class T>
T * _sceneForwardGetComponent(SceneItem *item) {
std::shared_ptr<T> _sceneForwardGetComponent(SceneItem *item) {
return item->getComponent<T>();
}
template<class T>
T * _sceneForwardGetComponents(SceneItem *item) {
std::shared_ptr<T> _sceneForwardGetComponents(SceneItem *item) {
return item->getComponents<T>();
}
}

View File

@ -22,7 +22,7 @@ namespace Dawn {
std::function<void()> evtResized;
public:
static Camera * create(Scene *scene) {
static std::shared_ptr<Camera> create(Scene *scene) {
auto item = scene->createSceneItem();
auto cam = item->addComponent<Camera>();
return cam;

View File

@ -11,7 +11,7 @@ namespace Dawn {
class CameraTexture : public SceneItemComponent {
public:
// @optional
StateProperty<Camera*> camera;
StateProperty<std::shared_ptr<Camera>> camera;
std::shared_ptr<TextureRenderTarget> renderTarget;
CameraTexture(SceneItem *item);

View File

@ -12,9 +12,9 @@ namespace Dawn {
class RenderPipeline;
struct IRenderableContext {
Scene *scene;
Camera *camera;
RenderPipeline *renderPipeline;
std::shared_ptr<Scene> scene;
std::shared_ptr<Camera> camera;
std::shared_ptr<RenderPipeline> renderPipeline;
};
class IRenderable {

View File

@ -55,7 +55,7 @@ void PixelPerfectCamera::onStart() {
}, scale);
useEffectWithTeardown([&]{
if(!camera) {
if(!camera._realValue) {
return teardown = [&]{};
}

View File

@ -18,7 +18,7 @@ namespace Dawn {
public:
// @optional
StateProperty<Camera*> camera;
StateProperty<std::shared_ptr<Camera>> camera;
// @optional
StateProperty<float_t> scale;

View File

@ -13,8 +13,8 @@ SimpleRenderTargetQuad::SimpleRenderTargetQuad(SceneItem *i) :
{
}
std::vector<SceneItemComponent*> SimpleRenderTargetQuad::getDependencies() {
return std::vector<SceneItemComponent*>{
std::vector<std::shared_ptr<SceneItemComponent>> SimpleRenderTargetQuad::getDependencies() {
return {
(this->meshHost = this->item->getComponent<MeshHost>())
};
}

View File

@ -11,7 +11,7 @@
namespace Dawn {
class SimpleRenderTargetQuad : public SceneItemComponent {
protected:
MeshHost *meshHost = nullptr;
std::shared_ptr<MeshHost> meshHost;
std::function<void()> evtResized;
public:
@ -27,7 +27,7 @@ namespace Dawn {
*/
SimpleRenderTargetQuad(SceneItem *item);
std::vector<SceneItemComponent*> getDependencies() override;
std::vector<std::shared_ptr<SceneItemComponent>> getDependencies() override;
void onStart() override;
};
}

View File

@ -20,14 +20,12 @@ TiledSprite::TiledSprite(SceneItem *item) :
}
std::vector<SceneItemComponent*> TiledSprite::getDependencies() {
std::vector<std::shared_ptr<SceneItemComponent>> TiledSprite::getDependencies() {
if(this->meshHost == nullptr) {
this->meshHost = this->item->getComponent<QuadMeshHost>();
}
return {
this->meshHost
};
return { this->meshHost._realValue };
}
void TiledSprite::onStart() {

View File

@ -22,7 +22,7 @@ namespace Dawn {
// @optional
StateProperty<Tileset*> tileset;
// @optional
StateProperty<QuadMeshHost*> meshHost;
StateProperty<std::shared_ptr<QuadMeshHost>> meshHost;
// @optional
StateProperty<int32_t> tile;
// @optional
@ -33,7 +33,7 @@ namespace Dawn {
StateProperty<float_t> size;
TiledSprite(SceneItem *item);
std::vector<SceneItemComponent*> getDependencies();
std::vector<std::shared_ptr<SceneItemComponent>> getDependencies();
void onStart() override;
};
}

View File

@ -12,8 +12,8 @@ using namespace Dawn;
MeshRenderer::MeshRenderer(SceneItem *item) : SceneItemComponent(item) {
}
std::vector<SceneItemComponent*> MeshRenderer::getDependencies() {
return std::vector<SceneItemComponent*>{
std::vector<std::shared_ptr<SceneItemComponent>> MeshRenderer::getDependencies() {
return {
this->meshHost = this->item->getComponent<MeshHost>()
};
}

View File

@ -12,7 +12,7 @@ namespace Dawn {
class MeshRenderer : public SceneItemComponent {
protected:
MeshHost *meshHost = nullptr;
std::shared_ptr<MeshHost> meshHost;
public:
// @optional
@ -25,7 +25,7 @@ namespace Dawn {
*/
MeshRenderer(SceneItem *item);
std::vector<SceneItemComponent*> getDependencies() override;
std::vector<std::shared_ptr<SceneItemComponent>> getDependencies() override;
void onStart() override;
};
}

View File

@ -9,7 +9,7 @@
namespace Dawn {
struct CharacterController2DCollisionEventInfo {
SolidController2D *collider;
std::shared_ptr<SolidController2D> collider;
glm::vec2 normal;
float_t entryTime;
float_t exitTime;

View File

@ -10,7 +10,7 @@ using namespace Dawn;
SolidController2D::SolidController2D(SceneItem *item) : SceneItemComponent(item) {
}
std::vector<SceneItemComponent*> SolidController2D::getDependencies() {
std::vector<std::shared_ptr<SceneItemComponent>> SolidController2D::getDependencies() {
return {
(this->collider = item->getComponent<Collider2D>())
};
@ -18,7 +18,7 @@ std::vector<SceneItemComponent*> SolidController2D::getDependencies() {
bool_t SolidController2D::getCollidingResult(
glm::vec2 movement,
Collider2D *movingObject,
std::shared_ptr<Collider2D> movingObject,
glm::vec2 &normal,
float_t &entryTime,
float_t &exitTime,
@ -34,13 +34,13 @@ bool_t SolidController2D::getCollidingResult(
// Check what the moving object is
switch(movingObject->getColliderType()) {
case COLLIDER2D_TYPE_BOX: {
auto box1 = dynamic_cast<BoxCollider*>(movingObject);
auto box1 = std::static_pointer_cast<BoxCollider>(movingObject);
assertNotNull(box1, "SolidController2D::getCollidingResult: Moving object is not a BoxCollider");
// Box VS (this)?
switch(this->collider->getColliderType()) {
case COLLIDER2D_TYPE_BOX: {
auto box2 = dynamic_cast<BoxCollider*>(this->collider);
auto box2 = std::static_pointer_cast<BoxCollider>(this->collider);
assertNotNull(box2, "SolidController2D::getCollidingResult: Collider is not a BoxCollider");
auto otherPos = physics3Dto2D(box2->transform->getWorldPosition());

View File

@ -10,10 +10,10 @@
namespace Dawn {
class SolidController2D : public SceneItemComponent {
public:
Collider2D *collider = nullptr;
std::shared_ptr<Collider2D> collider;
SolidController2D(SceneItem *item);
std::vector<SceneItemComponent*> getDependencies() override;
std::vector<std::shared_ptr<SceneItemComponent>> getDependencies() override;
/**
* Gets the result of checking if two collider bodies are intersecting.
@ -31,7 +31,7 @@ namespace Dawn {
*/
bool_t getCollidingResult(
glm::vec2 movement,
Collider2D *movingObject,
std::shared_ptr<Collider2D> movingObject,
glm::vec2 &normal,
float_t &entryTime,
float_t &exitTime,

View File

@ -11,25 +11,25 @@ TriggerController2D::TriggerController2D(SceneItem *i) : SceneItemComponent(i) {
}
std::vector<SceneItemComponent*> TriggerController2D::getDependencies() {
std::vector<std::shared_ptr<SceneItemComponent>> TriggerController2D::getDependencies() {
return {
(this->collider = this->item->getComponent<Collider2D>())
};
}
bool_t TriggerController2D::getCollidingResult(Collider2D* movingObject) {
bool_t TriggerController2D::getCollidingResult(std::shared_ptr<Collider2D> movingObject) {
assertNotNull(this->collider, "TriggerController2D::getCollidingResult: Collider cannot be null");
assertNotNull(movingObject, "TriggerController2D::getCollidingResult: Moving object cannot be null");
switch(movingObject->getColliderType()) {
case COLLIDER2D_TYPE_BOX: {
auto box1 = dynamic_cast<BoxCollider*>(movingObject);
auto box1 = std::static_pointer_cast<BoxCollider>(movingObject);
assertNotNull(box1, "TriggerController2D::getCollidingResult: Moving object is not a BoxCollider");
// Box VS ?
switch(collider->getColliderType()) {
case COLLIDER2D_TYPE_BOX: {
auto box2 = dynamic_cast<BoxCollider*>(collider);
auto box2 = std::static_pointer_cast<BoxCollider>(collider);
assertNotNull(box2, "TriggerController2D::getCollidingResult: Collider is not a BoxCollider");
return boxIsBoxColliding(
physics3Dto2D(box1->transform->getWorldPosition()), box1->min, box1->max,

View File

@ -12,16 +12,16 @@ namespace Dawn {
class TriggerController2D : public SceneItemComponent {
public:
Collider2D *collider = nullptr;
std::shared_ptr<Collider2D> collider;
StateEvent<CharacterController2D*> eventTriggerEnter;
TriggerController2D(SceneItem *i);
std::vector<SceneItemComponent*> getDependencies() override;
std::vector<std::shared_ptr<SceneItemComponent>> getDependencies() override;
/**
* Returns whether or not the given moving object is colliding with this
* trigger collider.
*/
bool_t getCollidingResult(Collider2D* movingObject);
bool_t getCollidingResult(std::shared_ptr<Collider2D> movingObject);
};
}

View File

@ -9,7 +9,7 @@
using namespace Dawn;
UICanvas * UICanvas::create(Scene *scene) {
std::shared_ptr<UICanvas> UICanvas::create(Scene *scene) {
auto item = scene->createSceneItem();
return item->addComponent<UICanvas>();
}

View File

@ -83,10 +83,10 @@ namespace Dawn {
* @param scene Scene to create the UI Canvas for.
* @return Created UI Canvas.
*/
static UICanvas * create(Scene *scene);
static std::shared_ptr<UICanvas> create(Scene *scene);
//======================================================================//
StateProperty<Camera*> camera;
StateProperty<std::shared_ptr<Camera>> camera;
enum UIDrawType drawType = UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE;
/**

View File

@ -21,7 +21,7 @@ UIComponent::UIComponent(SceneItem *item) :
}
UIComponentDimensional * UIComponent::getParentDimensional() {
std::shared_ptr<UIComponentDimensional> UIComponent::getParentDimensional() {
auto parent = this->transform->getParent();
if(parent == nullptr) return nullptr;
auto dimensional = parent->item->getComponent<UIComponentDimensional>();
@ -238,7 +238,7 @@ void UIComponent::calculateDimensions(
}
}
UICanvas * UIComponent::getCanvas() {
std::shared_ptr<UICanvas> UIComponent::getCanvas() {
// TODO: Cache this on first hit.
auto parent = this->transform->getParent();
while(parent != nullptr) {

View File

@ -38,7 +38,7 @@ namespace Dawn {
*
* @return Pointer to the parent dimensional.
*/
UIComponentDimensional * getParentDimensional();
std::shared_ptr<UIComponentDimensional> getParentDimensional();
/**
* Internal method to update the alignment of this item.
@ -130,7 +130,7 @@ namespace Dawn {
*
* @return Pointer to the UI Canvas this component is a child of.
*/
virtual UICanvas * getCanvas();
virtual std::shared_ptr<UICanvas> getCanvas();
float_t getWidth() override;
float_t getHeight() override;

View File

@ -14,7 +14,7 @@ UISimpleMenu::UISimpleMenu(SceneItem *item) :
}
std::vector<SceneItemComponent*> UISimpleMenu::getDependencies() {
std::vector<std::shared_ptr<SceneItemComponent>> UISimpleMenu::getDependencies() {
return {
(this->menu = this->item->getComponent<UIMenuController>()),
(this->canvas == nullptr ? (this->canvas = this->item->getComponent<UICanvas>()) : nullptr)

View File

@ -10,14 +10,14 @@
namespace Dawn {
class UISimpleMenu : public SceneItemComponent {
protected:
UIMenuController *menu = nullptr;
UICanvas *canvas = nullptr;
UISimpleMenuItem *currentlySelected = nullptr;
std::vector<UISimpleMenuItem*> menuItems;
std::shared_ptr<UIMenuController> menu;
std::shared_ptr<UICanvas> canvas;
std::shared_ptr<UISimpleMenuItem> currentlySelected;
std::vector<std::shared_ptr<UISimpleMenuItem>> menuItems;
public:
UISimpleMenu(SceneItem *item);
std::vector<SceneItemComponent*> getDependencies() override;
std::vector<std::shared_ptr<SceneItemComponent>> getDependencies() override;
void onStart() override;
};
}

View File

@ -11,7 +11,7 @@ UISimpleMenuItem::UISimpleMenuItem(SceneItem *item) : SceneItemComponent(item) {
}
UIComponent * UISimpleMenuItem::getComponentForHighlighting() {
std::shared_ptr<UIComponent> UISimpleMenuItem::getComponentForHighlighting() {
if(uiComponent == nullptr) uiComponent = item->getComponent<UIComponent>();
return uiComponent;
}

View File

@ -15,7 +15,7 @@ namespace Dawn {
// May make these into either state events or make a new event.
int32_t menuX = 0;
int32_t menuY = 0;
UIComponent *uiComponent = nullptr;
std::shared_ptr<UIComponent> uiComponent = nullptr;
UISimpleMenuItem(SceneItem *item);
@ -25,6 +25,6 @@ namespace Dawn {
*
* @return UI Component to use for highlighting / hovering.
*/
virtual UIComponent * getComponentForHighlighting();
virtual std::shared_ptr<UIComponent> getComponentForHighlighting();
};
}

View File

@ -11,9 +11,9 @@
namespace Dawn {
class HelloWorldScene : public Scene {
protected:
Camera *camera;
std::shared_ptr<Camera> camera;
SimpleSpinningCubePrefab *cube;
UICanvas *canvas;
std::shared_ptr<UICanvas> canvas;
void stage() override {
camera = Camera::create(this);