From ea593feb8d550abdd137104f073fb1252dc59eb6 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 22 Oct 2025 14:25:02 +0200 Subject: [PATCH] feat: refactoring --- engine/include/engine/IO/file_manager.h | 4 + engine/include/engine/IO/parser.h | 4 + engine/include/engine/api.h | 6 +- engine/include/engine/app/app.h | 20 ++-- engine/include/engine/components/batch.h | 2 + engine/include/engine/components/camera.h | 2 + engine/include/engine/components/light.h | 2 + engine/include/engine/components/mesh.h | 2 + engine/include/engine/components/rotate.h | 2 + engine/include/engine/components/transform.h | 2 + engine/include/engine/renderer/basics.h | 4 + engine/include/engine/renderer/core.h | 3 + engine/include/engine/renderer/debug.h | 4 + engine/include/engine/renderer/material.h | 4 + engine/include/engine/renderer/mesh.h | 4 + engine/include/engine/renderer/renderer.h | 4 + engine/include/engine/renderer/shader.h | 4 + engine/include/engine/renderer/texture.h | 4 + engine/include/engine/renderer/wavefront.h | 4 + engine/include/engine/scene/scene.h | 33 ++++++- engine/include/engine/window/event.hpp | 4 + .../engine/window/events/window_events.h | 4 + engine/include/engine/window/window.h | 4 + engine/src/IO/file_manager.cpp | 6 +- engine/src/IO/parser.cpp | 6 +- engine/src/components/batch.cpp | 6 +- engine/src/renderer/core.cpp | 3 + engine/src/renderer/debug.cpp | 4 + engine/src/renderer/mesh.cpp | 6 +- engine/src/renderer/renderer.cpp | 6 +- engine/src/renderer/shader.cpp | 6 +- engine/src/renderer/texture.cpp | 4 + engine/src/renderer/wavefront.cpp | 4 +- engine/src/scene/scene.cpp | 9 +- engine/src/window/window.cpp | 6 +- sandbox/src/main.cpp | 99 ++++++++++--------- 36 files changed, 220 insertions(+), 71 deletions(-) diff --git a/engine/include/engine/IO/file_manager.h b/engine/include/engine/IO/file_manager.h index b78a83c..2cee5af 100644 --- a/engine/include/engine/IO/file_manager.h +++ b/engine/include/engine/IO/file_manager.h @@ -3,6 +3,8 @@ #include +namespace Engine { + class FileManager { public: @@ -12,4 +14,6 @@ public: static std::string read(const std::string &filename); }; +} + #endif // FILE_MANAGER_H \ No newline at end of file diff --git a/engine/include/engine/IO/parser.h b/engine/include/engine/IO/parser.h index 60dc1d4..88ab511 100644 --- a/engine/include/engine/IO/parser.h +++ b/engine/include/engine/IO/parser.h @@ -1,6 +1,8 @@ #ifndef PARSER_H_ #define PARSER_H_ +namespace Engine { + // Very fast OBJ/MTL line parser class Parser { private: @@ -17,4 +19,6 @@ public: int TakeIndex(int baseCount); }; +} + #endif // PARSER_H_ \ No newline at end of file diff --git a/engine/include/engine/api.h b/engine/include/engine/api.h index 7c5c372..c86fc8a 100644 --- a/engine/include/engine/api.h +++ b/engine/include/engine/api.h @@ -9,10 +9,10 @@ #include "engine/app/app.h" #include "engine/renderer/core.h" -extern IApplication* CreateApplication(); +extern Engine::IApplication* CreateApplication(); int main() { - auto engine = Engine::GetInstance(); - engine->Run(std::unique_ptr(CreateApplication())); + auto engine = Engine::Engine::GetInstance(); + engine->Run(std::unique_ptr(CreateApplication())); return 0; } \ No newline at end of file diff --git a/engine/include/engine/app/app.h b/engine/include/engine/app/app.h index 90fb007..1489740 100644 --- a/engine/include/engine/app/app.h +++ b/engine/include/engine/app/app.h @@ -5,15 +5,17 @@ #include "engine/window/event.hpp" #include "engine/export.h" -class ENGINE_API IApplication { -public: - virtual ~IApplication() = default; +namespace Engine { + class ENGINE_API IApplication { + public: + virtual ~IApplication() = default; - virtual void OnInit(std::shared_ptr scene) {}; - virtual void OnUpdate() {}; - virtual void OnShutdown() {}; - - virtual void OnEvent(const Event& event) {}; -}; + virtual void OnInit(std::shared_ptr scene) {}; + virtual void OnUpdate() {}; + virtual void OnShutdown() {}; + + virtual void OnEvent(const Event& event) {}; + }; +} #endif // APPLICATION_H_ \ No newline at end of file diff --git a/engine/include/engine/components/batch.h b/engine/include/engine/components/batch.h index eea8305..e41d85f 100644 --- a/engine/include/engine/components/batch.h +++ b/engine/include/engine/components/batch.h @@ -4,6 +4,7 @@ #include #include "engine/export.h" +namespace Engine { // requires mesh component struct ENGINE_API batch { friend class Renderer; @@ -25,5 +26,6 @@ private: private: void prepare(glm::mat4 *instances, unsigned int count); }; +} #endif // COMPONENT_BATCH_H_ \ No newline at end of file diff --git a/engine/include/engine/components/camera.h b/engine/include/engine/components/camera.h index 1dcf7aa..5ab8029 100644 --- a/engine/include/engine/components/camera.h +++ b/engine/include/engine/components/camera.h @@ -3,6 +3,8 @@ #include "engine/export.h" +namespace Engine { struct ENGINE_API camera {}; +} #endif // COMPONENTS_PLAYER_H_ \ No newline at end of file diff --git a/engine/include/engine/components/light.h b/engine/include/engine/components/light.h index e396cb6..4192a7b 100644 --- a/engine/include/engine/components/light.h +++ b/engine/include/engine/components/light.h @@ -6,6 +6,7 @@ #include "engine/renderer/renderer.h" #include "engine/export.h" +namespace Engine { struct ENGINE_API light { friend class Renderer; public: @@ -25,5 +26,6 @@ private: glm::mat4 lightSpace; int shadowRes{1024}; }; +} #endif // COMPONENTS_LIGHT_H_ \ No newline at end of file diff --git a/engine/include/engine/components/mesh.h b/engine/include/engine/components/mesh.h index ba40534..e47b08a 100644 --- a/engine/include/engine/components/mesh.h +++ b/engine/include/engine/components/mesh.h @@ -6,8 +6,10 @@ #include "engine/renderer/wavefront.h" #include "engine/export.h" +namespace Engine { struct ENGINE_API mesh { std::shared_ptr object; }; +} #endif // COMPONENTS_MESH_H_ \ No newline at end of file diff --git a/engine/include/engine/components/rotate.h b/engine/include/engine/components/rotate.h index 6057922..d72f46d 100644 --- a/engine/include/engine/components/rotate.h +++ b/engine/include/engine/components/rotate.h @@ -3,6 +3,8 @@ #include "engine/export.h" +namespace Engine { struct ENGINE_API rotate {}; +} #endif // COMPONENT_ROTATE_H_ \ No newline at end of file diff --git a/engine/include/engine/components/transform.h b/engine/include/engine/components/transform.h index a551e5d..8703af2 100644 --- a/engine/include/engine/components/transform.h +++ b/engine/include/engine/components/transform.h @@ -4,10 +4,12 @@ #include #include "engine/export.h" +namespace Engine { struct ENGINE_API transform { glm::vec3 position; glm::vec3 rotation; glm::vec3 scale; }; +} #endif // COMPONENTS_TRANSFORM_H_ \ No newline at end of file diff --git a/engine/include/engine/renderer/basics.h b/engine/include/engine/renderer/basics.h index 29e0820..05eb299 100644 --- a/engine/include/engine/renderer/basics.h +++ b/engine/include/engine/renderer/basics.h @@ -3,6 +3,8 @@ #include +namespace Engine { + class Vertex { friend class Mesh; private: @@ -14,4 +16,6 @@ public: : m_position(position), m_normal(normal), m_texCoord(texCoord) {} }; +} + #endif // RENDERER_BASICS_H \ No newline at end of file diff --git a/engine/include/engine/renderer/core.h b/engine/include/engine/renderer/core.h index 5871c63..33b21d6 100644 --- a/engine/include/engine/renderer/core.h +++ b/engine/include/engine/renderer/core.h @@ -13,6 +13,8 @@ #include "engine/app/app.h" #include "engine/export.h" +namespace Engine { + class ENGINE_API Engine : public EventHandler { public: static Engine* GetInstance(); @@ -31,5 +33,6 @@ private: bool m_running; }; +} #endif // ENGINE_H_ \ No newline at end of file diff --git a/engine/include/engine/renderer/debug.h b/engine/include/engine/renderer/debug.h index e82635c..487a2df 100644 --- a/engine/include/engine/renderer/debug.h +++ b/engine/include/engine/renderer/debug.h @@ -3,6 +3,8 @@ #include +namespace Engine { + void MessageCallback(GLenum source, GLenum type, GLuint id, @@ -11,4 +13,6 @@ void MessageCallback(GLenum source, const GLchar* message, const void* userParam); +} + #endif // RENDERER_DEBUG_ \ No newline at end of file diff --git a/engine/include/engine/renderer/material.h b/engine/include/engine/renderer/material.h index 2dda85e..8a82d4d 100644 --- a/engine/include/engine/renderer/material.h +++ b/engine/include/engine/renderer/material.h @@ -6,6 +6,8 @@ #include "engine/renderer/texture.h" +namespace Engine { + class Material { private: glm::vec3 m_ambient { 0.2f, 0.2f, 0.2f }; @@ -39,4 +41,6 @@ public: inline void SetIllumination(float illum) { m_illum = illum; } }; +} + #endif // MATERIAL_H_ \ No newline at end of file diff --git a/engine/include/engine/renderer/mesh.h b/engine/include/engine/renderer/mesh.h index 9c9a912..f2cdd1c 100644 --- a/engine/include/engine/renderer/mesh.h +++ b/engine/include/engine/renderer/mesh.h @@ -7,6 +7,8 @@ #include "engine/renderer/basics.h" +namespace Engine { + class Mesh { public: // TODO: abstract away unsigned int m_vao, m_vbo, m_ebo; @@ -24,4 +26,6 @@ public: void Render(unsigned int count); }; +} + #endif // MESH_H_ \ No newline at end of file diff --git a/engine/include/engine/renderer/renderer.h b/engine/include/engine/renderer/renderer.h index 89abeba..66aec99 100644 --- a/engine/include/engine/renderer/renderer.h +++ b/engine/include/engine/renderer/renderer.h @@ -8,6 +8,8 @@ #include "engine/export.h" #include "engine/components/light.h" +namespace Engine { + // TODO: make static or singleton class ENGINE_API Renderer { public: @@ -37,4 +39,6 @@ private: glm::mat4 m_view; }; +} + #endif // RENDERER_H_ \ No newline at end of file diff --git a/engine/include/engine/renderer/shader.h b/engine/include/engine/renderer/shader.h index 251ad9e..2ad3ad0 100644 --- a/engine/include/engine/renderer/shader.h +++ b/engine/include/engine/renderer/shader.h @@ -7,6 +7,8 @@ #include "engine/export.h" +namespace Engine { + class ENGINE_API Shader { public: @@ -45,4 +47,6 @@ private: void checkLinkingError(); }; +} + #endif // SHADER_H \ No newline at end of file diff --git a/engine/include/engine/renderer/texture.h b/engine/include/engine/renderer/texture.h index 75ddfaf..04a8dfb 100644 --- a/engine/include/engine/renderer/texture.h +++ b/engine/include/engine/renderer/texture.h @@ -3,6 +3,8 @@ #include #include +namespace Engine { + class Texture { public: Texture() : m_id(0) {} @@ -13,4 +15,6 @@ private: unsigned int m_id; }; +} + #endif // TEXTURE_H_ diff --git a/engine/include/engine/renderer/wavefront.h b/engine/include/engine/renderer/wavefront.h index 70dd22d..55e8a81 100644 --- a/engine/include/engine/renderer/wavefront.h +++ b/engine/include/engine/renderer/wavefront.h @@ -14,6 +14,8 @@ #include "engine/export.h" +namespace Engine { + enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN }; enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN }; @@ -53,4 +55,6 @@ private: std::unordered_map> m_materials; }; +} + #endif // MODEL_H_ \ No newline at end of file diff --git a/engine/include/engine/scene/scene.h b/engine/include/engine/scene/scene.h index 295398c..f03421f 100644 --- a/engine/include/engine/scene/scene.h +++ b/engine/include/engine/scene/scene.h @@ -2,14 +2,45 @@ #define ENGINE_SCENE_H_ #include +#include + +namespace Engine { + +class Scene; + +class Entity { + friend class Scene; +private: + Entity(entt::entity entity, Scene* scene) : m_entity(entity), m_scene(scene) {} +public: + template + inline auto AddComponent(Args &&...args) { + assert(this->m_scene != nullptr && "Scene has not been assigned to the entity"); + return m_scene->m_registry.emplace(m_entity, std::forward(args)...); + } + + template + [[nodiscard]] inline auto GetComponent() { + assert(this->m_scene != nullptr && "Scene has not been assigned to the entity"); + 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; - friend class Game; }; +} // namespace Engine + #endif // ENGINE_SCENE_H_ \ No newline at end of file diff --git a/engine/include/engine/window/event.hpp b/engine/include/engine/window/event.hpp index 599e4fa..f04561c 100644 --- a/engine/include/engine/window/event.hpp +++ b/engine/include/engine/window/event.hpp @@ -7,6 +7,8 @@ #include #include +namespace Engine { + enum class EventType { WINDOW_RESIZE, WINDOW_CLOSE, @@ -71,4 +73,6 @@ private: std::size_t m_next_id = 1; }; +} + #endif // EVENT_H_ \ No newline at end of file diff --git a/engine/include/engine/window/events/window_events.h b/engine/include/engine/window/events/window_events.h index 7391f20..6e8ca33 100644 --- a/engine/include/engine/window/events/window_events.h +++ b/engine/include/engine/window/events/window_events.h @@ -3,6 +3,8 @@ #include "engine/window/event.hpp" +namespace Engine { + class WindowEvent : public Event { public: WindowEvent() : Event(Event::EventCategory::WINDOW) {} @@ -27,4 +29,6 @@ public: WindowCloseEvent() {} }; +} + #endif // WINDOW_EVENTS_H_ \ No newline at end of file diff --git a/engine/include/engine/window/window.h b/engine/include/engine/window/window.h index 179d024..494aca3 100644 --- a/engine/include/engine/window/window.h +++ b/engine/include/engine/window/window.h @@ -13,6 +13,8 @@ #define DEFAULT_WIDTH 1024 #define DEFAULT_HEIGHT 768 +namespace Engine { + class Window : public EventEmitter { friend class Engine; private: @@ -48,4 +50,6 @@ private: int m_height; }; +} + #endif //WINDOW_H_ \ No newline at end of file diff --git a/engine/src/IO/file_manager.cpp b/engine/src/IO/file_manager.cpp index 6e7255b..468cf86 100644 --- a/engine/src/IO/file_manager.cpp +++ b/engine/src/IO/file_manager.cpp @@ -4,6 +4,8 @@ #include #include +namespace Engine { + FileManager::FileManager() { } @@ -30,4 +32,6 @@ std::string FileManager::read(const std::string &filename) } return fileStream.str(); -} \ No newline at end of file +} + +} diff --git a/engine/src/IO/parser.cpp b/engine/src/IO/parser.cpp index 4a02b83..174defd 100644 --- a/engine/src/IO/parser.cpp +++ b/engine/src/IO/parser.cpp @@ -4,6 +4,8 @@ #include "engine/IO/parser.h" +namespace Engine { + // Skip whitespace void Parser::SkipSpaces() { while (*m_sv == ' ' || *m_sv == '\t') ++m_sv; @@ -120,4 +122,6 @@ bool Parser::TakeFaceIndices(int &vi, int &ti, int &ni) { // At this point m_sv is either at whitespace, end, or next token char. // Do NOT mutate indices (leave them raw). Let NormalizeIndex handle conversion. return true; -} \ No newline at end of file +} + +} diff --git a/engine/src/components/batch.cpp b/engine/src/components/batch.cpp index 8952898..c651246 100644 --- a/engine/src/components/batch.cpp +++ b/engine/src/components/batch.cpp @@ -2,6 +2,8 @@ #include "engine/components/batch.h" +namespace Engine { + unsigned int batch::LastID = 0; batch::batch() { @@ -27,4 +29,6 @@ void batch::prepare(glm::mat4 *instances, unsigned int count) { // Just update the data region — much cheaper glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::mat4) * count, instances); glBindBuffer(GL_ARRAY_BUFFER, 0); -} \ No newline at end of file +} + +} diff --git a/engine/src/renderer/core.cpp b/engine/src/renderer/core.cpp index 8be0b26..974154a 100644 --- a/engine/src/renderer/core.cpp +++ b/engine/src/renderer/core.cpp @@ -5,6 +5,8 @@ #include "engine/window/event.hpp" #include "engine/renderer/wavefront.h" +namespace Engine { + Engine* Engine::s_instance = nullptr; void Engine::Run(std::unique_ptr app) { @@ -66,3 +68,4 @@ Engine* Engine::GetInstance() { return s_instance; } +} diff --git a/engine/src/renderer/debug.cpp b/engine/src/renderer/debug.cpp index c9c4e61..8f26e49 100644 --- a/engine/src/renderer/debug.cpp +++ b/engine/src/renderer/debug.cpp @@ -2,6 +2,8 @@ #include +namespace Engine { + void MessageCallback(GLenum source, GLenum type, GLuint id, @@ -59,3 +61,5 @@ void MessageCallback(GLenum source, // (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), // type, severity, message); } + +} diff --git a/engine/src/renderer/mesh.cpp b/engine/src/renderer/mesh.cpp index f453f32..47aa0cd 100644 --- a/engine/src/renderer/mesh.cpp +++ b/engine/src/renderer/mesh.cpp @@ -2,6 +2,8 @@ #include "engine/renderer/mesh.h" +namespace Engine { + Mesh::Mesh() { m_vao = 0; m_vbo = 0; @@ -57,4 +59,6 @@ void Mesh::Render(unsigned int count) glDrawElements(GL_TRIANGLES, static_cast(m_indexBuffer.size()), GL_UNSIGNED_INT, 0); } Unbind(); -} \ No newline at end of file +} + +} diff --git a/engine/src/renderer/renderer.cpp b/engine/src/renderer/renderer.cpp index 5dace4f..8ba21d0 100644 --- a/engine/src/renderer/renderer.cpp +++ b/engine/src/renderer/renderer.cpp @@ -18,6 +18,8 @@ #include "engine/components/mesh.h" #include "engine/components/batch.h" +namespace Engine { + Renderer::Renderer(std::shared_ptr scene) : m_scene(scene) { m_proj = glm::perspective( @@ -245,4 +247,6 @@ void Renderer::Render() { ApplyLights(m_shader); UpdateView(); RenderScene(m_shader); -} \ No newline at end of file +} + +} diff --git a/engine/src/renderer/shader.cpp b/engine/src/renderer/shader.cpp index 4f33a68..82913a8 100644 --- a/engine/src/renderer/shader.cpp +++ b/engine/src/renderer/shader.cpp @@ -2,6 +2,8 @@ #include #include "engine/renderer/shader.h" +namespace Engine { + Shader::Shader() { } @@ -132,4 +134,6 @@ void Shader::checkLinkingError() << infoLog << std::endl; } -} \ No newline at end of file +} + +} diff --git a/engine/src/renderer/texture.cpp b/engine/src/renderer/texture.cpp index 04539c8..e276830 100644 --- a/engine/src/renderer/texture.cpp +++ b/engine/src/renderer/texture.cpp @@ -7,6 +7,8 @@ #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" +namespace Engine { + std::unique_ptr Texture::LoadFile(const std::string& filename) { auto texture = std::make_unique(); @@ -34,3 +36,5 @@ std::unique_ptr Texture::LoadFile(const std::string& filename) { return std::move(texture); } + +} diff --git a/engine/src/renderer/wavefront.cpp b/engine/src/renderer/wavefront.cpp index fb8b5dc..38602b2 100644 --- a/engine/src/renderer/wavefront.cpp +++ b/engine/src/renderer/wavefront.cpp @@ -12,6 +12,8 @@ #define DEFAULT_MATERIAL_NAME "default" +namespace Engine { + // ObjElement toElement(const std::string &s) { // if (s == "#") return ObjElement::OHASH; // if (s == "mtllib") return ObjElement::MTLLIB; @@ -509,4 +511,4 @@ void Object::Render(Shader& shader, unsigned int count) } } - +} diff --git a/engine/src/scene/scene.cpp b/engine/src/scene/scene.cpp index 1b5af07..fa7b3a2 100644 --- a/engine/src/scene/scene.cpp +++ b/engine/src/scene/scene.cpp @@ -1,4 +1,11 @@ #include "engine/scene/scene.h" -Scene::Scene() : m_registry() { +namespace Engine { + +Scene::Scene() = default; + +std::unique_ptr Scene::CreateEntity() { + return std::unique_ptr(new Entity(m_registry.create(), this)); } + +} // namespace Engine diff --git a/engine/src/window/window.cpp b/engine/src/window/window.cpp index beb2226..71eeb3a 100644 --- a/engine/src/window/window.cpp +++ b/engine/src/window/window.cpp @@ -8,6 +8,8 @@ #include "engine/renderer/debug.h" +namespace Engine { + std::shared_ptr Window::s_instance = nullptr; Window::Window(const char* title, int width, int height) { @@ -100,12 +102,10 @@ void Window::ProcessEvents() { switch (event.type) { case SDL_EVENT_WINDOW_CLOSE_REQUESTED: case SDL_EVENT_QUIT: - Dispatch(WindowCloseEvent()); EmitEvent(WindowCloseEvent{}); break; case SDL_EVENT_KEY_DOWN: if (event.key.scancode == SDL_SCANCODE_ESCAPE) { - Dispatch(WindowCloseEvent()); EmitEvent(WindowCloseEvent{}); } if (event.key.scancode == SDL_SCANCODE_F11) { @@ -125,7 +125,6 @@ void Window::ProcessEvents() { width, height); auto event = WindowResizeEvent(static_cast(m_width), static_cast(m_height)); - Dispatch(event); EmitEvent(event); SDL_SetWindowRelativeMouseMode(m_handle, true); SDL_Rect boundaries = {0, 0, m_width, m_height}; @@ -152,3 +151,4 @@ void Window::Destroy() const { SDL_DestroyWindow(m_handle); } +} diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index 33a73ea..22d1c53 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -19,8 +19,12 @@ #include "engine/components/rotate.h" #include "engine/components/batch.h" +#include "engine/scene/scene.h" + #include "engine/api.h" +using namespace Engine; + class Game : public IApplication { public: Game() {} @@ -30,49 +34,49 @@ public: m_scene = scene; Object* lightObj = Object::LoadFile("./assets/common/sphere/sphere.obj"); - const auto lght = scene->m_registry.create(); - scene->m_registry.emplace(lght, glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f)); - scene->m_registry.emplace(lght, light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f); - scene->m_registry.emplace(lght, std::shared_ptr(lightObj)); + 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)); - const auto cameraEntity = scene->m_registry.create(); - scene->m_registry.emplace(cameraEntity, glm::vec3(0.f, 2.f, 2.f)); - scene->m_registry.emplace(cameraEntity); + cameraEntity = scene->CreateEntity(); + cameraEntity->AddComponent(); + cameraEntity->AddComponent(glm::vec3(0.f, 2.f, 2.f)); Object* targetObj = Object::LoadFile("./assets/wizard/wizard.obj"); - const auto targetEntity = scene->m_registry.create(); - scene->m_registry.emplace(targetEntity, glm::vec3(0.f, 0.0f, 0.f)); - scene->m_registry.emplace(targetEntity, std::shared_ptr(targetObj)); - scene->m_registry.emplace(targetEntity); + modelEntity = scene->CreateEntity(); + 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(); - scene->m_registry.emplace(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f)); - scene->m_registry.emplace(cubeEntity, std::shared_ptr(grass)); + // Object* grass = Object::LoadFile("./assets/common/cube/cube.obj"); + // const auto cubeEntity = scene->m_registry.create(); + // scene->m_registry.emplace(cubeEntity, glm::vec3(-1.5f, 0.4f, 0.f)); + // scene->m_registry.emplace(cubeEntity, std::shared_ptr(grass)); // 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->m_registry.create(); - scene->m_registry.emplace(batchEntt); - scene->m_registry.emplace(batchEntt, cubeObj); - auto cubeBatch = scene->m_registry.get(batchEntt); + const 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->m_registry.create(); + const 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] - scene->m_registry.emplace(cubeEntity, glm::vec3(x, y, z)); - scene->m_registry.emplace(cubeEntity); - scene->m_registry.emplace(cubeEntity, 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->m_registry.create(); - scene->m_registry.emplace(floorEntt, glm::vec3(0.f)); - scene->m_registry.emplace(floorEntt, std::shared_ptr(floorObj)); + const auto floorEntt = scene->CreateEntity(); + floorEntt->AddComponent(glm::vec3(0.f)); + floorEntt->AddComponent(std::shared_ptr(floorObj)); std::cout << "Game initialized" << std::endl; @@ -130,11 +134,9 @@ public: if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f; if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f; - auto view = m_scene->m_registry.view(); - for (auto [cam, camTransform] : view.each()) { - camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f - camTransform.rotation = cameraViewDirection; - } + auto camTransform = cameraEntity->GetComponent(); + camTransform.position += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f + camTransform.rotation = cameraViewDirection; // update rotation if (!m_paused) { @@ -167,24 +169,23 @@ public: glm::vec3 sunColor = glm::mix(dayColor, sunsetColor, sunsetFactor); // Update the directional light in the registry - auto lightsView = m_scene->m_registry.view(); - for (auto [entity, l, t] : lightsView.each()) { - if (l.type == light::LightType::DIRECTIONAL) { - // "position" for directional light often stores direction vector - // If your system instead uses transform.rotation, adjust accordingly - t.position = sunDir * 15.f; // use this as light direction - l.color = sunColor; - l.intensity = intensity; - } + 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 + t.position = sunDir * 15.f; // use this as light direction + l.color = sunColor; + l.intensity = intensity; } - auto rotateEntts = m_scene->m_registry.view(); - for (auto [entity, t] : rotateEntts.each()) { - // auto targetTransform = rotateEntts.get(entity); - if (!m_scene->m_registry.all_of(entity)) { - t.rotation.y = m_angle; - } - } + // auto rotateEntts = m_scene->m_registry.view(); + // for (auto [entity, t] : rotateEntts.each()) { + // // auto targetTransform = rotateEntts.get(entity); + // if (!m_scene->m_registry.all_of(entity)) { + // t.rotation.y = m_angle; + // } + // } m_frameCount++; m_currentTicks = SDL_GetTicks(); @@ -210,6 +211,10 @@ public: private: std::shared_ptr m_scene; + std::unique_ptr lightEntity; + std::unique_ptr cameraEntity; + std::unique_ptr modelEntity; + float m_angle; Uint64 m_lastTicks;