start implementing ECS

This commit is contained in:
Bailey Harrison 2022-12-14 22:50:17 +00:00
parent a278b7d035
commit 7920d1199e
8 changed files with 14 additions and 242 deletions

View File

@ -20,7 +20,6 @@ set(SRC_FILES
"src/gfx_device_vulkan.cpp"
"src/scene.cpp"
"src/object.cpp"
"src/util/files.cpp"
)
@ -46,7 +45,6 @@ set(INCLUDE_FILES
"include/gfx_device.hpp"
"include/scene.hpp"
"include/object.hpp"
"include/util/files.hpp"
)

View File

@ -1,112 +0,0 @@
#pragma once
#include "engine_api.h"
#include "log.hpp"
#include <glm/mat4x4.hpp>
#include <glm/gtc/quaternion.hpp>
#include <list>
#include <vector>
#include <string>
#include <memory>
namespace engine {
/* forward declarations */
class Scene;
class Component {
};
struct Transform {
// Scale, rotate (XYZ), translate
glm::vec3 position{ 0.0f };
glm::quat rotation{};
glm::vec3 scale{ 1.0f };
};
class Object {
public:
Object(const std::string& name, Object* parent, Scene* scene);
Object(const Object&) = delete;
Object& operator=(const Object&) = delete;
~Object();
/* methods */
Object* getChild(const std::string& name);
std::vector<Object*> getChildren();
Object* createChild(const std::string& name);
bool deleteChild(const std::string& name);
void printTree(int level = 0);
// Returns the component of type T
// Returns nullptr if the component is not found.
template<class T> T* getComponent();
template<class T> T* createComponent();
template<class T> bool deleteComponent();
/* public member variables */
const std::string name;
Object* const parent;
Scene* const scene;
Transform transform;
private:
static int s_next_id;
int m_id = s_next_id;
// If nullptr, this is the root object
std::list<std::unique_ptr<Object>> m_children{};
std::list<std::unique_ptr<Component>> m_components{};
};
/* implementation of template functions */
template<class T> T* Object::getComponent()
{
for (const auto& component : m_components) {
T* derived = dynamic_cast<T*>(component.get());
if (derived != nullptr) {
return derived;
}
}
return nullptr;
}
template <class T> T* Object::createComponent()
{
if (std::is_base_of<Component, T>::value == false) {
ERROR("Object::createComponent(): attempt to create a component with a non-component class");
return nullptr;
}
if (getComponent<T>() != nullptr) {
ERROR("Object::createComponent(): attempt to create a component that already exists on an object");
return nullptr;
}
m_components.emplace_back(std::make_unique<T>(this));
return dynamic_cast<T*>(m_components.back().get());
}
template<class T> bool Object::deleteComponent()
{
for (auto itr = m_components.begin(); itr != m_components.end(); ++itr) {
if (dynamic_cast<T*>((*itr).get()) != nullptr) {
m_components.erase(itr);
return true;
}
}
return false;
}
}

View File

@ -1,10 +1,10 @@
#pragma once
#include "object.hpp"
#include <cstdint>
namespace engine {
class Scene : public Object {
class Scene {
public:
Scene();
@ -12,7 +12,13 @@ namespace engine {
Scene& operator=(const Scene&) = delete;
~Scene();
uint32_t createEntity()
{
return m_nextEntityID++;
}
private:
uint32_t m_nextEntityID = 1000;
};

2
run.sh
View File

@ -1,3 +1,3 @@
#!/bin/sh
cd "Debug/test"
cd "Release/test"
./enginetest

View File

@ -321,7 +321,7 @@ namespace engine {
ERROR
};
constexpr MessageSeverity MESSAGE_LEVEL = MessageSeverity::INFO;
constexpr MessageSeverity MESSAGE_LEVEL = MessageSeverity::WARNING;
switch (MESSAGE_LEVEL) {
case MessageSeverity::VERBOSE:
debugMessengerInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;

View File

@ -1,118 +0,0 @@
#include "object.hpp"
#include "log.hpp"
namespace engine {
int Object::s_next_id = 1000;
Object::Object(const std::string& name, Object* parent, Scene* scene)
: name(name), parent(parent), scene(scene)
{
s_next_id++;
}
Object::~Object() {}
Object* Object::getChild(const std::string& name)
{
for (const auto& child : m_children) {
if (name == child->name) {
return child.get();
}
}
return nullptr;
}
std::vector<Object*> Object::getChildren()
{
std::vector<Object*> newVector{};
for (const auto& child : m_children) {
newVector.push_back(child.get());
}
return newVector;
}
Object* Object::createChild(const std::string& name)
{
if (getChild(name) != nullptr) {
ERROR("Attempt to create child object with existing name");
return nullptr;
}
m_children.emplace_back(std::make_unique<Object>(name, this, scene));
return m_children.back().get();
}
bool Object::deleteChild(const std::string& name)
{
for (auto itr = m_children.begin(); itr != m_children.end(); ++itr) {
if ((*itr)->name == name) {
m_children.erase(itr);
return true;
}
}
return false;
}
void Object::printTree(int level)
{
std::string buf;
for (int i = 0; i < level; i++) {
if (i + 1 == level) {
buf += "\\_______";
}
else {
buf += " ";
}
}
buf += name;
INFO("{}", buf);
for (const auto& child : this->getChildren()) {
child->printTree(level + 1);
}
}
/*
void Object::getAllSubComponents(struct CompList& compList, glm::mat4 parentTransform)
{
glm::mat4 objTransform{ 1.0f };
auto t = transform;
// rotation
objTransform = glm::mat4_cast(t.rotation);
// position
reinterpret_cast<glm::vec3&>(objTransform[3]) = t.position;
// scale (effectively applied first
objTransform = glm::scale(objTransform, t.scale);
glm::mat4 newTransform = parentTransform * objTransform;
for (const auto& compUnq : m_components) {
const auto comp = compUnq.get();
switch (comp->getType()) {
case Component::TypeEnum::CAMERA:
compList.cameras.emplace_back(dynamic_cast<Camera*>(comp), newTransform);
break;
case Component::TypeEnum::RENDERER:
compList.renderers.emplace_back(dynamic_cast<Renderer*>(comp), newTransform);
break;
case Component::TypeEnum::UI:
compList.uis.emplace_back(dynamic_cast<UI*>(comp), newTransform);
break;
case Component::TypeEnum::CUSTOM:
compList.customs.emplace_back(dynamic_cast<CustomComponent*>(comp), newTransform);
break;
default:
break;
}
}
for (const auto& child : m_children) {
child->getAllSubComponents(compList, newTransform);
}
}
*/
}

View File

@ -3,7 +3,6 @@
namespace engine {
Scene::Scene()
: Object("root", nullptr, this)
{
}

View File

@ -23,7 +23,6 @@ namespace engine {
void SceneManager::updateActiveScene()
{
if (m_activeSceneIndex >= 0) {
INFO("updating scene: {}", m_scenes[m_activeSceneIndex]->name);
}
}