diff --git a/engine/source/editor/include/editor.h b/engine/source/editor/include/editor.h index d4b63df0a..19116e9fe 100644 --- a/engine/source/editor/include/editor.h +++ b/engine/source/editor/include/editor.h @@ -1,6 +1,5 @@ #pragma once -#include "runtime/core/base/public_singleton.h" #include "runtime/core/math/vector2.h" #include @@ -10,12 +9,12 @@ namespace Pilot class EditorUI; class PilotEngine; - class PilotEditor : public PublicSingleton + class PilotEditor { friend class EditorUI; - friend class PublicSingleton; public: + PilotEditor(); virtual ~PilotEditor(); void initialize(PilotEngine* engine_runtime); @@ -24,8 +23,6 @@ namespace Pilot void run(); protected: - PilotEditor(); - std::shared_ptr m_editor_ui; PilotEngine* m_engine_runtime{ nullptr }; }; diff --git a/engine/source/editor/include/editor_global_context.h b/engine/source/editor/include/editor_global_context.h index d6977ac2b..dc7a50c01 100644 --- a/engine/source/editor/include/editor_global_context.h +++ b/engine/source/editor/include/editor_global_context.h @@ -6,15 +6,17 @@ namespace Pilot { class WindowSystem* window_system; class RenderSystem* render_system; + class PilotEngine* engine_runtime; }; class EditorGlobalContext { public: - class EditorSceneManager* m_scene_manager{ nullptr }; - class EditorInputManager* m_input_manager{ nullptr }; - class RenderSystem* m_render_system{ nullptr }; - class WindowSystem* m_window_system{ nullptr }; + class EditorSceneManager* m_scene_manager {nullptr}; + class EditorInputManager* m_input_manager {nullptr}; + class RenderSystem* m_render_system {nullptr}; + class WindowSystem* m_window_system {nullptr}; + class PilotEngine* m_engine_runtime {nullptr}; public: void initialize(const EditorGlobalContextInitInfo& init_info); @@ -22,4 +24,4 @@ namespace Pilot }; extern EditorGlobalContext g_editor_global_context; -} \ No newline at end of file +} // namespace Pilot \ No newline at end of file diff --git a/engine/source/editor/include/editor_input_manager.h b/engine/source/editor/include/editor_input_manager.h index 8c47ef0dd..46934baf0 100644 --- a/engine/source/editor/include/editor_input_manager.h +++ b/engine/source/editor/include/editor_input_manager.h @@ -1,66 +1,69 @@ #pragma once -#include -//#include -#include "runtime/function/render/render_camera.h" #include "runtime/core/math/vector2.h" +#include "runtime/function/render/render_camera.h" + +#include + namespace Pilot { - class PilotEditor; + class PilotEditor; + + enum class EditorCommand : unsigned int + { + camera_left = 1 << 0, // A + camera_back = 1 << 1, // S + camera_foward = 1 << 2, // W + camera_right = 1 << 3, // D + camera_up = 1 << 4, // Q + camera_down = 1 << 5, // E + translation_mode = 1 << 6, // T + rotation_mode = 1 << 7, // R + scale_mode = 1 << 8, // C + exit = 1 << 9, // Esc + delete_object = 1 << 10, // Delete + }; + + class EditorInputManager + { + public: + void initialize(); + void tick(float delta_time); - enum class EditorCommand : unsigned int - { - camera_left = 1 << 0, // A - camera_back = 1 << 1, // S - camera_foward = 1 << 2, // W - camera_right = 1 << 3, // D - camera_up = 1 << 4, // Q - camera_down = 1 << 5, // E - translation_mode = 1 << 6, // T - rotation_mode = 1 << 7, // R - scale_mode = 1 << 8, // C - exit = 1 << 9, // Esc - delete_object = 1 << 10, // Delete - }; + public: + void registerInput(); + void updateCursorOnAxis(Vector2 cursor_uv); + void processEditorCommand(); + void onKeyInEditorMode(int key, int scancode, int action, int mods); - class EditorInputManager - { - public: - void initialize(); - void tick(float delta_time); - public: - void registerInput(); - void updateCursorOnAxis(Vector2 cursor_uv); - void processEditorCommand(); - void onKeyInEditorMode(int key, int scancode, int action, int mods); + void onKey(int key, int scancode, int action, int mods); + void onReset(); + void onCursorPos(double xpos, double ypos); + void onCursorEnter(int entered); + void onScroll(double xoffset, double yoffset); + void onMouseButtonClicked(int key, int action); + void onWindowClosed(); + bool isCursorInRect(Vector2 pos, Vector2 size) const; - void onKey(int key, int scancode, int action, int mods); - void onReset(); - void onCursorPos(double xpos, double ypos); - void onCursorEnter(int entered); - void onScroll(double xoffset, double yoffset); - void onMouseButtonClicked(int key, int action); - void onWindowClosed(); + public: + Vector2 getEngineWindowPos() const { return m_engine_window_pos; }; + Vector2 getEngineWindowSize() const { return m_engine_window_size; }; + float getCameraSpeed() const { return m_camera_speed; }; - bool isCursorInRect(Vector2 pos, Vector2 size) const; - public: - Vector2 getEngineWindowPos() { return m_engine_window_pos; }; - Vector2 getEngineWindowSize() { return m_engine_window_size; }; - float getCameraSpeed() { return m_camera_speed; }; + void setEngineWindowPos(Vector2 new_window_pos) { m_engine_window_pos = new_window_pos; }; + void setEngineWindowSize(Vector2 new_window_size) { m_engine_window_size = new_window_size; }; + void resetEditorCommand() { m_editor_command = 0; } - void setEngineWindowPos(Vector2 new_window_pos) { m_engine_window_pos = new_window_pos; }; - void setEngineWindowSize(Vector2 new_window_size) { m_engine_window_size = new_window_size; }; - void resetEditorCommand() { m_editor_command = 0; } - private: - Vector2 m_engine_window_pos{ 0.0f, 0.0f }; - Vector2 m_engine_window_size{ 1280.0f, 768.0f }; - float m_mouse_x{ 0.0f }; - float m_mouse_y{ 0.0f }; - float m_camera_speed{ 0.05f }; + private: + Vector2 m_engine_window_pos {0.0f, 0.0f}; + Vector2 m_engine_window_size {1280.0f, 768.0f}; + float m_mouse_x {0.0f}; + float m_mouse_y {0.0f}; + float m_camera_speed {0.05f}; - size_t m_cursor_on_axis{ 3 }; - unsigned int m_editor_command{ 0 }; - }; -} + size_t m_cursor_on_axis {3}; + unsigned int m_editor_command {0}; + }; +} // namespace Pilot diff --git a/engine/source/editor/source/editor.cpp b/engine/source/editor/source/editor.cpp index f5f7ec3b7..a57a956b1 100644 --- a/engine/source/editor/source/editor.cpp +++ b/engine/source/editor/source/editor.cpp @@ -1,14 +1,14 @@ #include "editor//include/editor.h" -#include "editor/include/editor_ui.h" -#include "editor/include/editor_scene_manager.h" -#include "editor/include/editor_input_manager.h" -#include "editor/include/editor_global_context.h" -#include "runtime/engine.h" -#include "runtime/function/render/render_system.h" +#include "runtime/engine.h" +#include "runtime/function/global/global_context.h" #include "runtime/function/render/render_camera.h" +#include "runtime/function/render/render_system.h" -#include +#include "editor/include/editor_global_context.h" +#include "editor/include/editor_input_manager.h" +#include "editor/include/editor_scene_manager.h" +#include "editor/include/editor_ui.h" namespace Pilot { @@ -25,7 +25,6 @@ namespace Pilot PilotEditor::~PilotEditor() {} - void PilotEditor::initialize(PilotEngine* engine_runtime) { assert(engine_runtime); @@ -33,20 +32,18 @@ namespace Pilot g_is_editor_mode = true; m_engine_runtime = engine_runtime; - EditorGlobalContextInitInfo init_info = {engine_runtime->getWindowSystem().get(),engine_runtime->getRenderSystem().get()}; + EditorGlobalContextInitInfo init_info = {g_runtime_global_context.m_window_system.get(), + g_runtime_global_context.m_render_system.get()}; g_editor_global_context.initialize(init_info); - g_editor_global_context.m_scene_manager->setEditorCamera(engine_runtime->m_render_system->getRenderCamera()); + g_editor_global_context.m_scene_manager->setEditorCamera(g_runtime_global_context.m_render_system->getRenderCamera()); g_editor_global_context.m_scene_manager->uploadAxisResource(); - m_editor_ui = std::make_shared(); - WindowUIInitInfo ui_init_info = {engine_runtime->m_window_system, engine_runtime->m_render_system}; + m_editor_ui = std::make_shared(); + WindowUIInitInfo ui_init_info = {g_runtime_global_context.m_window_system, g_runtime_global_context.m_render_system}; m_editor_ui->initialize(ui_init_info); } - void PilotEditor::clear() - { - g_editor_global_context.clear(); - } + void PilotEditor::clear() { g_editor_global_context.clear(); } void PilotEditor::run() { diff --git a/engine/source/editor/source/editor_file_service.cpp b/engine/source/editor/source/editor_file_service.cpp index 674a10b09..75238daca 100644 --- a/engine/source/editor/source/editor_file_service.cpp +++ b/engine/source/editor/source/editor_file_service.cpp @@ -2,9 +2,12 @@ #include "runtime/platform/file_service/file_service.h" #include "runtime/platform/path/path.h" + #include "runtime/resource/asset_manager/asset_manager.h" #include "runtime/resource/config_manager/config_manager.h" +#include "runtime/function/global/global_context.h" + namespace Pilot { /// helper function: split the input string with separator, and filter the substring @@ -42,16 +45,13 @@ namespace Pilot void EditorFileService::buildEngineFileTree() { - ConfigManager& config_manager = ConfigManager::getInstance(); - Path& path_singleton = Path::getInstance(); - - std::string asset_folder = config_manager.getAssetFolder().generic_string(); - const std::vector file_paths = FileService::getInstance().getFiles(asset_folder); + std::string asset_folder = g_runtime_global_context.m_config_manager->getAssetFolder().generic_string(); + const std::vector file_paths = g_runtime_global_context.m_file_servcie->getFiles(asset_folder); std::vector> all_file_segments; for (const auto& path : file_paths) { - const std::filesystem::path& relative_path = path_singleton.getRelativePath(asset_folder, path); - all_file_segments.emplace_back(path_singleton.getPathSegments(relative_path)); + const std::filesystem::path& relative_path = Path::getRelativePath(asset_folder, path); + all_file_segments.emplace_back(Path::getPathSegments(relative_path)); } std::vector> node_array; @@ -78,7 +78,7 @@ namespace Pilot } else { - const auto& extensions = path_singleton.getFileExtensions(file_paths[file_index]); + const auto& extensions = Path::getFileExtensions(file_paths[file_index]); file_node->m_file_type = std::get<0>(extensions); if (file_node->m_file_type.size() == 0) continue; diff --git a/engine/source/editor/source/editor_global_context.cpp b/engine/source/editor/source/editor_global_context.cpp index 2942410c7..e883751b7 100644 --- a/engine/source/editor/source/editor_global_context.cpp +++ b/engine/source/editor/source/editor_global_context.cpp @@ -1,10 +1,10 @@ #include "editor/include/editor_global_context.h" -#include "editor/include/editor_scene_manager.h" #include "editor/include/editor_input_manager.h" +#include "editor/include/editor_scene_manager.h" -#include "runtime/function/render/window_system.h" #include "runtime/function/render/render_system.h" +#include "runtime/function/render/window_system.h" namespace Pilot { @@ -12,8 +12,10 @@ namespace Pilot void EditorGlobalContext::initialize(const EditorGlobalContextInitInfo& init_info) { - g_editor_global_context.m_window_system = init_info.window_system; - g_editor_global_context.m_render_system = init_info.render_system; + g_editor_global_context.m_window_system = init_info.window_system; + g_editor_global_context.m_render_system = init_info.render_system; + g_editor_global_context.m_engine_runtime = init_info.engine_runtime; + m_scene_manager = new EditorSceneManager(); m_input_manager = new EditorInputManager(); m_scene_manager->initialize(); @@ -22,7 +24,7 @@ namespace Pilot void EditorGlobalContext::clear() { - delete(m_scene_manager); - delete(m_input_manager); + delete (m_scene_manager); + delete (m_input_manager); } -} \ No newline at end of file +} // namespace Pilot \ No newline at end of file diff --git a/engine/source/editor/source/editor_input_manager.cpp b/engine/source/editor/source/editor_input_manager.cpp index a21084a69..87dc33b8f 100644 --- a/engine/source/editor/source/editor_input_manager.cpp +++ b/engine/source/editor/source/editor_input_manager.cpp @@ -1,44 +1,44 @@ -#include #include "editor/include/editor_input_manager.h" + #include "editor/include/editor.h" #include "editor/include/editor_global_context.h" #include "editor/include/editor_scene_manager.h" -#include "engine.h" +#include "runtime/engine.h" #include "runtime/function/framework/level/level.h" -#include "runtime/function/input/input_system.h" #include "runtime/function/framework/world/world_manager.h" +#include "runtime/function/global/global_context.h" +#include "runtime/function/input/input_system.h" #include "runtime/function/render/render_camera.h" -#include "runtime/function/render/window_system.h" #include "runtime/function/render/render_system.h" +#include "runtime/function/render/window_system.h" namespace Pilot { - void EditorInputManager::initialize() - { - registerInput(); - } + void EditorInputManager::initialize() { registerInput(); } - void EditorInputManager::tick(float delta_time) - { - processEditorCommand(); - } + void EditorInputManager::tick(float delta_time) { processEditorCommand(); } void EditorInputManager::registerInput() { g_editor_global_context.m_window_system->registerOnResetFunc(std::bind(&EditorInputManager::onReset, this)); - g_editor_global_context.m_window_system->registerOnCursorPosFunc(std::bind(&EditorInputManager::onCursorPos, this, std::placeholders::_1, std::placeholders::_2)); - g_editor_global_context.m_window_system->registerOnCursorEnterFunc(std::bind(&EditorInputManager::onCursorEnter, this, std::placeholders::_1)); - g_editor_global_context.m_window_system->registerOnScrollFunc(std::bind(&EditorInputManager::onScroll, this, std::placeholders::_1, std::placeholders::_2)); - g_editor_global_context.m_window_system->registerOnMouseButtonFunc(std::bind(&EditorInputManager::onMouseButtonClicked, this, std::placeholders::_1, std::placeholders::_2)); - g_editor_global_context.m_window_system->registerOnWindowCloseFunc(std::bind(&EditorInputManager::onWindowClosed, this)); + g_editor_global_context.m_window_system->registerOnCursorPosFunc( + std::bind(&EditorInputManager::onCursorPos, this, std::placeholders::_1, std::placeholders::_2)); + g_editor_global_context.m_window_system->registerOnCursorEnterFunc( + std::bind(&EditorInputManager::onCursorEnter, this, std::placeholders::_1)); + g_editor_global_context.m_window_system->registerOnScrollFunc( + std::bind(&EditorInputManager::onScroll, this, std::placeholders::_1, std::placeholders::_2)); + g_editor_global_context.m_window_system->registerOnMouseButtonFunc( + std::bind(&EditorInputManager::onMouseButtonClicked, this, std::placeholders::_1, std::placeholders::_2)); + g_editor_global_context.m_window_system->registerOnWindowCloseFunc( + std::bind(&EditorInputManager::onWindowClosed, this)); g_editor_global_context.m_window_system->registerOnKeyFunc(std::bind(&EditorInputManager::onKey, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3, - std::placeholders::_4)); + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3, + std::placeholders::_4)); } void EditorInputManager::updateCursorOnAxis(Vector2 cursor_uv) @@ -52,34 +52,34 @@ namespace Pilot void EditorInputManager::processEditorCommand() { - float camera_speed = m_camera_speed; + float camera_speed = m_camera_speed; std::shared_ptr editor_camera = g_editor_global_context.m_scene_manager->getEditorCamera(); - Quaternion camera_rotate = editor_camera->rotation().inverse(); - Vector3 camera_relative_pos(0, 0, 0); + Quaternion camera_rotate = editor_camera->rotation().inverse(); + Vector3 camera_relative_pos(0, 0, 0); if ((unsigned int)EditorCommand::camera_foward & m_editor_command) { - camera_relative_pos += camera_rotate * Vector3{ 0, camera_speed, 0 }; + camera_relative_pos += camera_rotate * Vector3 {0, camera_speed, 0}; } if ((unsigned int)EditorCommand::camera_back & m_editor_command) { - camera_relative_pos += camera_rotate * Vector3{ 0, -camera_speed, 0 }; + camera_relative_pos += camera_rotate * Vector3 {0, -camera_speed, 0}; } if ((unsigned int)EditorCommand::camera_left & m_editor_command) { - camera_relative_pos += camera_rotate * Vector3{ -camera_speed, 0, 0 }; + camera_relative_pos += camera_rotate * Vector3 {-camera_speed, 0, 0}; } if ((unsigned int)EditorCommand::camera_right & m_editor_command) { - camera_relative_pos += camera_rotate * Vector3{ camera_speed, 0, 0 }; + camera_relative_pos += camera_rotate * Vector3 {camera_speed, 0, 0}; } if ((unsigned int)EditorCommand::camera_up & m_editor_command) { - camera_relative_pos += Vector3{ 0, 0, camera_speed }; + camera_relative_pos += Vector3 {0, 0, camera_speed}; } if ((unsigned int)EditorCommand::camera_down & m_editor_command) { - camera_relative_pos += Vector3{ 0, 0, -camera_speed }; + camera_relative_pos += Vector3 {0, 0, -camera_speed}; } if ((unsigned int)EditorCommand::delete_object & m_editor_command) { @@ -95,79 +95,79 @@ namespace Pilot { switch (key) { - case GLFW_KEY_A: - m_editor_command |= (unsigned int)EditorCommand::camera_left; - break; - case GLFW_KEY_S: - m_editor_command |= (unsigned int)EditorCommand::camera_back; - break; - case GLFW_KEY_W: - m_editor_command |= (unsigned int)EditorCommand::camera_foward; - break; - case GLFW_KEY_D: - m_editor_command |= (unsigned int)EditorCommand::camera_right; - break; - case GLFW_KEY_Q: - m_editor_command |= (unsigned int)EditorCommand::camera_up; - break; - case GLFW_KEY_E: - m_editor_command |= (unsigned int)EditorCommand::camera_down; - break; - case GLFW_KEY_T: - m_editor_command |= (unsigned int)EditorCommand::translation_mode; - break; - case GLFW_KEY_R: - m_editor_command |= (unsigned int)EditorCommand::rotation_mode; - break; - case GLFW_KEY_C: - m_editor_command |= (unsigned int)EditorCommand::scale_mode; - break; - case GLFW_KEY_DELETE: - m_editor_command |= (unsigned int)EditorCommand::delete_object; - break; - default: - break; + case GLFW_KEY_A: + m_editor_command |= (unsigned int)EditorCommand::camera_left; + break; + case GLFW_KEY_S: + m_editor_command |= (unsigned int)EditorCommand::camera_back; + break; + case GLFW_KEY_W: + m_editor_command |= (unsigned int)EditorCommand::camera_foward; + break; + case GLFW_KEY_D: + m_editor_command |= (unsigned int)EditorCommand::camera_right; + break; + case GLFW_KEY_Q: + m_editor_command |= (unsigned int)EditorCommand::camera_up; + break; + case GLFW_KEY_E: + m_editor_command |= (unsigned int)EditorCommand::camera_down; + break; + case GLFW_KEY_T: + m_editor_command |= (unsigned int)EditorCommand::translation_mode; + break; + case GLFW_KEY_R: + m_editor_command |= (unsigned int)EditorCommand::rotation_mode; + break; + case GLFW_KEY_C: + m_editor_command |= (unsigned int)EditorCommand::scale_mode; + break; + case GLFW_KEY_DELETE: + m_editor_command |= (unsigned int)EditorCommand::delete_object; + break; + default: + break; } } else if (action == GLFW_RELEASE) { switch (key) { - case GLFW_KEY_ESCAPE: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::exit); - break; - case GLFW_KEY_A: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_left); - break; - case GLFW_KEY_S: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_back); - break; - case GLFW_KEY_W: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_foward); - break; - case GLFW_KEY_D: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_right); - break; - case GLFW_KEY_Q: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_up); - break; - case GLFW_KEY_E: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_down); - break; - case GLFW_KEY_T: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::translation_mode); - break; - case GLFW_KEY_R: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::rotation_mode); - break; - case GLFW_KEY_C: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::scale_mode); - break; - case GLFW_KEY_DELETE: - m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::delete_object); - break; - default: - break; + case GLFW_KEY_ESCAPE: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::exit); + break; + case GLFW_KEY_A: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_left); + break; + case GLFW_KEY_S: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_back); + break; + case GLFW_KEY_W: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_foward); + break; + case GLFW_KEY_D: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_right); + break; + case GLFW_KEY_Q: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_up); + break; + case GLFW_KEY_E: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::camera_down); + break; + case GLFW_KEY_T: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::translation_mode); + break; + case GLFW_KEY_R: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::rotation_mode); + break; + case GLFW_KEY_C: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::scale_mode); + break; + case GLFW_KEY_DELETE: + m_editor_command &= (k_complement_control_command ^ (unsigned int)EditorCommand::delete_object); + break; + default: + break; } } } @@ -196,13 +196,22 @@ namespace Pilot { if (g_editor_global_context.m_window_system->isMouseButtonDown(GLFW_MOUSE_BUTTON_RIGHT)) { - glfwSetInputMode(g_editor_global_context.m_window_system->getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); - g_editor_global_context.m_scene_manager->getEditorCamera()->rotate(Vector2(ypos - m_mouse_y, xpos - m_mouse_x) * angularVelocity); + glfwSetInputMode( + g_editor_global_context.m_window_system->getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); + g_editor_global_context.m_scene_manager->getEditorCamera()->rotate( + Vector2(ypos - m_mouse_y, xpos - m_mouse_x) * angularVelocity); } else if (g_editor_global_context.m_window_system->isMouseButtonDown(GLFW_MOUSE_BUTTON_LEFT)) { - g_editor_global_context.m_scene_manager->moveEntity(xpos, ypos, m_mouse_x, m_mouse_y, m_engine_window_pos, m_engine_window_size, - m_cursor_on_axis, g_editor_global_context.m_scene_manager->getSelectedObjectMatrix()); + g_editor_global_context.m_scene_manager->moveEntity( + xpos, + ypos, + m_mouse_x, + m_mouse_y, + m_engine_window_pos, + m_engine_window_size, + m_cursor_on_axis, + g_editor_global_context.m_scene_manager->getSelectedObjectMatrix()); glfwSetInputMode(g_editor_global_context.m_window_system->getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); } else @@ -212,7 +221,7 @@ namespace Pilot if (isCursorInRect(m_engine_window_pos, m_engine_window_size)) { Vector2 cursor_uv = Vector2((m_mouse_x - m_engine_window_pos.x) / m_engine_window_size.x, - (m_mouse_y - m_engine_window_pos.y) / m_engine_window_size.y); + (m_mouse_y - m_engine_window_pos.y) / m_engine_window_size.y); updateCursorOnAxis(cursor_uv); } } @@ -251,8 +260,8 @@ namespace Pilot } else { - g_editor_global_context.m_scene_manager->getEditorCamera()->zoom((float)yoffset * - 2.0f); // wheel scrolled up = zoom in by 2 extra degrees + g_editor_global_context.m_scene_manager->getEditorCamera()->zoom( + (float)yoffset * 2.0f); // wheel scrolled up = zoom in by 2 extra degrees } } } @@ -264,7 +273,7 @@ namespace Pilot if (m_cursor_on_axis != 3) return; - std::shared_ptr current_active_level = WorldManager::getInstance().getCurrentActiveLevel().lock(); + std::shared_ptr current_active_level = g_runtime_global_context.m_world_manager->getCurrentActiveLevel().lock(); if (current_active_level == nullptr) return; @@ -273,7 +282,7 @@ namespace Pilot if (key == GLFW_MOUSE_BUTTON_LEFT) { Vector2 picked_uv((m_mouse_x - m_engine_window_pos.x) / m_engine_window_size.x, - (m_mouse_y - m_engine_window_pos.y) / m_engine_window_size.y); + (m_mouse_y - m_engine_window_pos.y) / m_engine_window_size.y); size_t select_mesh_id = g_editor_global_context.m_scene_manager->getGuidOfPickedMesh(picked_uv); size_t gobject_id = g_editor_global_context.m_render_system->getGObjectIDByMeshID(select_mesh_id); @@ -282,13 +291,10 @@ namespace Pilot } } - void EditorInputManager::onWindowClosed() - { - PilotEngine::getInstance().shutdownEngine(); - } + void EditorInputManager::onWindowClosed() { g_editor_global_context.m_engine_runtime->shutdownEngine(); } bool EditorInputManager::isCursorInRect(Vector2 pos, Vector2 size) const { return pos.x <= m_mouse_x && m_mouse_x <= pos.x + size.x && pos.y <= m_mouse_y && m_mouse_y <= pos.y + size.y; } -} \ No newline at end of file +} // namespace Pilot \ No newline at end of file diff --git a/engine/source/editor/source/editor_scene_manager.cpp b/engine/source/editor/source/editor_scene_manager.cpp index 80679e039..a3647431d 100644 --- a/engine/source/editor/source/editor_scene_manager.cpp +++ b/engine/source/editor/source/editor_scene_manager.cpp @@ -1,32 +1,30 @@ -#include #include +#include #include "editor/include/editor.h" -#include "editor/include/editor_scene_manager.h" #include "editor/include/editor_global_context.h" +#include "editor/include/editor_scene_manager.h" #include -#include -#include +#include "runtime/core/base/macro.h" + +#include "runtime/engine.h" +#include "runtime/function/framework/component/transform/transform_component.h" #include "runtime/function/framework/level/level.h" #include "runtime/function/framework/world/world_manager.h" #include "runtime/function/input/input_system.h" -#include "runtime/function/ui/ui_system.h" - #include "runtime/function/render/glm_wrapper.h" #include "runtime/function/render/render_camera.h" #include "runtime/function/render/render_system.h" namespace Pilot { - void EditorSceneManager::initialize() - { - } + void EditorSceneManager::initialize() {} void EditorSceneManager::tick(float delta_time) { - //todo: editor scene tick + // todo: editor scene tick } float intersectPlaneRay(glm::vec3 normal, float d, glm::vec3 origin, glm::vec3 dir) @@ -40,16 +38,14 @@ namespace Pilot return -(glm::dot(normal, origin) + d) / deno; } - size_t EditorSceneManager::updateCursorOnAxis( - Vector2 cursor_uv, - Vector2 game_engine_window_size) + size_t EditorSceneManager::updateCursorOnAxis(Vector2 cursor_uv, Vector2 game_engine_window_size) { - float camera_fov = m_camera->getFovYDeprecated(); + float camera_fov = m_camera->getFovYDeprecated(); Vector3 camera_forward = m_camera->forward(); - Vector3 camera_up = m_camera->up(); - Vector3 camera_right = m_camera->right(); + Vector3 camera_up = m_camera->up(); + Vector3 camera_right = m_camera->right(); Vector3 camera_position = m_camera->position(); if (m_selected_gobject_id == k_invalid_gobject_id) @@ -57,7 +53,7 @@ namespace Pilot return m_selected_axis; } RenderEntity* selected_aixs = getAxisMeshByType(m_axis_mode); - m_selected_axis = 3; + m_selected_axis = 3; if (m_is_show_axis == false) { return m_selected_axis; @@ -71,7 +67,7 @@ namespace Pilot glm::vec3 model_skew; glm::vec4 model_perspective; glm::decompose(model_matrix, model_scale, model_rotation, model_translation, model_skew, model_perspective); - float window_forward = game_engine_window_size.y / 2.0f / glm::tan(glm::radians(camera_fov) / 2.0f); + float window_forward = game_engine_window_size.y / 2.0f / glm::tan(glm::radians(camera_fov) / 2.0f); glm::vec2 screen_center_uv = glm::vec2(cursor_uv.x, 1 - cursor_uv.y) - glm::vec2(0.5, 0.5); glm::vec3 world_ray_dir = GLMUtil::fromVec3(camera_forward) * window_forward + @@ -81,13 +77,13 @@ namespace Pilot glm::vec4 local_ray_origin = glm::inverse(model_matrix) * glm::vec4(GLMUtil::fromVec3(camera_position), 1.0f); glm::vec3 local_ray_origin_xyz = glm::vec3(local_ray_origin.x, local_ray_origin.y, local_ray_origin.z); - glm::vec3 local_ray_dir = glm::normalize(glm::inverse(model_rotation)) * world_ray_dir; + glm::vec3 local_ray_dir = glm::normalize(glm::inverse(model_rotation)) * world_ray_dir; - glm::vec3 plane_normals[3] = { glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1) }; + glm::vec3 plane_normals[3] = {glm::vec3(1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1)}; - float plane_view_depth[3] = { intersectPlaneRay(plane_normals[0], 0, local_ray_origin_xyz, local_ray_dir), + float plane_view_depth[3] = {intersectPlaneRay(plane_normals[0], 0, local_ray_origin_xyz, local_ray_dir), intersectPlaneRay(plane_normals[1], 0, local_ray_origin_xyz, local_ray_dir), - intersectPlaneRay(plane_normals[2], 0, local_ray_origin_xyz, local_ray_dir) }; + intersectPlaneRay(plane_normals[2], 0, local_ray_origin_xyz, local_ray_dir)}; glm::vec3 intersect_pt[3] = { local_ray_origin_xyz + plane_view_depth[0] * local_ray_dir, // yoz @@ -97,23 +93,23 @@ namespace Pilot if ((int)m_axis_mode == 0 || (int)m_axis_mode == 2) // transition axis & scale axis { - const float DIST_THRESHOLD = 0.6f; + const float DIST_THRESHOLD = 0.6f; const float EDGE_OF_AXIS_MIN = 0.1f; const float EDGE_OF_AXIS_MAX = 2.0f; - const float AXIS_LENGTH = 2.0f; + const float AXIS_LENGTH = 2.0f; float max_dist = 0.0f; // whether the ray (camera to mouse point) on any plane for (int i = 0; i < 3; ++i) { float local_ray_dir_proj = glm::abs(glm::dot(local_ray_dir, plane_normals[i])); - float cos_alpha = local_ray_dir_proj / 1.0f; // local_ray_dir_proj / local_ray_dir.length + float cos_alpha = local_ray_dir_proj / 1.0f; // local_ray_dir_proj / local_ray_dir.length if (cos_alpha <= 0.15) // cos(80deg)~cps(100deg) { - int index00 = (i + 1) % 3; - int index01 = 3 - i - index00; - int index10 = (i + 2) % 3; - int index11 = 3 - i - index10; + int index00 = (i + 1) % 3; + int index01 = 3 - i - index00; + int index10 = (i + 2) % 3; + int index11 = 3 - i - index10; float axis_dist = (glm::abs(intersect_pt[index00][i]) + glm::abs(intersect_pt[index10][i])) / 2; if (axis_dist > DIST_THRESHOLD) // too far from axis { @@ -125,7 +121,7 @@ namespace Pilot (intersect_pt[index00][index01] > max_dist) && (glm::abs(intersect_pt[index00][i]) < EDGE_OF_AXIS_MAX)) { - max_dist = intersect_pt[index00][index01]; + max_dist = intersect_pt[index00][index01]; m_selected_axis = index01; } if ((intersect_pt[index10][index11] > EDGE_OF_AXIS_MIN) && @@ -133,7 +129,7 @@ namespace Pilot (intersect_pt[index10][index11] > max_dist) && (glm::abs(intersect_pt[index10][i]) < EDGE_OF_AXIS_MAX)) { - max_dist = intersect_pt[index10][index11]; + max_dist = intersect_pt[index10][index11]; m_selected_axis = index11; } } @@ -152,7 +148,7 @@ namespace Pilot (intersect_pt[index0][i] < EDGE_OF_AXIS_MAX) && (dist < DIST_THRESHOLD) && (dist < min_dist)) { - min_dist = dist; + min_dist = dist; m_selected_axis = i; } } @@ -169,7 +165,7 @@ namespace Pilot std::fabs(1 - std::hypot(intersect_pt[i].x, intersect_pt[i].y, intersect_pt[i].z)); if ((dist < DIST_THRESHOLD) && (dist < min_dist)) { - min_dist = dist; + min_dist = dist; m_selected_axis = i; } } @@ -179,7 +175,7 @@ namespace Pilot return m_selected_axis; } } - + g_editor_global_context.m_render_system->setSelectedAxis(m_selected_axis); return m_selected_axis; @@ -190,17 +186,17 @@ namespace Pilot RenderEntity* axis_mesh = nullptr; switch (axis_mode) { - case EditorAxisMode::TranslateMode: - axis_mesh = &m_translation_axis; - break; - case EditorAxisMode::RotateMode: - axis_mesh = &m_rotation_axis; - break; - case EditorAxisMode::ScaleMode: - axis_mesh = &m_scale_aixs; - break; - default: - break; + case EditorAxisMode::TranslateMode: + axis_mesh = &m_translation_axis; + break; + case EditorAxisMode::RotateMode: + axis_mesh = &m_rotation_axis; + break; + case EditorAxisMode::ScaleMode: + axis_mesh = &m_scale_aixs; + break; + default: + break; } return axis_mesh; } @@ -217,10 +213,10 @@ namespace Pilot Quaternion rotation; Vector3 translation; transform_component->getMatrix().decomposition(translation, scale, rotation); - Matrix4x4 translation_matrix = Matrix4x4::getTrans(translation); - Matrix4x4 scale_matrix = Matrix4x4::buildScaleMatrix(1.0f, 1.0f, 1.0f); - Matrix4x4 axis_model_matrix = translation_matrix * scale_matrix; - RenderEntity* selected_aixs = getAxisMeshByType(m_axis_mode); + Matrix4x4 translation_matrix = Matrix4x4::getTrans(translation); + Matrix4x4 scale_matrix = Matrix4x4::buildScaleMatrix(1.0f, 1.0f, 1.0f); + Matrix4x4 axis_model_matrix = translation_matrix * scale_matrix; + RenderEntity* selected_aixs = getAxisMeshByType(m_axis_mode); if (m_axis_mode == EditorAxisMode::TranslateMode || m_axis_mode == EditorAxisMode::RotateMode) { selected_aixs->m_model_matrix = axis_model_matrix; @@ -229,7 +225,7 @@ namespace Pilot { selected_aixs->m_model_matrix = axis_model_matrix * Matrix4x4(rotation); } - + g_editor_global_context.m_render_system->setVisibleAxis(*selected_aixs); } else @@ -243,7 +239,7 @@ namespace Pilot std::weak_ptr selected_object; if (m_selected_gobject_id != k_invalid_gobject_id) { - std::shared_ptr level = WorldManager::getInstance().getCurrentActiveLevel().lock(); + std::shared_ptr level = g_runtime_global_context.m_world_manager->getCurrentActiveLevel().lock(); if (level != nullptr) { selected_object = level->getGObjectByID(m_selected_gobject_id); @@ -263,7 +259,7 @@ namespace Pilot if (selected_gobject) { const TransformComponent* transform_component = selected_gobject->tryGetComponentConst(TransformComponent); - m_selected_object_matrix = transform_component->getMatrix(); + m_selected_object_matrix = transform_component->getMatrix(); } drawSelectedEntityAxis(); @@ -284,7 +280,8 @@ namespace Pilot std::shared_ptr selected_object = getSelectedGObject().lock(); if (selected_object != nullptr) { - std::shared_ptr current_active_level = WorldManager::getInstance().getCurrentActiveLevel().lock(); + std::shared_ptr current_active_level = + g_runtime_global_context.m_world_manager->getCurrentActiveLevel().lock(); if (current_active_level == nullptr) return; @@ -297,13 +294,13 @@ namespace Pilot } void EditorSceneManager::moveEntity(float new_mouse_pos_x, - float new_mouse_pos_y, - float last_mouse_pos_x, - float last_mouse_pos_y, - Vector2 engine_window_pos, - Vector2 engine_window_size, - size_t cursor_on_axis, - Matrix4x4 model_matrix) + float new_mouse_pos_y, + float last_mouse_pos_x, + float last_mouse_pos_y, + Vector2 engine_window_pos, + Vector2 engine_window_size, + size_t cursor_on_axis, + Matrix4x4 model_matrix) { std::shared_ptr selected_object = getSelectedGObject().lock(); if (selected_object == nullptr) @@ -311,7 +308,7 @@ namespace Pilot float angularVelocity = 18.0f / Math::max(engine_window_size.x, engine_window_size.y); // 18 degrees while moving full screen - Vector2 delta_mouse_move_uv = { (new_mouse_pos_x - last_mouse_pos_x), (new_mouse_pos_y - last_mouse_pos_y) }; + Vector2 delta_mouse_move_uv = {(new_mouse_pos_x - last_mouse_pos_x), (new_mouse_pos_y - last_mouse_pos_y)}; Vector3 model_scale; Quaternion model_rotation; @@ -337,8 +334,8 @@ namespace Pilot axis_x_local_position_4 = Matrix4x4(model_rotation) * axis_x_local_position_4; } Vector4 axis_x_world_position_4 = axis_model_matrix * axis_x_local_position_4; - axis_x_world_position_4.w = 1.0f; - Vector4 axis_x_clip_position = proj_matrix * view_matrix * axis_x_world_position_4; + axis_x_world_position_4.w = 1.0f; + Vector4 axis_x_clip_position = proj_matrix * view_matrix * axis_x_world_position_4; axis_x_clip_position /= axis_x_clip_position.w; Vector2 axis_x_clip_uv((axis_x_clip_position.x + 1) / 2.0f, (axis_x_clip_position.y + 1) / 2.0f); Vector2 axis_x_direction_uv = axis_x_clip_uv - model_origin_clip_uv; @@ -350,8 +347,8 @@ namespace Pilot axis_y_local_position_4 = Matrix4x4(model_rotation) * axis_y_local_position_4; } Vector4 axis_y_world_position_4 = axis_model_matrix * axis_y_local_position_4; - axis_y_world_position_4.w = 1.0f; - Vector4 axis_y_clip_position = proj_matrix * view_matrix * axis_y_world_position_4; + axis_y_world_position_4.w = 1.0f; + Vector4 axis_y_clip_position = proj_matrix * view_matrix * axis_y_world_position_4; axis_y_clip_position /= axis_y_clip_position.w; Vector2 axis_y_clip_uv((axis_y_clip_position.x + 1) / 2.0f, (axis_y_clip_position.y + 1) / 2.0f); Vector2 axis_y_direction_uv = axis_y_clip_uv - model_origin_clip_uv; @@ -363,8 +360,8 @@ namespace Pilot axis_z_local_position_4 = Matrix4x4(model_rotation) * axis_z_local_position_4; } Vector4 axis_z_world_position_4 = axis_model_matrix * axis_z_local_position_4; - axis_z_world_position_4.w = 1.0f; - Vector4 axis_z_clip_position = proj_matrix * view_matrix * axis_z_world_position_4; + axis_z_world_position_4.w = 1.0f; + Vector4 axis_z_clip_position = proj_matrix * view_matrix * axis_z_world_position_4; axis_z_clip_position /= axis_z_clip_position.w; Vector2 axis_z_clip_uv((axis_z_clip_position.x + 1) / 2.0f, (axis_z_clip_position.y + 1) / 2.0f); Vector2 axis_z_direction_uv = axis_z_clip_uv - model_origin_clip_uv; @@ -375,7 +372,7 @@ namespace Pilot Matrix4x4 new_model_matrix(Matrix4x4::IDENTITY); if (m_axis_mode == EditorAxisMode::TranslateMode) // translate { - Vector3 move_vector = { 0, 0, 0 }; + Vector3 move_vector = {0, 0, 0}; if (cursor_on_axis == 0) { move_vector.x = delta_mouse_move_uv.dotProduct(axis_x_direction_uv) * angularVelocity; @@ -407,14 +404,14 @@ namespace Pilot new_model_matrix.decomposition(new_translation, new_scale, new_rotation); Matrix4x4 translation_matrix = Matrix4x4::getTrans(new_translation); - Matrix4x4 scale_matrix = Matrix4x4::buildScaleMatrix(1.f, 1.f, 1.f); - Matrix4x4 axis_model_matrix = translation_matrix * scale_matrix; + Matrix4x4 scale_matrix = Matrix4x4::buildScaleMatrix(1.f, 1.f, 1.f); + Matrix4x4 axis_model_matrix = translation_matrix * scale_matrix; m_translation_axis.m_model_matrix = axis_model_matrix; - m_rotation_axis.m_model_matrix = axis_model_matrix; - m_scale_aixs.m_model_matrix = axis_model_matrix; - - g_editor_global_context.m_render_system->setVisibleAxis(m_translation_axis); + m_rotation_axis.m_model_matrix = axis_model_matrix; + m_scale_aixs.m_model_matrix = axis_model_matrix; + + g_editor_global_context.m_render_system->setVisibleAxis(m_translation_axis); transform_component->setPosition(new_translation); transform_component->setRotation(new_rotation); @@ -430,7 +427,7 @@ namespace Pilot Vector2 new_move_vector(new_mouse_u - model_origin_clip_uv.x, new_mouse_v - model_origin_clip_uv.y); Vector3 delta_mouse_uv_3(delta_mouse_move_uv.x, delta_mouse_move_uv.y, 0); float move_radian; - Vector3 axis_of_rotation = { 0, 0, 0 }; + Vector3 axis_of_rotation = {0, 0, 0}; if (cursor_on_axis == 0) { move_radian = (delta_mouse_move_uv * angularVelocity).length(); @@ -487,8 +484,8 @@ namespace Pilot } else if (m_axis_mode == EditorAxisMode::ScaleMode) // scale { - Vector3 delta_scale_vector = { 0, 0, 0 }; - Vector3 new_model_scale = { 0, 0, 0 }; + Vector3 delta_scale_vector = {0, 0, 0}; + Vector3 new_model_scale = {0, 0, 0}; if (cursor_on_axis == 0) { delta_scale_vector.x = 0.01f; @@ -517,7 +514,7 @@ namespace Pilot { return; } - new_model_scale = model_scale + delta_scale_vector; + new_model_scale = model_scale + delta_scale_vector; axis_model_matrix = axis_model_matrix * Matrix4x4(model_rotation); Matrix4x4 scale_mat; scale_mat.makeTransform(Vector3::ZERO, new_model_scale, Quaternion::IDENTITY); @@ -532,8 +529,8 @@ namespace Pilot transform_component->setScale(new_scale); } setSelectedObjectMatrix(new_model_matrix); - } - + } + void EditorSceneManager::uploadAxisResource() { auto& instance_id_allocator = g_editor_global_context.m_render_system->getGOInstanceIdAllocator(); @@ -564,13 +561,13 @@ namespace Pilot m_scale_aixs.m_mesh_asset_id = mesh_asset_id_allocator.allocGuid(mesh_source_desc); } - g_editor_global_context.m_render_system->createAxis( + g_editor_global_context.m_render_system->createAxis( {m_translation_axis, m_rotation_axis, m_scale_aixs}, {m_translation_axis.m_mesh_data, m_rotation_axis.m_mesh_data, m_scale_aixs.m_mesh_data}); } size_t EditorSceneManager::getGuidOfPickedMesh(const Vector2& picked_uv) const - { + { return g_editor_global_context.m_render_system->getGuidOfPickedMesh(picked_uv); } -} +} // namespace Pilot diff --git a/engine/source/editor/source/editor_ui.cpp b/engine/source/editor/source/editor_ui.cpp index 30fadf8c9..e08f42eb3 100644 --- a/engine/source/editor/source/editor_ui.cpp +++ b/engine/source/editor/source/editor_ui.cpp @@ -1,8 +1,8 @@ #include "editor/include/editor_ui.h" #include "editor/include/editor_global_context.h" -#include "editor/include/editor_scene_manager.h" #include "editor/include/editor_input_manager.h" +#include "editor/include/editor_scene_manager.h" #include "runtime/core/base/macro.h" #include "runtime/core/meta/reflection/reflection.h" @@ -16,16 +16,13 @@ #include "runtime/function/framework/component/mesh/mesh_component.h" #include "runtime/function/framework/component/transform/transform_component.h" - #include "runtime/function/framework/level/level.h" #include "runtime/function/framework/world/world_manager.h" +#include "runtime/function/global/global_context.h" #include "runtime/function/input/input_system.h" - #include "runtime/function/render/render_camera.h" -#include "runtime/function/render/window_system.h" #include "runtime/function/render/render_system.h" - -#include "runtime/function/ui/ui_system.h" +#include "runtime/function/render/window_system.h" #include #include @@ -46,8 +43,7 @@ namespace Pilot EditorUI::EditorUI() { - Path& path_service = Path::getInstance(); - const auto& asset_folder = ConfigManager::getInstance().getAssetFolder(); + const auto& asset_folder = g_runtime_global_context.m_config_manager->getAssetFolder(); m_editor_ui_creator["TreeNodePush"] = [this](const std::string& name, void* value_ptr) -> void { static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings; bool node_state = false; @@ -205,8 +201,7 @@ namespace Pilot qua_ptr->z = val[2]; qua_ptr->w = val[3]; }; - m_editor_ui_creator["std::string"] = [this, &path_service, &asset_folder](const std::string& name, - void* value_ptr) -> void { + m_editor_ui_creator["std::string"] = [this, &asset_folder](const std::string& name, void* value_ptr) -> void { if (g_node_depth == -1) { std::string label = "##" + name; @@ -226,7 +221,7 @@ namespace Pilot std::filesystem::path value_path(value_str); if (value_path.is_absolute()) { - value_path = path_service.getRelativePath(asset_folder, value_path); + value_path = Path::getRelativePath(asset_folder, value_path); } value_str = value_path.generic_string(); if (value_str.size() >= 2 && value_str[0] == '.' && value_str[1] == '.') @@ -293,8 +288,9 @@ namespace Pilot ImGui::DockBuilderAddNode(main_docking_id, dock_flags); ImGui::DockBuilderSetNodePos(main_docking_id, - ImVec2(main_viewport->WorkPos.x, main_viewport->WorkPos.y + 18.0f)); - ImGui::DockBuilderSetNodeSize(main_docking_id, ImVec2((float)window_size[0], (float)window_size[1] - 18.0f)); + ImVec2(main_viewport->WorkPos.x, main_viewport->WorkPos.y + 18.0f)); + ImGui::DockBuilderSetNodeSize(main_docking_id, + ImVec2((float)window_size[0], (float)window_size[1] - 18.0f)); ImGuiID center = main_docking_id; ImGuiID left; @@ -323,12 +319,12 @@ namespace Pilot { if (ImGui::MenuItem("Reload Current Level")) { - WorldManager::getInstance().reloadCurrentLevel(); + g_runtime_global_context.m_world_manager->reloadCurrentLevel(); g_editor_global_context.m_scene_manager->onGObjectSelected(k_invalid_gobject_id); } if (ImGui::MenuItem("Save Current Level")) { - WorldManager::getInstance().saveCurrentLevel(); + g_runtime_global_context.m_world_manager->saveCurrentLevel(); } if (ImGui::MenuItem("Exit")) { @@ -354,7 +350,8 @@ namespace Pilot return; } - std::shared_ptr current_active_level = WorldManager::getInstance().getCurrentActiveLevel().lock(); + std::shared_ptr current_active_level = + g_runtime_global_context.m_world_manager->getCurrentActiveLevel().lock(); if (current_active_level == nullptr) return; @@ -614,11 +611,11 @@ namespace Pilot float indent_val = 0.0f; #if defined(__GNUC__) && defined(__MACH__) - float indent_scale =1.0f; + float indent_scale = 1.0f; #else // Not tested on Linux float x_scale, y_scale; glfwGetWindowContentScale(g_editor_global_context.m_window_system->getWindow(), &x_scale, &y_scale); - float indent_scale= fmaxf(1.0f, fmaxf(x_scale, y_scale)); + float indent_scale = fmaxf(1.0f, fmaxf(x_scale, y_scale)); #endif indent_val = g_editor_global_context.m_input_manager->getEngineWindowSize().x - 100.0f * indent_scale; @@ -643,8 +640,9 @@ namespace Pilot { g_is_editor_mode = true; g_editor_global_context.m_scene_manager->drawSelectedEntityAxis(); - InputSystem::getInstance().resetGameCommand(); - g_editor_global_context.m_render_system->getRenderCamera()->setMainViewMatrix(g_editor_global_context.m_scene_manager->getEditorCamera()->getViewMatrix()); + g_runtime_global_context.m_input_system->resetGameCommand(); + g_editor_global_context.m_render_system->getRenderCamera()->setMainViewMatrix( + g_editor_global_context.m_scene_manager->getEditorCamera()->getViewMatrix()); } } @@ -658,8 +656,9 @@ namespace Pilot } else { - ImGui::TextColored( - ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Current editor camera move speed: [%f]", g_editor_global_context.m_input_manager->getCameraSpeed()); + ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), + "Current editor camera move speed: [%f]", + g_editor_global_context.m_input_manager->getCameraSpeed()); } auto menu_bar_rect = ImGui::GetCurrentWindow()->MenuBarRect(); @@ -678,12 +677,13 @@ namespace Pilot // Return value from ImGui::GetMainViewport()->DpiScal is always the same as first frame. // glfwGetMonitorContentScale and glfwSetWindowContentScaleCallback are more adaptive. float dpi_scale = main_viewport->DpiScale; - PilotEngine::getInstance().getRenderSystem()->updateEngineContentViewport(new_window_pos.x * dpi_scale, - new_window_pos.y * dpi_scale, - new_window_size.x * dpi_scale, - new_window_size.y * dpi_scale); + g_runtime_global_context.m_render_system->updateEngineContentViewport(new_window_pos.x * dpi_scale, + new_window_pos.y * dpi_scale, + new_window_size.x * dpi_scale, + new_window_size.y * dpi_scale); #else - PilotEngine::getInstance().getRenderSystem()->updateEngineContentViewport(new_window_pos.x, new_window_pos.y, new_window_size.x, new_window_size.y); + g_runtime_global_context.m_render_system->updateEngineContentViewport( + new_window_pos.x, new_window_pos.y, new_window_size.x, new_window_size.y); #endif g_editor_global_context.m_input_manager->setEngineWindowPos(new_window_pos); g_editor_global_context.m_input_manager->setEngineWindowSize(new_window_size); @@ -755,7 +755,7 @@ namespace Pilot if (node->m_file_type != "object") return; - std::shared_ptr level = WorldManager::getInstance().getCurrentActiveLevel().lock(); + std::shared_ptr level = g_runtime_global_context.m_world_manager->getCurrentActiveLevel().lock(); if (level == nullptr) return; @@ -763,9 +763,9 @@ namespace Pilot ObjectInstanceRes new_object_instance_res; new_object_instance_res.m_name = - "New_" + Path::getInstance().getFilePureName(node->m_file_name) + "_" + std::to_string(new_object_index); + "New_" + Path::getFilePureName(node->m_file_name) + "_" + std::to_string(new_object_index); new_object_instance_res.m_definition = - AssetManager::getInstance().getFullPath(node->m_file_path).generic_string(); + g_runtime_global_context.m_asset_manager->getFullPath(node->m_file_path).generic_string(); size_t new_gobject_id = level->createObject(new_object_instance_res); if (new_gobject_id != k_invalid_gobject_id) @@ -788,8 +788,11 @@ namespace Pilot windowContentScaleUpdate(fmaxf(x_scale, y_scale)); } - void EditorUI::initialize(WindowUIInitInfo init_info) + void EditorUI::initialize(WindowUIInitInfo init_info) { + std::shared_ptr config_manager = g_runtime_global_context.m_config_manager; + ASSERT(config_manager); + // create imgui context IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -799,44 +802,44 @@ namespace Pilot glfwGetWindowContentScale(init_info.window_system->getWindow(), &x_scale, &y_scale); float content_scale = fmaxf(1.0f, fmaxf(x_scale, y_scale)); windowContentScaleUpdate(content_scale); - glfwSetWindowContentScaleCallback(init_info.window_system->getWindow(), windowContentScaleCallback); - - // load font for imgui - ImGuiIO& io = ImGui::GetIO(); + glfwSetWindowContentScaleCallback(init_info.window_system->getWindow(), windowContentScaleCallback); + + // load font for imgui + ImGuiIO& io = ImGui::GetIO(); io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; io.ConfigDockingAlwaysTabBar = true; io.ConfigWindowsMoveFromTitleBarOnly = true; - io.Fonts->AddFontFromFileTTF(ConfigManager::getInstance().getEditorFontPath().generic_string().data(), - content_scale * 16, - nullptr, - nullptr); + io.Fonts->AddFontFromFileTTF( + config_manager->getEditorFontPath().generic_string().data(), content_scale * 16, nullptr, nullptr); io.Fonts->Build(); - ImGuiStyle& style = ImGui::GetStyle(); + ImGuiStyle& style = ImGui::GetStyle(); style.WindowPadding = ImVec2(1.0, 0); style.FramePadding = ImVec2(14.0, 2.0f); style.ChildBorderSize = 0.0f; style.FrameRounding = 5.0f; - style.FrameBorderSize = 1.5f; - - // set imgui color style - setUIColorStyle(); - + style.FrameBorderSize = 1.5f; + + // set imgui color style + setUIColorStyle(); + // setup window icon - GLFWimage window_icon[2]; - std::string big_icon_path_string = ConfigManager::getInstance().getEditorBigIconPath().generic_string(); - std::string small_icon_path_string = ConfigManager::getInstance().getEditorSmallIconPath().generic_string(); - window_icon[0].pixels = stbi_load(big_icon_path_string.data(), &window_icon[0].width, &window_icon[0].height, 0, 4); - window_icon[1].pixels = stbi_load(small_icon_path_string.data(), &window_icon[1].width, &window_icon[1].height, 0, 4); + GLFWimage window_icon[2]; + std::string big_icon_path_string = config_manager->getEditorBigIconPath().generic_string(); + std::string small_icon_path_string = config_manager->getEditorSmallIconPath().generic_string(); + window_icon[0].pixels = + stbi_load(big_icon_path_string.data(), &window_icon[0].width, &window_icon[0].height, 0, 4); + window_icon[1].pixels = + stbi_load(small_icon_path_string.data(), &window_icon[1].width, &window_icon[1].height, 0, 4); glfwSetWindowIcon(init_info.window_system->getWindow(), 2, window_icon); stbi_image_free(window_icon[0].pixels); - stbi_image_free(window_icon[1].pixels); - - // initialize imgui vulkan render backend - init_info.render_system->initializeUIRenderBackend(this); - } - - void EditorUI::setUIColorStyle() + stbi_image_free(window_icon[1].pixels); + + // initialize imgui vulkan render backend + init_info.render_system->initializeUIRenderBackend(this); + } + + void EditorUI::setUIColorStyle() { ImGuiStyle* style = &ImGui::GetStyle(); ImVec4* colors = style->Colors; @@ -895,14 +898,11 @@ namespace Pilot colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); - colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); - } - - void EditorUI::preRender() - { - showEditorUI(); + colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); } + void EditorUI::preRender() { showEditorUI(); } + void DrawVecControl(const std::string& label, Pilot::Vector3& values, float resetValue, float columnWidth) { ImGui::PushID(label.c_str()); diff --git a/engine/source/editor/source/main.cpp b/engine/source/editor/source/main.cpp index 2da9a4510..175e5bcb4 100644 --- a/engine/source/editor/source/main.cpp +++ b/engine/source/editor/source/main.cpp @@ -3,9 +3,10 @@ #include #include -#include "editor/include/editor.h" #include "runtime/engine.h" +#include "editor/include/editor.h" + // https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html #define PILOT_XSTR(s) PILOT_STR(s) #define PILOT_STR(s) #s @@ -18,17 +19,20 @@ int main(int argc, char** argv) params.m_root_folder = pilot_root_folder; params.m_config_file_path = pilot_root_folder / "PilotEditor.ini"; - Pilot::PilotEngine::getInstance().startEngine(params); - Pilot::PilotEngine::getInstance().initialize(); + Pilot::PilotEngine* engine = new Pilot::PilotEngine(); + + engine->startEngine(params); + engine->initialize(); - Pilot::PilotEditor::getInstance().initialize(&(Pilot::PilotEngine::getInstance())); + Pilot::PilotEditor* editor = new Pilot::PilotEditor(); + editor->initialize(engine); - Pilot::PilotEditor::getInstance().run(); + editor->run(); - Pilot::PilotEditor::getInstance().clear(); + editor->clear(); - Pilot::PilotEngine::getInstance().clear(); - Pilot::PilotEngine::getInstance().shutdownEngine(); + engine->clear(); + engine->shutdownEngine(); return 0; } diff --git a/engine/source/runtime/core/base/macro.h b/engine/source/runtime/core/base/macro.h index c1a4b2128..c3c6bc5f1 100644 --- a/engine/source/runtime/core/base/macro.h +++ b/engine/source/runtime/core/base/macro.h @@ -1,13 +1,14 @@ #pragma once -#include "runtime/core/base/public_singleton.h" #include "runtime/core/log/log_system.h" +#include "runtime/function/global/global_context.h" + #include #include #define LOG_HELPER(LOG_LEVEL, ...) \ - LogSystem::getInstance().log(LOG_LEVEL, "[" + std::string(__FUNCTION__) + "] " + __VA_ARGS__); + g_runtime_global_context.m_logger_system->log(LOG_LEVEL, "[" + std::string(__FUNCTION__) + "] " + __VA_ARGS__); #define LOG_DEBUG(...) LOG_HELPER(LogSystem::LogLevel::debug, __VA_ARGS__); diff --git a/engine/source/runtime/core/base/public_singleton.h b/engine/source/runtime/core/base/public_singleton.h deleted file mode 100644 index a93230d34..000000000 --- a/engine/source/runtime/core/base/public_singleton.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include - -namespace Pilot -{ - - template - class PublicSingleton - { - protected: - PublicSingleton() = default; - - public: - static T& getInstance() noexcept(std::is_nothrow_constructible::value) - { - static T instance; - return instance; - } - virtual ~PublicSingleton() noexcept = default; - PublicSingleton(const PublicSingleton&) = delete; - PublicSingleton& operator=(const PublicSingleton&) = delete; - }; -} // namespace Pilot diff --git a/engine/source/runtime/core/log/log_system.h b/engine/source/runtime/core/log/log_system.h index 356520195..a2a2ae903 100644 --- a/engine/source/runtime/core/log/log_system.h +++ b/engine/source/runtime/core/log/log_system.h @@ -1,7 +1,5 @@ #pragma once -#include "runtime/core/base/public_singleton.h" - #include #include @@ -10,7 +8,7 @@ namespace Pilot { - class LogSystem final : public PublicSingleton + class LogSystem final { public: enum class LogLevel : uint8_t diff --git a/engine/source/runtime/engine.cpp b/engine/source/runtime/engine.cpp index 54bc5e15c..76eef5c20 100644 --- a/engine/source/runtime/engine.cpp +++ b/engine/source/runtime/engine.cpp @@ -3,48 +3,24 @@ #include "runtime/core/base/macro.h" #include "runtime/core/meta/reflection/reflection_register.h" -#include "runtime/resource/asset_manager/asset_manager.h" -#include "runtime/resource/config_manager/config_manager.h" - #include "runtime/function/framework/world/world_manager.h" +#include "runtime/function/global/global_context.h" #include "runtime/function/input/input_system.h" - #include "runtime/function/render/render_system.h" #include "runtime/function/render/window_system.h" -#include "runtime/function/ui/ui_system.h" - namespace Pilot { bool g_is_editor_mode {false}; std::unordered_set g_editor_tick_component_types {}; - PilotEngine::PilotEngine() - { - m_window_system = std::make_shared(); - m_render_system = std::make_shared(); - } - void PilotEngine::startEngine(const EngineInitParams& param) { - Reflection::TypeMetaRegister::Register(); - - ConfigManager::getInstance().initialize(param); - AssetManager::getInstance().initialize(); - PUIManager::getInstance().initialize(); - InputSystem::getInstance().initialize(); - - WorldManager::getInstance().initialize(); + m_init_params = param; - WindowCreateInfo window_create_info; - m_window_system->initialize(window_create_info); - - RenderSystemInitInfo render_init_info; - render_init_info.window_system = m_window_system; - m_render_system->initialize(render_init_info); + Reflection::TypeMetaRegister::Register(); - MeshComponent::m_swap_context = &(m_render_system->getSwapContext()); - CameraComponent::m_render_camera = &(*m_render_system->getRenderCamera()); + g_runtime_global_context.startSystems(param); LOG_INFO("engine start"); } @@ -53,20 +29,18 @@ namespace Pilot { LOG_INFO("engine shutdown"); - WorldManager::getInstance().clear(); - PUIManager::getInstance().clear(); - AssetManager::getInstance().clear(); - ConfigManager::getInstance().clear(); + g_runtime_global_context.shutdownSystems(); Reflection::TypeMetaRegister::Unregister(); } void PilotEngine::initialize() {} void PilotEngine::clear() {} + void PilotEngine::run() { float delta_time; - while (!m_window_system->shouldClose()) + while (!g_runtime_global_context.m_window_system->shouldClose()) { delta_time = getDeltaTime(); @@ -75,13 +49,14 @@ namespace Pilot // single thread // exchange data between logic and render contexts - m_render_system->swapLogicRenderData(); + g_runtime_global_context.m_render_system->swapLogicRenderData(); rendererTick(); - m_window_system->pollEvents(); + g_runtime_global_context.m_window_system->pollEvents(); - m_window_system->setTile(std::string("Pilot - " + std::to_string(getFPS()) + " FPS").c_str()); + g_runtime_global_context.m_window_system->setTile( + std::string("Pilot - " + std::to_string(getFPS()) + " FPS").c_str()); } } @@ -107,27 +82,27 @@ namespace Pilot // single thread // exchange data between logic and render contexts - m_render_system->swapLogicRenderData(); + g_runtime_global_context.m_render_system->swapLogicRenderData(); rendererTick(); - m_window_system->pollEvents(); + g_runtime_global_context.m_window_system->pollEvents(); - m_window_system->setTile(std::string("Pilot - " + std::to_string(getFPS()) + " FPS").c_str()); + g_runtime_global_context.m_window_system->setTile(std::string("Pilot - " + std::to_string(getFPS()) + " FPS").c_str()); - return !m_window_system->shouldClose(); + const bool should_window_close = g_runtime_global_context.m_window_system->shouldClose(); + return !should_window_close; } void PilotEngine::logicalTick(float delta_time) { - WorldManager::getInstance().tick(delta_time); - InputSystem::getInstance().tick(); - // PhysicsSystem::getInstance().tick(delta_time); + g_runtime_global_context.m_world_manager->tick(delta_time); + g_runtime_global_context.m_input_system->tick(); } bool PilotEngine::rendererTick() { - m_render_system->tick(); + g_runtime_global_context.m_render_system->tick(); return true; } diff --git a/engine/source/runtime/engine.h b/engine/source/runtime/engine.h index abd61a4cf..eb16f0fdb 100644 --- a/engine/source/runtime/engine.h +++ b/engine/source/runtime/engine.h @@ -1,7 +1,5 @@ #pragma once -#include "runtime/core/base/public_singleton.h" - #include #include #include @@ -9,10 +7,7 @@ namespace Pilot { - class WindowSystem; - class RenderSystem; - - extern bool g_is_editor_mode; + extern bool g_is_editor_mode; extern std::unordered_set g_editor_tick_component_types; struct EngineInitParams @@ -21,26 +16,26 @@ namespace Pilot std::filesystem::path m_config_file_path; }; - class PilotEngine : public PublicSingleton + class PilotEngine { - friend class PublicSingleton; friend class PilotEditor; static const float k_fps_alpha; - protected: - PilotEngine(); + public: + void startEngine(const EngineInitParams& param); + void shutdownEngine(); - bool m_is_quit {false}; - std::chrono::steady_clock::time_point m_last_tick_time_point {std::chrono::steady_clock::now()}; + void initialize(); + void clear(); - std::shared_ptr m_window_system; - std::shared_ptr m_render_system; + bool isQuit() const { return m_is_quit; } + void run(); + bool tickOneFrame(float delta_time); - float m_average_duration {0.f}; - int m_frame_count {0}; - int m_fps {0}; + int getFPS() const { return m_fps; } + protected: void logicalTick(float delta_time); bool rendererTick(); @@ -51,21 +46,16 @@ namespace Pilot */ float getDeltaTime(); - public: - void startEngine(const EngineInitParams& param); - void shutdownEngine(); - - void initialize(); - void clear(); + protected: + EngineInitParams m_init_params; - bool isQuit() const { return m_is_quit; } - void run(); - bool tickOneFrame(float delta_time); + bool m_is_quit {false}; - int getFPS() const { return m_fps; } + std::chrono::steady_clock::time_point m_last_tick_time_point {std::chrono::steady_clock::now()}; - std::shared_ptr getWindowSystem() const { return m_window_system; } - std::shared_ptr getRenderSystem() const { return m_render_system; } + float m_average_duration {0.f}; + int m_frame_count {0}; + int m_fps {0}; }; } // namespace Pilot diff --git a/engine/source/runtime/function/animation/animation_loader.cpp b/engine/source/runtime/function/animation/animation_loader.cpp index 5a06d19b2..9090bc874 100644 --- a/engine/source/runtime/function/animation/animation_loader.cpp +++ b/engine/source/runtime/function/animation/animation_loader.cpp @@ -6,6 +6,7 @@ #include "runtime/resource/res_type/data/skeleton_mask.h" #include "runtime/function/animation/utilities.h" +#include "runtime/function/global/global_context.h" #include "_generated/serializer/all_serializer.h" @@ -72,37 +73,29 @@ namespace Pilot std::shared_ptr AnimationLoader::loadAnimationClipData(std::string animation_clip_url) { - AssetManager& asset_manager = AssetManager::getInstance(); - AnimationAsset animation_clip; - asset_manager.loadAsset(animation_clip_url, animation_clip); + g_runtime_global_context.m_asset_manager->loadAsset(animation_clip_url, animation_clip); return std::make_shared(animation_clip.clip_data); } std::shared_ptr AnimationLoader::loadSkeletonData(std::string skeleton_data_url) { - AssetManager& asset_manager = AssetManager::getInstance(); - SkeletonData data; - asset_manager.loadAsset(skeleton_data_url, data); + g_runtime_global_context.m_asset_manager->loadAsset(skeleton_data_url, data); return std::make_shared(data); } std::shared_ptr AnimationLoader::loadAnimSkelMap(std::string anim_skel_map_url) { - AssetManager& asset_manager = AssetManager::getInstance(); - AnimSkelMap data; - asset_manager.loadAsset(anim_skel_map_url, data); + g_runtime_global_context.m_asset_manager->loadAsset(anim_skel_map_url, data); return std::make_shared(data); } std::shared_ptr AnimationLoader::loadSkeletonMask(std::string skeleton_mask_file_url) { - AssetManager& asset_manager = AssetManager::getInstance(); - BoneBlendMask data; - asset_manager.loadAsset(skeleton_mask_file_url, data); + g_runtime_global_context.m_asset_manager->loadAsset(skeleton_mask_file_url, data); return std::make_shared(data); } diff --git a/engine/source/runtime/function/character/character.cpp b/engine/source/runtime/function/character/character.cpp index caf775513..3c8c9c7da 100644 --- a/engine/source/runtime/function/character/character.cpp +++ b/engine/source/runtime/function/character/character.cpp @@ -37,7 +37,7 @@ namespace Pilot } } - void Character::tick() + void Character::tick(float delta_time) { if (m_character_object == nullptr) return; @@ -65,16 +65,14 @@ namespace Pilot const Vector3& new_position = motor_component->getTargetPosition(); - const int fps = PilotEngine::getInstance().getFPS(); - if (fps == 0) - return; + m_position = new_position; - float blend_ratio = std::max(1.f, motor_component->getSpeedRatio()); + //float blend_ratio = std::max(1.f, motor_component->getSpeedRatio()); - float frame_length = 1.f / static_cast(fps) * blend_ratio; - m_position = - (m_position * (k_camera_blend_time - frame_length) + new_position * frame_length) / k_camera_blend_time; - m_position = - (m_position * (k_camera_blend_time - frame_length) + new_position * frame_length) / k_camera_blend_time; + //float frame_length = delta_time * blend_ratio; + //m_position = + // (m_position * (k_camera_blend_time - frame_length) + new_position * frame_length) / k_camera_blend_time; + //m_position = + // (m_position * (k_camera_blend_time - frame_length) + new_position * frame_length) / k_camera_blend_time; } } // namespace Pilot \ No newline at end of file diff --git a/engine/source/runtime/function/character/character.h b/engine/source/runtime/function/character/character.h index 4f8f4094b..40830fcb8 100644 --- a/engine/source/runtime/function/character/character.h +++ b/engine/source/runtime/function/character/character.h @@ -24,7 +24,7 @@ namespace Pilot const Vector3& getPosition() const { return m_position; } const Quaternion& getRotation() const { return m_rotation; } - void tick(); + void tick(float delta_time); private: Vector3 m_position; diff --git a/engine/source/runtime/function/controller/character_controller.cpp b/engine/source/runtime/function/controller/character_controller.cpp index c1ee28017..e67e3ea81 100644 --- a/engine/source/runtime/function/controller/character_controller.cpp +++ b/engine/source/runtime/function/controller/character_controller.cpp @@ -2,13 +2,14 @@ #include "runtime/function/framework/component/motor/motor_component.h" #include "runtime/function/physics/physics_system.h" +#include "runtime/function/global/global_context.h" namespace Pilot { Vector3 CharacterController::move(const Vector3& current_position, const Vector3& displacement) { Vector3 desired_position = current_position + displacement; - if (PhysicsSystem::getInstance().overlapByCapsule(desired_position, m_capsule)) + if (g_runtime_global_context.m_physics_system->overlapByCapsule(desired_position, m_capsule)) { desired_position = current_position; } diff --git a/engine/source/runtime/function/framework/component/camera/camera_component.cpp b/engine/source/runtime/function/framework/component/camera/camera_component.cpp index d79d534dc..f38cee36b 100644 --- a/engine/source/runtime/function/framework/component/camera/camera_component.cpp +++ b/engine/source/runtime/function/framework/component/camera/camera_component.cpp @@ -8,6 +8,7 @@ #include "runtime/function/framework/level/level.h" #include "runtime/function/framework/object/object.h" #include "runtime/function/framework/world/world_manager.h" +#include "runtime/function/global/global_context.h" #include "runtime/function/input/input_system.h" #include "runtime/function/render/render_camera.h" @@ -49,7 +50,7 @@ namespace Pilot if (!m_parent_object.lock()) return; - std::shared_ptr current_level = WorldManager::getInstance().getCurrentActiveLevel().lock(); + std::shared_ptr current_level = g_runtime_global_context.m_world_manager->getCurrentActiveLevel().lock(); std::shared_ptr current_character = current_level->getCurrentActiveCharacter().lock(); if (current_character == nullptr) return; @@ -74,15 +75,15 @@ namespace Pilot void CameraComponent::tickFirstPersonCamera(float delta_time) { - std::shared_ptr current_level = WorldManager::getInstance().getCurrentActiveLevel().lock(); + std::shared_ptr current_level = g_runtime_global_context.m_world_manager->getCurrentActiveLevel().lock(); std::shared_ptr current_character = current_level->getCurrentActiveCharacter().lock(); if (current_character == nullptr) return; Quaternion q_yaw, q_pitch; - q_yaw.fromAngleAxis(InputSystem::getInstance().m_cursor_delta_yaw, Vector3::UNIT_Z); - q_pitch.fromAngleAxis(InputSystem::getInstance().m_cursor_delta_pitch, m_left); + q_yaw.fromAngleAxis(g_runtime_global_context.m_input_system->m_cursor_delta_yaw, Vector3::UNIT_Z); + q_pitch.fromAngleAxis(g_runtime_global_context.m_input_system->m_cursor_delta_pitch, m_left); const float offset = static_cast(m_camera_res.m_parameter)->m_vertical_offset; Vector3 eye_pos = current_character->getPosition() + offset * Vector3::UNIT_Z; @@ -107,7 +108,7 @@ namespace Pilot void CameraComponent::tickThirdPersonCamera(float delta_time) { - std::shared_ptr current_level = WorldManager::getInstance().getCurrentActiveLevel().lock(); + std::shared_ptr current_level = g_runtime_global_context.m_world_manager->getCurrentActiveLevel().lock(); std::shared_ptr current_character = current_level->getCurrentActiveCharacter().lock(); if (current_character == nullptr) return; @@ -116,8 +117,8 @@ namespace Pilot Quaternion q_yaw, q_pitch; - q_yaw.fromAngleAxis(InputSystem::getInstance().m_cursor_delta_yaw, Vector3::UNIT_Z); - q_pitch.fromAngleAxis(InputSystem::getInstance().m_cursor_delta_pitch, Vector3::UNIT_X); + q_yaw.fromAngleAxis(g_runtime_global_context.m_input_system->m_cursor_delta_yaw, Vector3::UNIT_Z); + q_pitch.fromAngleAxis(g_runtime_global_context.m_input_system->m_cursor_delta_pitch, Vector3::UNIT_X); param->m_cursor_pitch = q_pitch * param->m_cursor_pitch; diff --git a/engine/source/runtime/function/framework/component/mesh/mesh_component.cpp b/engine/source/runtime/function/framework/component/mesh/mesh_component.cpp index 98f5c961b..6924b3175 100644 --- a/engine/source/runtime/function/framework/component/mesh/mesh_component.cpp +++ b/engine/source/runtime/function/framework/component/mesh/mesh_component.cpp @@ -1,10 +1,12 @@ +#include "runtime/function/framework/component/mesh/mesh_component.h" + #include "runtime/resource/asset_manager/asset_manager.h" #include "runtime/resource/res_type/data/material.h" #include "runtime/function/framework/component/animation/animation_component.h" -#include "runtime/function/framework/component/mesh/mesh_component.h" #include "runtime/function/framework/component/transform/transform_component.h" #include "runtime/function/framework/object/object.h" +#include "runtime/function/global/global_context.h" #include "runtime/function/render/render_swap_context.h" @@ -16,7 +18,8 @@ namespace Pilot { m_parent_object = parent_object; - AssetManager& asset_manager = AssetManager::getInstance(); + std::shared_ptr asset_manager = g_runtime_global_context.m_asset_manager; + ASSERT(asset_manager); m_raw_meshes.resize(m_mesh_res.m_sub_meshes.size()); @@ -24,25 +27,25 @@ namespace Pilot for (const SubMeshRes& sub_mesh : m_mesh_res.m_sub_meshes) { GameObjectPartDesc& meshComponent = m_raw_meshes[raw_mesh_count]; - meshComponent.mesh_desc.mesh_file = asset_manager.getFullPath(sub_mesh.m_obj_file_ref).generic_string(); + meshComponent.mesh_desc.mesh_file = asset_manager->getFullPath(sub_mesh.m_obj_file_ref).generic_string(); meshComponent.material_desc.with_texture = sub_mesh.m_material.empty() == false; if (meshComponent.material_desc.with_texture) { MaterialRes material_res; - asset_manager.loadAsset(sub_mesh.m_material, material_res); + asset_manager->loadAsset(sub_mesh.m_material, material_res); meshComponent.material_desc.baseColorTextureFile = - asset_manager.getFullPath(material_res.m_base_colour_texture_file).generic_string(); + asset_manager->getFullPath(material_res.m_base_colour_texture_file).generic_string(); meshComponent.material_desc.metallicRoughnessTextureFile = - asset_manager.getFullPath(material_res.m_metallic_roughness_texture_file).generic_string(); + asset_manager->getFullPath(material_res.m_metallic_roughness_texture_file).generic_string(); meshComponent.material_desc.normalTextureFile = - asset_manager.getFullPath(material_res.m_normal_texture_file).generic_string(); + asset_manager->getFullPath(material_res.m_normal_texture_file).generic_string(); meshComponent.material_desc.occlusionTextureFile = - asset_manager.getFullPath(material_res.m_occlusion_texture_file).generic_string(); + asset_manager->getFullPath(material_res.m_occlusion_texture_file).generic_string(); meshComponent.material_desc.emissiveTextureFile = - asset_manager.getFullPath(material_res.m_emissive_texture_file).generic_string(); + asset_manager->getFullPath(material_res.m_emissive_texture_file).generic_string(); } auto object_space_transform = sub_mesh.m_transform.getMatrix(); diff --git a/engine/source/runtime/function/framework/component/motor/motor_component.cpp b/engine/source/runtime/function/framework/component/motor/motor_component.cpp index 78a515d38..d5eec21b8 100644 --- a/engine/source/runtime/function/framework/component/motor/motor_component.cpp +++ b/engine/source/runtime/function/framework/component/motor/motor_component.cpp @@ -1,7 +1,6 @@ #include "runtime/function/framework/component/motor/motor_component.h" #include "runtime/core/base/macro.h" -#include "runtime/core/base/public_singleton.h" #include "runtime/function/character/character.h" #include "runtime/function/controller/character_controller.h" @@ -11,6 +10,7 @@ #include "runtime/function/framework/level/level.h" #include "runtime/function/framework/object/object.h" #include "runtime/function/framework/world/world_manager.h" +#include "runtime/function/global/global_context.h" #include "runtime/function/input/input_system.h" namespace Pilot @@ -53,7 +53,7 @@ namespace Pilot if (!m_parent_object.lock()) return; - std::shared_ptr current_level = WorldManager::getInstance().getCurrentActiveLevel().lock(); + std::shared_ptr current_level = g_runtime_global_context.m_world_manager->getCurrentActiveLevel().lock(); std::shared_ptr current_character = current_level->getCurrentActiveCharacter().lock(); if (current_character == nullptr) return; @@ -64,9 +64,9 @@ namespace Pilot TransformComponent* transform_component = m_parent_object.lock()->tryGetComponent("TransformComponent"); - Radian turn_angle_yaw = InputSystem::getInstance().m_cursor_delta_yaw; + Radian turn_angle_yaw = g_runtime_global_context.m_input_system->m_cursor_delta_yaw; - unsigned int command = InputSystem::getInstance().getGameCommand(); + unsigned int command = g_runtime_global_context.m_input_system->getGameCommand(); if (command >= (unsigned int)GameCommand::invalid) return; diff --git a/engine/source/runtime/function/framework/component/rigidbody/rigidbody_component.cpp b/engine/source/runtime/function/framework/component/rigidbody/rigidbody_component.cpp index 4d8c45a19..34b2b2758 100644 --- a/engine/source/runtime/function/framework/component/rigidbody/rigidbody_component.cpp +++ b/engine/source/runtime/function/framework/component/rigidbody/rigidbody_component.cpp @@ -1,11 +1,11 @@ #include "runtime/function/framework/component/rigidbody/rigidbody_component.h" #include "runtime/core/base/macro.h" -#include "runtime/core/base/public_singleton.h" #include "runtime/function/framework/component/transform/transform_component.h" #include "runtime/function/framework/object/object.h" #include "runtime/function/physics/physics_system.h" +#include "runtime/function/global/global_context.h" namespace Pilot { @@ -20,7 +20,7 @@ namespace Pilot return; } - m_physics_actor = PhysicsSystem::getInstance().createPhysicsActor( + m_physics_actor = g_runtime_global_context.m_physics_system->createPhysicsActor( parent_object, parent_transform->getTransformConst(), m_rigidbody_res); } @@ -28,7 +28,7 @@ namespace Pilot { if (m_physics_actor) { - PhysicsSystem::getInstance().removePhyicsActor(m_physics_actor); + g_runtime_global_context.m_physics_system->removePhyicsActor(m_physics_actor); m_physics_actor = nullptr; } } diff --git a/engine/source/runtime/function/framework/level/level.cpp b/engine/source/runtime/function/framework/level/level.cpp index e7a3df846..d6b11670c 100644 --- a/engine/source/runtime/function/framework/level/level.cpp +++ b/engine/source/runtime/function/framework/level/level.cpp @@ -31,7 +31,7 @@ namespace Pilot { gobject = std::make_shared(object_id); } - catch(const std::bad_alloc&) + catch (const std::bad_alloc&) { LOG_FATAL("cannot allocate memory for new gobject"); } @@ -56,7 +56,7 @@ namespace Pilot m_level_res_url = level_res_url; LevelRes level_res; - const bool is_load_success = AssetManager::getInstance().loadAsset(level_res_url, level_res); + const bool is_load_success = g_runtime_global_context.m_asset_manager->loadAsset(level_res_url, level_res); if (is_load_success == false) { return false; @@ -113,7 +113,7 @@ namespace Pilot } } - const bool is_save_success = AssetManager::getInstance().saveAsset(output_level_res, m_level_res_url); + const bool is_save_success = g_runtime_global_context.m_asset_manager->saveAsset(output_level_res, m_level_res_url); if (is_save_success == false) { @@ -144,7 +144,7 @@ namespace Pilot } if (m_current_active_character && g_is_editor_mode == false) { - m_current_active_character->tick(); + m_current_active_character->tick(delta_time); } } diff --git a/engine/source/runtime/function/framework/object/object.cpp b/engine/source/runtime/function/framework/object/object.cpp index 28ab6aee3..357fcf73b 100644 --- a/engine/source/runtime/function/framework/object/object.cpp +++ b/engine/source/runtime/function/framework/object/object.cpp @@ -1,12 +1,15 @@ #include "runtime/function/framework/object/object.h" -#include "runtime/core/meta/reflection/reflection.h" #include "runtime/engine.h" -#include "runtime/function/framework/component/component.h" -#include "runtime/function/framework/component/transform/transform_component.h" + +#include "runtime/core/meta/reflection/reflection.h" #include "runtime/resource/asset_manager/asset_manager.h" +#include "runtime/function/framework/component/component.h" +#include "runtime/function/framework/component/transform/transform_component.h" +#include "runtime/function/global/global_context.h" + #include #include @@ -79,7 +82,7 @@ namespace Pilot ObjectDefinitionRes definition_res; - const bool is_loaded_success = AssetManager::getInstance().loadAsset(m_definition_url, definition_res); + const bool is_loaded_success = g_runtime_global_context.m_asset_manager->loadAsset(m_definition_url, definition_res); if (!is_loaded_success) return false; diff --git a/engine/source/runtime/function/framework/world/world_manager.cpp b/engine/source/runtime/function/framework/world/world_manager.cpp index 518e96b5e..49af3f4d6 100644 --- a/engine/source/runtime/function/framework/world/world_manager.cpp +++ b/engine/source/runtime/function/framework/world/world_manager.cpp @@ -1,10 +1,13 @@ #include "runtime/function/framework/world/world_manager.h" #include "runtime/core/base/macro.h" -#include "runtime/function/framework/level/level.h" + #include "runtime/resource/asset_manager/asset_manager.h" #include "runtime/resource/config_manager/config_manager.h" +#include "runtime/function/framework/level/level.h" +#include "runtime/function/global/global_context.h" + #include "_generated/serializer/all_serializer.h" namespace Pilot @@ -14,7 +17,7 @@ namespace Pilot void WorldManager::initialize() { m_is_world_loaded = false; - m_current_world_url = ConfigManager::getInstance().getDefaultWorldUrl(); + m_current_world_url = g_runtime_global_context.m_config_manager->getDefaultWorldUrl(); } void WorldManager::clear() @@ -53,7 +56,7 @@ namespace Pilot { LOG_INFO("loading world: {}", world_url); WorldRes world_res; - const bool is_world_load_success = AssetManager::getInstance().loadAsset(world_url, world_res); + const bool is_world_load_success = g_runtime_global_context.m_asset_manager->loadAsset(world_url, world_res); if (!is_world_load_success) { return false; diff --git a/engine/source/runtime/function/framework/world/world_manager.h b/engine/source/runtime/function/framework/world/world_manager.h index 0dd0b401c..06d53aa64 100644 --- a/engine/source/runtime/function/framework/world/world_manager.h +++ b/engine/source/runtime/function/framework/world/world_manager.h @@ -1,6 +1,5 @@ #pragma once -#include "runtime/core/base/public_singleton.h" #include "runtime/resource/res_type/common/world.h" #include @@ -12,10 +11,8 @@ namespace Pilot /// Manage all game worlds, it should be support multiple worlds, including game world and editor world. /// Currently, the implement just supports one active world and one active level - class WorldManager : public PublicSingleton + class WorldManager { - friend class PublicSingleton; - public: virtual ~WorldManager(); @@ -28,9 +25,6 @@ namespace Pilot void tick(float delta_time); std::weak_ptr getCurrentActiveLevel() const { return m_current_active_level; } - protected: - WorldManager() = default; - private: bool loadWorld(const std::string& world_url); bool loadLevel(const std::string& level_url); diff --git a/engine/source/runtime/function/global/global_context.cpp b/engine/source/runtime/function/global/global_context.cpp new file mode 100644 index 000000000..5086864f7 --- /dev/null +++ b/engine/source/runtime/function/global/global_context.cpp @@ -0,0 +1,77 @@ +#include "runtime/function/global/global_context.h" + +#include "core/log/log_system.h" + +#include "runtime/engine.h" + +#include "runtime/platform/file_service/file_service.h" + +#include "runtime/resource/asset_manager/asset_manager.h" +#include "runtime/resource/config_manager/config_manager.h" + +#include "runtime/engine.h" +#include "runtime/function/framework/world/world_manager.h" +#include "runtime/function/input/input_system.h" +#include "runtime/function/physics/physics_system.h" +#include "runtime/function/render/render_system.h" +#include "runtime/function/render/window_system.h" + +namespace Pilot +{ + RuntimeGlobalContext g_runtime_global_context; + + void RuntimeGlobalContext::startSystems(const EngineInitParams& init_params) + { + m_config_manager = std::make_shared(); + m_config_manager->initialize(init_params); + + m_file_servcie = std::make_shared(); + + m_logger_system = std::make_shared(); + + m_asset_manager = std::make_shared(); + + m_world_manager = std::make_shared(); + m_world_manager->initialize(); + + m_physics_system = std::make_shared(); + + m_window_system = std::make_shared(); + WindowCreateInfo window_create_info; + m_window_system->initialize(window_create_info); + + m_input_system = std::make_shared(); + m_input_system->initialize(); + + m_render_system = std::make_shared(); + RenderSystemInitInfo render_init_info; + render_init_info.window_system = m_window_system; + m_render_system->initialize(render_init_info); + + MeshComponent::m_swap_context = &(m_render_system->getSwapContext()); + CameraComponent::m_render_camera = &(*m_render_system->getRenderCamera()); + } + + void RuntimeGlobalContext::shutdownSystems() + { + m_render_system.reset(); + + m_window_system.reset(); + + m_physics_system.reset(); + + m_scene_manager.reset(); + + m_world_manager.reset(); + + m_input_system.reset(); + + m_asset_manager.reset(); + + m_logger_system.reset(); + + m_file_servcie.reset(); + + m_config_manager.reset(); + } +} // namespace Pilot \ No newline at end of file diff --git a/engine/source/runtime/function/global/global_context.h b/engine/source/runtime/function/global/global_context.h new file mode 100644 index 000000000..8c7f2c350 --- /dev/null +++ b/engine/source/runtime/function/global/global_context.h @@ -0,0 +1,43 @@ +#pragma once + +#include + +namespace Pilot +{ + class LogSystem; + class InputSystem; + class PhysicsSystem; + class FileService; + class AssetManager; + class ConfigManager; + class WorldManager; + class SceneManager; + class RenderSystem; + class WindowSystem; + + struct EngineInitParams; + + /// Manage the lifetime and creation/destruction order of all global system + class RuntimeGlobalContext + { + public: + // create all global systems and initialize these systems + void startSystems(const EngineInitParams& init_params); + // destroy all global systems + void shutdownSystems(); + + public: + std::shared_ptr m_logger_system; + std::shared_ptr m_input_system; + std::shared_ptr m_file_servcie; + std::shared_ptr m_asset_manager; + std::shared_ptr m_config_manager; + std::shared_ptr m_world_manager; + std::shared_ptr m_scene_manager; + std::shared_ptr m_physics_system; + std::shared_ptr m_window_system; + std::shared_ptr m_render_system; + }; + + extern RuntimeGlobalContext g_runtime_global_context; +} // namespace Pilot \ No newline at end of file diff --git a/engine/source/runtime/function/input/input_system.cpp b/engine/source/runtime/function/input/input_system.cpp index e9a84262f..b0f81b754 100644 --- a/engine/source/runtime/function/input/input_system.cpp +++ b/engine/source/runtime/function/input/input_system.cpp @@ -1,10 +1,12 @@ #include "runtime/function/input/input_system.h" -#include "runtime/engine.h" +#include "core/base/macro.h" -#include "runtime/function/render/window_system.h" -#include "runtime/function/render/render_system.h" +#include "runtime/engine.h" +#include "runtime/function/global/global_context.h" #include "runtime/function/render/render_camera.h" +#include "runtime/function/render/render_system.h" +#include "runtime/function/render/window_system.h" #include @@ -47,7 +49,7 @@ namespace Pilot m_game_command |= (unsigned int)GameCommand::squat; break; case GLFW_KEY_LEFT_ALT: { - std::shared_ptr window_system = PilotEngine::getInstance().getWindowSystem(); + std::shared_ptr window_system = g_runtime_global_context.m_window_system; window_system->setFocusMode(!window_system->getFocusMode()); } break; @@ -93,7 +95,7 @@ namespace Pilot void InputSystem::onCursorPos(double current_cursor_x, double current_cursor_y) { - if (PilotEngine::getInstance().getWindowSystem()->getFocusMode()) + if (g_runtime_global_context.m_window_system->getFocusMode()) { m_cursor_delta_x = m_last_cursor_x - current_cursor_x; m_cursor_delta_y = m_last_cursor_y - current_cursor_y; @@ -110,26 +112,27 @@ namespace Pilot void InputSystem::calculateCursorDeltaAngles() { - std::array window_size = PilotEngine::getInstance().getWindowSystem()->getWindowSize(); + std::array window_size = g_runtime_global_context.m_window_system->getWindowSize(); if (window_size[0] < 1 || window_size[1] < 1) { return; } - std::shared_ptr render_camera = PilotEngine::getInstance().getRenderSystem()->getRenderCamera(); - const Vector2& fov = render_camera->getFOV(); + std::shared_ptr render_camera = g_runtime_global_context.m_render_system->getRenderCamera(); + const Vector2& fov = render_camera->getFOV(); Radian cursor_delta_x(Math::degreesToRadians(m_cursor_delta_x)); Radian cursor_delta_y(Math::degreesToRadians(m_cursor_delta_y)); - m_cursor_delta_yaw = (cursor_delta_x / (float)window_size[0]) * fov.x; + m_cursor_delta_yaw = (cursor_delta_x / (float)window_size[0]) * fov.x; m_cursor_delta_pitch = -(cursor_delta_y / (float)window_size[1]) * fov.y; } void InputSystem::initialize() { - std::shared_ptr window_system = PilotEngine::getInstance().getWindowSystem(); + std::shared_ptr window_system = g_runtime_global_context.m_window_system; + ASSERT(window_system); window_system->registerOnKeyFunc(std::bind(&InputSystem::onKey, this, @@ -139,14 +142,14 @@ namespace Pilot std::placeholders::_4)); window_system->registerOnCursorPosFunc( std::bind(&InputSystem::onCursorPos, this, std::placeholders::_1, std::placeholders::_2)); - } - + } + void InputSystem::tick() { calculateCursorDeltaAngles(); clear(); - - std::shared_ptr window_system = PilotEngine::getInstance().getWindowSystem(); + + std::shared_ptr window_system = g_runtime_global_context.m_window_system; if (window_system->getFocusMode()) { m_game_command &= (k_complement_control_command ^ (unsigned int)GameCommand::invalid); diff --git a/engine/source/runtime/function/input/input_system.h b/engine/source/runtime/function/input/input_system.h index 0353263a8..f567a9af7 100644 --- a/engine/source/runtime/function/input/input_system.h +++ b/engine/source/runtime/function/input/input_system.h @@ -1,6 +1,5 @@ #pragma once -#include "runtime/core/base/public_singleton.h" #include "runtime/core/math/math.h" namespace Pilot @@ -20,9 +19,8 @@ namespace Pilot extern unsigned int k_complement_control_command; - class InputSystem : public PublicSingleton + class InputSystem { - friend class PublicSingleton; public: void onKey(int key, int scancode, int action, int mods); @@ -41,9 +39,6 @@ namespace Pilot void resetGameCommand() { m_game_command = 0; } unsigned int getGameCommand() const { return m_game_command; } - protected: - InputSystem() = default; - private: void onKeyInGameMode(int key, int scancode, int action, int mods); diff --git a/engine/source/runtime/function/physics/physics_system.h b/engine/source/runtime/function/physics/physics_system.h index 7e59cab7b..59faf767f 100644 --- a/engine/source/runtime/function/physics/physics_system.h +++ b/engine/source/runtime/function/physics/physics_system.h @@ -1,7 +1,7 @@ #pragma once -#include "runtime/core/base/public_singleton.h" #include "runtime/core/math/transform.h" + #include "runtime/function/physics/collision_detection.h" #include "runtime/function/physics/physics_actor.h" @@ -11,7 +11,7 @@ namespace Pilot { - class PhysicsSystem : public PublicSingleton + class PhysicsSystem { public: void tick(float delta_time); diff --git a/engine/source/runtime/function/render/render_resource_base.cpp b/engine/source/runtime/function/render/render_resource_base.cpp index e19967632..d27b6f6a2 100644 --- a/engine/source/runtime/function/render/render_resource_base.cpp +++ b/engine/source/runtime/function/render/render_resource_base.cpp @@ -6,6 +6,8 @@ #include "runtime/resource/config_manager/config_manager.h" #include "runtime/resource/res_type/data/mesh_data.h" +#include "runtime/function/global/global_context.h" + #define STB_IMAGE_IMPLEMENTATION #include @@ -20,11 +22,14 @@ namespace Pilot { std::shared_ptr RenderResourceBase::loadTextureHDR(std::string file, int desired_channels) { + std::shared_ptr asset_manager = g_runtime_global_context.m_asset_manager; + ASSERT(asset_manager); + std::shared_ptr texture = std::make_shared(); int iw, ih, n; - texture->m_pixels = stbi_loadf( - AssetManager::getInstance().getFullPath(file).generic_string().c_str(), &iw, &ih, &n, desired_channels); + texture->m_pixels = + stbi_loadf(asset_manager->getFullPath(file).generic_string().c_str(), &iw, &ih, &n, desired_channels); if (!texture->m_pixels) return nullptr; @@ -54,11 +59,13 @@ namespace Pilot std::shared_ptr RenderResourceBase::loadTexture(std::string file, bool is_srgb) { + std::shared_ptr asset_manager = g_runtime_global_context.m_asset_manager; + ASSERT(asset_manager); + std::shared_ptr texture = std::make_shared(); int iw, ih, n; - texture->m_pixels = - stbi_load(AssetManager::getInstance().getFullPath(file).generic_string().c_str(), &iw, &ih, &n, 4); + texture->m_pixels = stbi_load(asset_manager->getFullPath(file).generic_string().c_str(), &iw, &ih, &n, 4); if (!texture->m_pixels) return nullptr; @@ -77,6 +84,9 @@ namespace Pilot RenderMeshData RenderResourceBase::loadMeshData(const MeshSourceDesc& source, AxisAlignedBox& bounding_box) { + std::shared_ptr asset_manager = g_runtime_global_context.m_asset_manager; + ASSERT(asset_manager); + RenderMeshData ret; if (std::filesystem::path(source.mesh_file).extension() == ".obj") @@ -86,7 +96,7 @@ namespace Pilot else if (std::filesystem::path(source.mesh_file).extension() == ".json") { std::shared_ptr bind_data = std::make_shared(); - AssetManager::getInstance().loadAsset(source.mesh_file, *bind_data); + asset_manager->loadAsset(source.mesh_file, *bind_data); // vertex buffer size_t vertex_size = bind_data->vertex_buffer.size() * sizeof(MeshVertexDataDefinition); diff --git a/engine/source/runtime/function/render/render_system.cpp b/engine/source/runtime/function/render/render_system.cpp index 7c6f0b8a4..c86e6e3e4 100644 --- a/engine/source/runtime/function/render/render_system.cpp +++ b/engine/source/runtime/function/render/render_system.cpp @@ -1,17 +1,17 @@ #include "runtime/function/render/render_system.h" -#include "runtime/function/render/render_camera.h" -#include "runtime/function/render/render_resource_base.h" -#include "runtime/function/render/render_scene.h" -#include "runtime/function/render/window_system.h" + +#include "runtime/core/base/macro.h" #include "runtime/resource/asset_manager/asset_manager.h" #include "runtime/resource/config_manager/config_manager.h" +#include "runtime/function/render/render_camera.h" #include "runtime/function/render/render_pipeline.h" #include "runtime/function/render/render_resource.h" +#include "runtime/function/render/render_resource_base.h" +#include "runtime/function/render/render_scene.h" #include "runtime/function/render/rhi/vulkan/vulkan_rhi.h" - -#include "runtime/core/base/macro.h" +#include "runtime/function/render/window_system.h" namespace Pilot { @@ -19,6 +19,11 @@ namespace Pilot void RenderSystem::initialize(RenderSystemInitInfo init_info) { + std::shared_ptr config_manager = g_runtime_global_context.m_config_manager; + ASSERT(config_manager); + std::shared_ptr asset_manager = g_runtime_global_context.m_asset_manager; + ASSERT(asset_manager); + // render context initialize RHIInitInfo rhi_init_info; rhi_init_info.window_system = init_info.window_system; @@ -28,8 +33,8 @@ namespace Pilot // global rendering resource GlobalRenderingRes global_rendering_res; - const std::string& global_rendering_res_url = ConfigManager::getInstance().getGlobalRenderingResUrl(); - AssetManager::getInstance().loadAsset(global_rendering_res_url, global_rendering_res); + const std::string& global_rendering_res_url = config_manager->getGlobalRenderingResUrl(); + asset_manager->loadAsset(global_rendering_res_url, global_rendering_res); // upload ibl, color grading textures LevelResourceDesc level_resource_desc; @@ -181,6 +186,9 @@ namespace Pilot { RenderSwapData& swap_data = m_swap_context.getRenderSwapData(); + std::shared_ptr asset_manager = g_runtime_global_context.m_asset_manager; + ASSERT(asset_manager); + // TODO: update global resources if needed if (swap_data.level_resource_desc.has_value()) { @@ -247,13 +255,9 @@ namespace Pilot { // TODO: move to default material definition json file material_source = { - AssetManager::getInstance() - .getFullPath("asset/texture/default/albedo.jpg") - .generic_string(), - AssetManager::getInstance().getFullPath("asset/texture/default/mr.jpg").generic_string(), - AssetManager::getInstance() - .getFullPath("asset/texture/default/normal.jpg") - .generic_string(), + asset_manager->getFullPath("asset/texture/default/albedo.jpg").generic_string(), + asset_manager->getFullPath("asset/texture/default/mr.jpg").generic_string(), + asset_manager->getFullPath("asset/texture/default/normal.jpg").generic_string(), "", ""}; } diff --git a/engine/source/runtime/function/ui/ui_system.cpp b/engine/source/runtime/function/ui/ui_system.cpp deleted file mode 100644 index 3feb34af4..000000000 --- a/engine/source/runtime/function/ui/ui_system.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "runtime/function/ui/ui_system.h" - -int Pilot::PUIManager::update() { return 0; } - -int Pilot::PUIManager::initialize() { return 0; } - -int Pilot::PUIManager::clear() { return 0; } \ No newline at end of file diff --git a/engine/source/runtime/function/ui/ui_system.h b/engine/source/runtime/function/ui/ui_system.h deleted file mode 100644 index 7983f003f..000000000 --- a/engine/source/runtime/function/ui/ui_system.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "runtime/core/base/public_singleton.h" - -namespace Pilot -{ - class PUIManager final : public PublicSingleton - { - friend class PublicSingleton; - - public: - int initialize(); - int update(); - int clear(); - - protected: - PUIManager() = default; - }; -} // namespace Pilot diff --git a/engine/source/runtime/platform/file_service/file_service.h b/engine/source/runtime/platform/file_service/file_service.h index 34e5f503c..180b4a694 100644 --- a/engine/source/runtime/platform/file_service/file_service.h +++ b/engine/source/runtime/platform/file_service/file_service.h @@ -1,20 +1,13 @@ #pragma once -#include "runtime/core/base/public_singleton.h" - #include #include namespace Pilot { - class FileService : public PublicSingleton + class FileService { - friend class PublicSingleton; - public: std::vector getFiles(const std::filesystem::path& directory); - - protected: - FileService() = default; }; } // namespace Pilot \ No newline at end of file diff --git a/engine/source/runtime/platform/path/path.cpp b/engine/source/runtime/platform/path/path.cpp index 7d3773dd3..389c19f2d 100644 --- a/engine/source/runtime/platform/path/path.cpp +++ b/engine/source/runtime/platform/path/path.cpp @@ -20,14 +20,14 @@ namespace Pilot return segments; } - const tuple Path::getFileExtensions(const filesystem::path& file_path) const + const tuple Path::getFileExtensions(const filesystem::path& file_path) { return make_tuple(file_path.extension().generic_string(), file_path.stem().extension().generic_string(), file_path.stem().stem().extension().generic_string()); } - const string Path::getFilePureName(const string file_full_name) const + const string Path::getFilePureName(const string file_full_name) { string file_pure_name = file_full_name; auto pos = file_full_name.find_first_of('.'); diff --git a/engine/source/runtime/platform/path/path.h b/engine/source/runtime/platform/path/path.h index 243040b58..97d99be0f 100644 --- a/engine/source/runtime/platform/path/path.h +++ b/engine/source/runtime/platform/path/path.h @@ -1,7 +1,5 @@ #pragma once -#include "runtime/core/base/public_singleton.h" - #include #include #include @@ -9,7 +7,7 @@ namespace Pilot { - class Path : public PublicSingleton + class Path { public: static const std::filesystem::path getRelativePath(const std::filesystem::path& directory, @@ -17,9 +15,9 @@ namespace Pilot static const std::vector getPathSegments(const std::filesystem::path& file_path) ; - const std::tuple - getFileExtensions(const std::filesystem::path& file_path) const; + static const std::tuple + getFileExtensions(const std::filesystem::path& file_path); - const std::string getFilePureName(const std::string) const; + static const std::string getFilePureName(const std::string); }; } // namespace Pilot \ No newline at end of file diff --git a/engine/source/runtime/resource/asset_manager/asset_manager.cpp b/engine/source/runtime/resource/asset_manager/asset_manager.cpp index bf95ceeaf..1c15f9de1 100644 --- a/engine/source/runtime/resource/asset_manager/asset_manager.cpp +++ b/engine/source/runtime/resource/asset_manager/asset_manager.cpp @@ -2,14 +2,12 @@ #include "runtime/resource/config_manager/config_manager.h" +#include "runtime/function/global/global_context.h" + namespace Pilot { - void AssetManager::initialize() - { - } - std::filesystem::path AssetManager::getFullPath(const std::string& relative_path) const { - return ConfigManager::getInstance().getRootFolder() / relative_path; + return g_runtime_global_context.m_config_manager->getRootFolder() / relative_path; } } // namespace Pilot \ No newline at end of file diff --git a/engine/source/runtime/resource/asset_manager/asset_manager.h b/engine/source/runtime/resource/asset_manager/asset_manager.h index 2df8ff63b..d96e445ff 100644 --- a/engine/source/runtime/resource/asset_manager/asset_manager.h +++ b/engine/source/runtime/resource/asset_manager/asset_manager.h @@ -1,7 +1,6 @@ #pragma once #include "runtime/core/base/macro.h" -#include "runtime/core/base/public_singleton.h" #include "runtime/core/meta/serializer/serializer.h" #include @@ -14,7 +13,7 @@ namespace Pilot { - class AssetManager : public PublicSingleton + class AssetManager { public: template @@ -66,9 +65,6 @@ namespace Pilot return true; } - void initialize(); - void clear() {} - std::filesystem::path getFullPath(const std::string& relative_path) const; }; diff --git a/engine/source/runtime/resource/config_manager/config_manager.cpp b/engine/source/runtime/resource/config_manager/config_manager.cpp index ec3adf029..c002604df 100644 --- a/engine/source/runtime/resource/config_manager/config_manager.cpp +++ b/engine/source/runtime/resource/config_manager/config_manager.cpp @@ -1,5 +1,7 @@ #include "runtime/resource/config_manager/config_manager.h" +#include "runtime/engine.h" + #include #include diff --git a/engine/source/runtime/resource/config_manager/config_manager.h b/engine/source/runtime/resource/config_manager/config_manager.h index 9b719185e..38b98d640 100644 --- a/engine/source/runtime/resource/config_manager/config_manager.h +++ b/engine/source/runtime/resource/config_manager/config_manager.h @@ -1,32 +1,15 @@ #pragma once -#include "runtime/engine.h" - #include namespace Pilot { - class ConfigManager final : public PublicSingleton - { - friend class PublicSingleton; - - private: - std::filesystem::path m_root_folder; - std::filesystem::path m_asset_folder; - std::filesystem::path m_schema_folder; - std::filesystem::path m_editor_big_icon_path; - std::filesystem::path m_editor_small_icon_path; - std::filesystem::path m_editor_font_path; - - std::string m_default_world_url; - std::string m_global_rendering_res_url; - - protected: - ConfigManager() = default; + struct EngineInitParams; + class ConfigManager + { public: void initialize(const EngineInitParams& init_param); - void clear(); const std::filesystem::path& getRootFolder() const; @@ -38,5 +21,16 @@ namespace Pilot const std::string& getDefaultWorldUrl() const; const std::string& getGlobalRenderingResUrl() const; + + private: + std::filesystem::path m_root_folder; + std::filesystem::path m_asset_folder; + std::filesystem::path m_schema_folder; + std::filesystem::path m_editor_big_icon_path; + std::filesystem::path m_editor_small_icon_path; + std::filesystem::path m_editor_font_path; + + std::string m_default_world_url; + std::string m_global_rendering_res_url; }; } // namespace Pilot