#include "Engine.h" #include "src/Core/Physics/PhysicsManager.h" #include #include "src/Core/Input.h" #include "src/Core/FileSystem.h" #include "src/Scripting/ScriptingEngine.h" #include "src/Audio/AudioManager.h" #include "src/Rendering/Renderer.h" #include "src/Rendering/Renderer2D.h" #include #include namespace Nuake { Ref Engine::s_CurrentProject; Ref Engine::s_CurrentWindow; bool Engine::s_IsPlayMode = false; float Engine::s_LastFrameTime = 0.0f; float Engine::s_FixedUpdateRate = 1.0f / 90.0f; float Engine::s_FixedUpdateDifference = 0.f; float Engine::s_Time = 0.f; Timestep Engine::s_TimeStep = 0.f; void Engine::Init() { Logger::Log("Nuake initializing"); AudioManager::Get().Initialize(); Logger::Log("Audio engine initialized"); AudioManager::Get().PlayTTS("Nuake Engine debug"); PhysicsManager::Get().Init(); Logger::Log("Physics initialized"); // Creates the window s_CurrentWindow = Window::Get(); Logger::Log("Window initialized"); Input::Init(); Renderer2D::Init(); Logger::Log("2D renderer initialized"); Logger::Log("Engine initialized successfully!"); } void Engine::Tick() { s_Time = (float)glfwGetTime(); s_TimeStep = s_Time - s_LastFrameTime; s_LastFrameTime = s_Time; s_TimeStep = std::min((float)s_TimeStep, 0.5f); // Dont update if no scene is loaded. if (s_CurrentWindow->GetScene()) { s_CurrentWindow->Update(s_TimeStep); // Play mode update all the entities, Editor does not. if (Engine::IsPlayMode()) { s_FixedUpdateDifference += s_TimeStep; // Fixed update while (s_FixedUpdateDifference >= s_FixedUpdateRate) { s_CurrentWindow->FixedUpdate(s_FixedUpdateDifference); s_FixedUpdateDifference -= s_FixedUpdateRate; } } else { GetCurrentScene()->EditorUpdate(s_TimeStep); } } Input::Update(); } void Engine::EnterPlayMode() { // Dont trigger init if already in player mode. if (IsPlayMode()) { Logger::Log("Cannot enter play mode if is already in play mode.", WARNING); return; } if (GetCurrentScene()->OnInit()) { s_IsPlayMode = true; } else { Logger::Log("Cannot enter play mode. Scene OnInit failed", CRITICAL); GetCurrentScene()->OnExit(); } } void Engine::ExitPlayMode() { // Dont trigger exit if already not in play mode. if (IsPlayMode()) { GetCurrentScene()->OnExit(); Input::ShowMouse(); s_IsPlayMode = false; } } void Engine::Draw() { Nuake::RenderCommand::Clear(); // Start imgui frame ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); // Draw scene Window::Get()->Draw(); } void Engine::EndDraw() { Window::Get()->EndDraw(); } void Engine::Close() { glfwTerminate(); } Ref Engine::GetCurrentScene() { return s_CurrentWindow->GetScene(); } bool Engine::LoadScene(Ref scene) { return s_CurrentWindow->SetScene(scene); } Ref Engine::GetProject() { return s_CurrentProject; } bool Engine::LoadProject(Ref project) { s_CurrentProject = project; if (!Engine::LoadScene(s_CurrentProject->DefaultScene)) { return false; } FileSystem::SetRootDirectory(FileSystem::GetParentPath(project->FullPath)); return true; } Ref Engine::GetCurrentWindow() { return s_CurrentWindow; } }