diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index bd0c0d0..7d01b6c 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -5,9 +5,8 @@ set(SOURCES src/IO/file_manager.cpp src/renderer/debug.cpp - src/window/window.cpp - src/scene/scene.cpp + src/window/window.cpp src/components/batch.cpp src/renderer/mesh.cpp diff --git a/engine/include/engine/scene/scene.h b/engine/include/engine/scene/scene.h index f03421f..93b8cea 100644 --- a/engine/include/engine/scene/scene.h +++ b/engine/include/engine/scene/scene.h @@ -1,18 +1,30 @@ -#ifndef ENGINE_SCENE_H_ -#define ENGINE_SCENE_H_ +#pragma once #include #include namespace Engine { -class Scene; +class Entity; + +class Scene { +private: + friend class Entity; +public: + Scene() = default; + + Entity CreateEntity(); +private: + entt::registry m_registry; + friend class Renderer; +}; class Entity { - friend class Scene; -private: - Entity(entt::entity entity, Scene* scene) : m_entity(entity), m_scene(scene) {} public: + Entity() = default; + Entity(entt::entity entity, Scene* scene) : m_entity(entity), m_scene(scene) {} + Entity(const Entity& other) = default; + template inline auto AddComponent(Args &&...args) { assert(this->m_scene != nullptr && "Scene has not been assigned to the entity"); @@ -25,22 +37,8 @@ public: return m_scene->m_registry.get(m_entity); } private: - entt::entity m_entity; - Scene *m_scene; -}; - -class Scene { -private: - friend class Entity; -public: - Scene(); - - std::unique_ptr CreateEntity(); -private: - entt::registry m_registry; - friend class Renderer; + entt::entity m_entity { 0 }; + Scene *m_scene = nullptr; }; } // namespace Engine - -#endif // ENGINE_SCENE_H_ \ No newline at end of file diff --git a/engine/src/scene/scene.cpp b/engine/src/scene/scene.cpp index fa7b3a2..e3fc563 100644 --- a/engine/src/scene/scene.cpp +++ b/engine/src/scene/scene.cpp @@ -2,10 +2,8 @@ namespace Engine { -Scene::Scene() = default; - -std::unique_ptr Scene::CreateEntity() { - return std::unique_ptr(new Entity(m_registry.create(), this)); +Entity Scene::CreateEntity() { + return { m_registry.create(), this }; } -} // namespace Engine +} diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index 22d1c53..9ddb0a3 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -35,19 +35,19 @@ public: Object* lightObj = Object::LoadFile("./assets/common/sphere/sphere.obj"); lightEntity = scene->CreateEntity(); - lightEntity->AddComponent(glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f)); - lightEntity->AddComponent(light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f); - lightEntity->AddComponent(std::shared_ptr(lightObj)); + lightEntity.AddComponent(glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f)); + lightEntity.AddComponent(light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f); + lightEntity.AddComponent(std::shared_ptr(lightObj)); cameraEntity = scene->CreateEntity(); - cameraEntity->AddComponent(); - cameraEntity->AddComponent(glm::vec3(0.f, 2.f, 2.f)); + cameraEntity.AddComponent(); + cameraEntity.AddComponent(glm::vec3(0.f, 2.f, 2.f)); Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj"); modelEntity = scene->CreateEntity(); - modelEntity->AddComponent(glm::vec3(0.f, 0.0f, 0.f)); - modelEntity->AddComponent(std::shared_ptr(targetObj)); - modelEntity->AddComponent(); + modelEntity.AddComponent(glm::vec3(0.f, 0.0f, 0.f)); + modelEntity.AddComponent(std::shared_ptr(targetObj)); + modelEntity.AddComponent(); // Object* grass = Object::LoadFile("./assets/common/cube/cube.obj"); // const auto cubeEntity = scene->m_registry.create(); @@ -56,27 +56,27 @@ public: // Cube template (use shared object to avoid reloading 1000 times) std::shared_ptr cubeObj = std::shared_ptr(Object::LoadFile("./assets/grass_block/grass_block.obj")); - const auto batchEntt = scene->CreateEntity(); - auto cubeBatch = batchEntt->AddComponent(); - batchEntt->AddComponent(cubeObj); + auto batchEntt = scene->CreateEntity(); + auto cubeBatch = batchEntt.AddComponent(); + batchEntt.AddComponent(cubeObj); // auto cubeBatch = scene->m_registry.get(batchEntt); // Generate 1000 random cubes for (int i = 0; i < 1000; ++i) { - const auto cubeEntity = scene->CreateEntity(); + auto cubeEntity = scene->CreateEntity(); float x = static_cast(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100] float y = static_cast(rand()) / RAND_MAX * 10.f; // range [0, 10] float z = static_cast(rand()) / RAND_MAX * 200.f - 100.f; // range [-100, 100] - cubeEntity->AddComponent(glm::vec3(x, y, z)); - cubeEntity->AddComponent(); - cubeEntity->AddComponent(cubeBatch.id()); + cubeEntity.AddComponent(glm::vec3(x, y, z)); + cubeEntity.AddComponent(); + cubeEntity.AddComponent(cubeBatch.id()); } Object* floorObj = Object::LoadFile("./assets/common/plane/plane.obj"); - const auto floorEntt = scene->CreateEntity(); - floorEntt->AddComponent(glm::vec3(0.f)); - floorEntt->AddComponent(std::shared_ptr(floorObj)); + auto floorEntt = scene->CreateEntity(); + floorEntt.AddComponent(glm::vec3(0.f)); + floorEntt.AddComponent(std::shared_ptr(floorObj)); std::cout << "Game initialized" << std::endl; @@ -134,7 +134,7 @@ public: if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f; if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f; - auto camTransform = cameraEntity->GetComponent(); + auto camTransform = cameraEntity.GetComponent(); camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f camTransform.rotation = cameraViewDirection; @@ -169,8 +169,8 @@ public: glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor); // Update the directional light in the registry - auto l = lightEntity->GetComponent(); - auto t = lightEntity->GetComponent(); + auto l = lightEntity.GetComponent(); + auto t = lightEntity.GetComponent(); if (l.type == light::LightType::DIRECTIONAL) { // "position" for directional light often stores direction vector // If your system instead uses transform.rotation, adjust accordingly @@ -211,9 +211,9 @@ public: private: std::shared_ptr m_scene; - std::unique_ptr lightEntity; - std::unique_ptr cameraEntity; - std::unique_ptr modelEntity; + Entity lightEntity; + Entity cameraEntity; + Entity modelEntity; float m_angle; Uint64 m_lastTicks;