Worked a bit on prefabs of all things

This commit is contained in:
2024-09-10 18:26:14 -05:00
parent e5f3f69120
commit 0e5b85633c
23 changed files with 286 additions and 19 deletions

View File

@ -0,0 +1,14 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/Scene.hpp"
namespace Dawn {
struct Prefab {
public:
std::shared_ptr<SceneItem> item;
};
}

View File

@ -4,32 +4,32 @@
// https://opensource.org/licenses/MIT
#include "SimpleSpinningCube.hpp"
#include "component/display/MeshRenderer.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp"
#include "display/mesh/CubeMesh.hpp"
#include "component/SimpleComponent.hpp"
using namespace Dawn;
std::shared_ptr<SceneItem> Dawn::createSimpleSpinningCube(Scene &s) {
struct SimpleSpinningCube Dawn::createSimpleSpinningCube(Scene &s) {
struct SimpleSpinningCube cube;
// Create the scene item.
auto cubeItem = s.createSceneItem();
cube.item = s.createSceneItem();
// Create a simple cube mesh.
auto cubeMesh = std::make_shared<Mesh>();
cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0);
cube.mesh = std::make_shared<Mesh>();
cube.mesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(cube.mesh, glm::vec3(-1, -1, -1), glm::vec3(2, 2, 2), 0, 0);
// Add a renderer to the scene item.
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
cubeMeshRenderer->mesh = cubeMesh;
cube.meshRenderer = cube.item->addComponent<MeshRenderer>();
cube.meshRenderer->mesh = cube.mesh;
// Add a material to the scene item.
auto cubeMaterial = cubeItem->addComponent<SimpleTexturedMaterial>();
cubeMaterial->setColor(COLOR_MAGENTA);
cube.material = cube.item->addComponent<SimpleTexturedMaterial>();
cube.material->setColor(COLOR_MAGENTA);
// Add a simple event listener component to the scene item.
addSimpleComponent(cubeItem, [](auto &cmp, auto &events) {
addSimpleComponent(cube.item, [](auto &cmp, auto &events) {
// Note that add component cannot receive a self reference, so we must
// capture the component by reference instead.
events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&](
@ -45,5 +45,5 @@ std::shared_ptr<SceneItem> Dawn::createSimpleSpinningCube(Scene &s) {
}));
});
return cubeItem;
return cube;
}

View File

@ -4,8 +4,23 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/Scene.hpp"
#include "prefab/Prefab.hpp"
#include "component/display/MeshRenderer.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp"
namespace Dawn {
std::shared_ptr<SceneItem> createSimpleSpinningCube(Scene &s);
struct SimpleSpinningCube : public Prefab {
public:
std::shared_ptr<Mesh> mesh;
std::shared_ptr<MeshRenderer> meshRenderer;
std::shared_ptr<SimpleTexturedMaterial> material;
};
/**
* Creates a simple spinning cube prefab on the scene.
*
* @param s Scene to add the simple spinning cube to.
* @return The simple spinning cube prefab.
*/
struct SimpleSpinningCube createSimpleSpinningCube(Scene &s);
}

View File

@ -44,17 +44,23 @@ void SceneComponent::dispose() {
}
std::shared_ptr<SceneItem> SceneComponent::getItem() {
return this->item.lock();
auto item = this->item.lock();
assertNotNull(item, "Item cannot be null?");
return item;
}
std::shared_ptr<Scene> SceneComponent::getScene() {
auto item = this->getItem();
return item->getScene();
auto scene = item->getScene();
assertNotNull(scene, "Scene cannot be null?");
return scene;
}
std::shared_ptr<Game> SceneComponent::getGame() {
auto scene = this->getScene();
return scene->getGame();
auto game = scene->getGame();
assertNotNull(game, "Game cannot be null?");
return game;
}
SceneComponent::~SceneComponent() {

View File

@ -13,6 +13,7 @@ namespace Dawn {
class Game;
class Scene;
class SceneItem;
class SceneItemComponents;
class SceneComponent : std::enable_shared_from_this<SceneComponent> {
private:
@ -70,5 +71,8 @@ namespace Dawn {
* Disposes this scene component.
*/
virtual ~SceneComponent();
friend class SceneItem;
friend class SceneItemComponents;
};
}

View File

@ -43,6 +43,7 @@ namespace Dawn {
this->components.push_back(
static_pointer_cast<SceneComponent>(component)
);
component->item = this->sceneItemComponentsSelf();
return component;
}