From 1e34f0e67b8a98dd330eef65fb6453dedbaa00f0 Mon Sep 17 00:00:00 2001 From: coding-agent Date: Mon, 9 Nov 2026 17:18:33 +0000 Subject: [PATCH] refactor: rename WindowManager to Application; move world ownership up WindowManager.{h,cpp} -> Application.{h,cpp} The Application class now owns the long-lived world (Scene, SystemGraph, GameInterface) and is the single setup point. This removes the per-view duplication where each GameView/PlayView loaded the binary and ran the simulation independently, which double-simulated with two windows open. Changes: - WindowManager -> Application (class, files, includes, CMake) - Application owns OScene scene_ / OSystemGraph graph_ (OwningPtr) and GameInterface* gameInterface (raw, deleted in dtor) - loadGame(graphics, path, name): single world-setup point (dlopen -> setupScene -> append 5 editor systems) - attachWorld(OScene, OSystemGraph): worldless hosts (benchmark) hand ownership in via move - Frame-loop fix: systemGraph->run() now runs ONCE per frame, before the window loop (was per-window -> double-simulated with 2 windows) - Editor/main.cpp + Benchmark/main.cpp rewired to the new API - Window.h/.cpp: PWindowManager back-pointer -> PApplication - 8 include paths updated to Window/Application.h Compile-verified: Application.cpp, Editor/main.cpp, Benchmark/main.cpp all pass g++ -std=c++23 -fsyntax-only against the full third-party include set. --- src/Benchmark/main.cpp | 21 +++++-- src/Editor/Asset/MaterialLoader.cpp | 2 +- src/Editor/main.cpp | 30 ++++----- src/Engine/Asset/AssetRegistry.cpp | 2 +- src/Engine/Asset/TextureAsset.cpp | 2 +- src/Engine/Material/Material.cpp | 2 +- src/Engine/Window/Application.cpp | 96 +++++++++++++++++++++++++++++ src/Engine/Window/Application.h | 59 ++++++++++++++++++ src/Engine/Window/CMakeLists.txt | 6 +- src/Engine/Window/Window.cpp | 4 +- src/Engine/Window/Window.h | 6 +- src/Engine/Window/WindowManager.cpp | 28 --------- src/Engine/Window/WindowManager.h | 22 ------- 13 files changed, 194 insertions(+), 86 deletions(-) create mode 100644 src/Engine/Window/Application.cpp create mode 100644 src/Engine/Window/Application.h delete mode 100644 src/Engine/Window/WindowManager.cpp delete mode 100644 src/Engine/Window/WindowManager.h diff --git a/src/Benchmark/main.cpp b/src/Benchmark/main.cpp index f4c459a..047b160 100644 --- a/src/Benchmark/main.cpp +++ b/src/Benchmark/main.cpp @@ -3,7 +3,9 @@ #include "Graphics/StaticMeshVertexData.h" #include "Graphics/Vulkan/Graphics.h" #include "PlayView.h" -#include "Window/WindowManager.h" +#include "Scene/Scene.h" +#include "System/SystemGraph.h" +#include "Window/Application.h" #include #include @@ -32,7 +34,14 @@ int main(int argc, char** argv) { vd->init(graphics); getGlobals().useRayTracing = true; - OWindowManager windowManager = new WindowManager(); + // The benchmark is a worldless host: it builds a plain Scene + SystemGraph + // (no game binary) and hands ownership to the Application, which then steps + // it once per frame and lets the views render it. The view renders the + // non-owning application->scene() handle. + OApplication application = new Application(); + OScene scene = new Scene(graphics); + OSystemGraph systemGraph = new SystemGraph(); + application->attachWorld(std::move(scene), std::move(systemGraph)); AssetRegistry::init("Assets", graphics); vd->commitMeshes(); WindowCreateInfo mainWindowInfo = { @@ -41,7 +50,7 @@ int main(int argc, char** argv) { .title = "Benchmark", .preferredFormat = Gfx::SE_FORMAT_B8G8R8A8_SRGB, }; - auto window = windowManager->addWindow(graphics, mainWindowInfo); + auto window = application->addWindow(graphics, mainWindowInfo); ViewportCreateInfo sceneViewInfo = { .dimensions = { @@ -49,11 +58,11 @@ int main(int argc, char** argv) { .offset = {0, 0}, }, }; - OGameView sceneView = new PlayView(graphics, window, new Scene(graphics), sceneViewInfo, useDepthCulling); + OGameView sceneView = new PlayView(graphics, window, application->scene(), sceneViewInfo, useDepthCulling); sceneView->setFocused(); - while (windowManager->isActive()) { - windowManager->render(); + while (application->isActive()) { + application->render(); } graphics->waitDeviceIdle(); vd->destroy(); diff --git a/src/Editor/Asset/MaterialLoader.cpp b/src/Editor/Asset/MaterialLoader.cpp index f2b93f7..98807c5 100644 --- a/src/Editor/Asset/MaterialLoader.cpp +++ b/src/Editor/Asset/MaterialLoader.cpp @@ -6,7 +6,7 @@ #include "Graphics/Shader.h" #include "Material/Material.h" #include "Material/ShaderExpression.h" -#include "Window/WindowManager.h" +#include "Window/Application.h" #include #include #include diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index e4696ac..db0e1e9 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -16,7 +16,7 @@ #include "System/FluidUpdater.h" #include "System/LightGather.h" #include "System/MeshUpdater.h" -#include "Window/WindowManager.h" +#include "Window/Application.h" #include "Window/InspectorView.h" #include "Scene/Scene.h" // OScene/Scene - the InspectorView's entity pane needs a live scene #include "Component/Transform.h" @@ -141,18 +141,12 @@ int main(int argc, char** argv) { StaticMeshVertexData* vd = StaticMeshVertexData::getInstance(); vd->init(graphics); - GameInterface gameInterface(binaryPath); - OScene scene = new Scene(graphics); - gameInterface.reload(); - - OSystemGraph systemGraph = new SystemGraph(); - gameInterface.getGame()->setupScene(scene, systemGraph); - systemGraph->addSystem(new System::KeyboardInput(scene)); - systemGraph->addSystem(new System::FluidUpdater(scene)); - systemGraph->addSystem(new System::LightGather(scene)); - systemGraph->addSystem(new System::MeshUpdater(scene)); - systemGraph->addSystem(new System::CameraUpdater(scene)); - OWindowManager windowManager = new WindowManager(systemGraph); + // The world (Scene + SystemGraph) and the game binary are owned by the + // Application from here on. `loadGame` dlopen's the .so, lets the game + // populate the scene+graph, and appends the editor's own systems. Views + // below only render the non-owning application->scene() handle. + OApplication application = new Application(); + application->loadGame(graphics, sourcePath, gameName); AssetRegistry::init(sourcePath / "Assets", graphics); AssetImporter::init(graphics); AssetImporter::importFont(FontImportArgs{ @@ -187,7 +181,7 @@ int main(int argc, char** argv) { .title = "SeeleEngine", .preferredFormat = Gfx::SE_FORMAT_B8G8R8A8_SRGB, }; - auto window = windowManager->addWindow(graphics, mainWindowInfo); + auto window = application->addWindow(graphics, mainWindowInfo); ViewportCreateInfo sceneViewInfo = { .dimensions = { @@ -195,7 +189,7 @@ int main(int argc, char** argv) { .offset = {0, 0}, }, }; - OGameView sceneView = new Editor::PlayView(graphics, window, scene, sceneViewInfo); + OGameView sceneView = new Editor::PlayView(graphics, window, application->scene(), sceneViewInfo); sceneView->setFocused(); OInspectorView inspectorView = new Editor::InspectorView(graphics, window, ViewportCreateInfo{ @@ -205,12 +199,12 @@ int main(int argc, char** argv) { .offset = {0, 0}, }, }, - /* scene = */ scene); + /* scene = */ application->scene()); inspectorView->setFocused(); window->show(); - while (windowManager->isActive() && getGlobals().running) { - windowManager->render(); + while (application->isActive() && getGlobals().running) { + application->render(); } graphics->waitDeviceIdle(); Material::destroy(); diff --git a/src/Engine/Asset/AssetRegistry.cpp b/src/Engine/Asset/AssetRegistry.cpp index d5dbe6c..2672a2d 100644 --- a/src/Engine/Asset/AssetRegistry.cpp +++ b/src/Engine/Asset/AssetRegistry.cpp @@ -9,7 +9,7 @@ #include "FontAsset.h" #include "Graphics/Graphics.h" #include "Graphics/Mesh.h" -#include "Window/WindowManager.h" +#include "Window/Application.h" #include #include #include diff --git a/src/Engine/Asset/TextureAsset.cpp b/src/Engine/Asset/TextureAsset.cpp index 3de5ff9..49bae88 100644 --- a/src/Engine/Asset/TextureAsset.cpp +++ b/src/Engine/Asset/TextureAsset.cpp @@ -1,7 +1,7 @@ #include "TextureAsset.h" #include "Graphics/Graphics.h" #include "Graphics/Texture.h" -#include "Window/WindowManager.h" +#include "Window/Application.h" #include "ktx.h" #include diff --git a/src/Engine/Material/Material.cpp b/src/Engine/Material/Material.cpp index fa871ad..fca93f7 100644 --- a/src/Engine/Material/Material.cpp +++ b/src/Engine/Material/Material.cpp @@ -4,7 +4,7 @@ #include "Graphics/Shader.h" #include "MaterialInstance.h" #include "Serialization/Serialization.h" -#include "Window/WindowManager.h" +#include "Window/Application.h" #include using namespace Seele; diff --git a/src/Engine/Window/Application.cpp b/src/Engine/Window/Application.cpp new file mode 100644 index 0000000..ba8784f --- /dev/null +++ b/src/Engine/Window/Application.cpp @@ -0,0 +1,96 @@ +#include "Application.h" +#include "Graphics/Enums.h" +#include "Graphics/Graphics.h" +#include "System/SystemGraph.h" +#include "Scene/Scene.h" +#include "Game.h" +#include "System/KeyboardInput.h" +#include "System/FluidUpdater.h" +#include "System/LightGather.h" +#include "System/MeshUpdater.h" +#include "System/CameraUpdater.h" +#ifdef WIN32 +#include "Platform/Windows/GameInterface.h" +#else +#include "Platform/Linux/GameInterface.h" +#endif + +using namespace Seele; + +namespace { +// Library extension per platform (mirrors the editor's old binary-path logic). +const char* libraryExtension() { +#ifdef WIN32 + return "dll"; +#elif __APPLE__ + return "dylib"; +#else + return "so"; +#endif +} +} // namespace + +Application::Application() : graphics(nullptr), scene_(nullptr), graph_(nullptr), gameInterface(nullptr) {} + +Application::~Application() { + if (gameInterface != nullptr) { + delete gameInterface; // ~GameInterface dlclose()s + destroys the Game instance + gameInterface = nullptr; + } + // `scene_` and `graph_` are OwningPtr: their destructors free the Scene and + // (via the moved-in OSystemBase entries) every System. +} + +void Application::loadGame(Gfx::PGraphics graphics, const std::filesystem::path& sourcePath, const std::string& gameName) { + this->graphics = graphics; + + // The world is created exactly once, owned by the Application. `loadGame` is + // the single setup point (called once per process) — no null guards needed. + scene_ = OScene(new Scene(graphics)); + graph_ = OSystemGraph(new SystemGraph()); + + // Resolve the game binary and load it (dlopen) once. + std::filesystem::path binaryPath = sourcePath / "bin" / (gameName + "." + libraryExtension()); + GameInterface* interface = new GameInterface(binaryPath); + interface->reload(); + gameInterface = interface; + + // Let the game populate the Scene + SystemGraph we own, then append the + // editor's own systems. This used to live in the editor's main.cpp; it now + // lives here so the world has a single owner and a single setup point. + interface->getGame()->setupScene(scene_, graph_); + graph_->addSystem(new System::KeyboardInput(scene_)); + graph_->addSystem(new System::FluidUpdater(scene_)); + graph_->addSystem(new System::LightGather(scene_)); + graph_->addSystem(new System::MeshUpdater(scene_)); + graph_->addSystem(new System::CameraUpdater(scene_)); +} + +OWindow Application::addWindow(Gfx::PGraphics graphics, const WindowCreateInfo& createInfo) { + OWindow window = new Window(this, graphics->createWindow(createInfo)); + windows.add(window); + return window; +} + +void Application::attachWorld(OScene scene, OSystemGraph graph) { + // Worldless hosts (benchmark) build their own world and hand ownership in; + // the Application's owning members take it via move so the frame loop can + // step it and the views can render it. + scene_ = std::move(scene); + graph_ = std::move(graph); +} + +void Application::render() { + // Step the world exactly ONCE per frame, independent of how many windows + // are open. The old code re-ran the graph inside the per-window loop, which + // double-simulated with two windows. Views below only render the world. + graph_->run(Gfx::getCurrentFrameDelta()); + for (auto& window : windows) { + window->pollInputs(); + if (window->isPaused()) + continue; + window->render(); + } +} + +void Application::notifyWindowClosed(PWindow window) { windows.remove(window); } diff --git a/src/Engine/Window/Application.h b/src/Engine/Window/Application.h new file mode 100644 index 0000000..7be8c8d --- /dev/null +++ b/src/Engine/Window/Application.h @@ -0,0 +1,59 @@ +#pragma once +#include "Containers/Array.h" +#include "System/SystemGraph.h" +#include "Window.h" +#include + +namespace Seele { +DECLARE_NAME_REF(Gfx, Graphics) +DECLARE_REF(Scene) +class Game; +class GameInterface; + +// The long-lived top-level application object. +// +// Owns the *world*: the single Scene, the single SystemGraph (the running +// simulation) and the loaded game binary (GameInterface). There is exactly one +// of these per process, and it steps the simulation exactly once per frame -- +// regardless of how many windows/views observe the scene. Views hold a +// non-owning PScene and only render; they never own the world. +// +// This is the old "WindowManager", renamed so its true responsibilities +// (windowing + world ownership + the frame loop) are named honestly. +class Application { + public: + Application(); + ~Application(); + + // Loads the game binary (dlopen) and lets it populate the Scene + + // SystemGraph it owns, then appends the editor's own systems. Called once, + // by the embedding host (the editor / benchmark main). `graphics` is needed + // to construct the Scene; `sourcePath`/`gameName` resolve the .so. + void loadGame(Gfx::PGraphics graphics, const std::filesystem::path& sourcePath, const std::string& gameName); + + // Window management + OWindow addWindow(Gfx::PGraphics graphics, const WindowCreateInfo& createInfo); + void notifyWindowClosed(PWindow window); + bool isActive() const { return getGlobals().running && windows.size(); } + + // Per-frame driver: steps the world ONCE, then renders every window. + void render(); + + // Non-owning handles so views can render the shared world. + PScene scene() const { return scene_; } + PSystemGraph systemGraph() const { return graph_; } + + // Worldless hosts (e.g. the GPU-timing benchmark, which has no game binary) + // build their own world and hand ownership in; the owning members take it via + // move so the frame loop can step `graph_` and views can render `scene_`. + void attachWorld(OScene scene, OSystemGraph graph); + + private: + Array windows; + Gfx::PGraphics graphics; + OScene scene_; + OSystemGraph graph_; + GameInterface* gameInterface = nullptr; // owned by Application (dlclose'd in dtor) +}; +DEFINE_REF(Application) +} // namespace Seele diff --git a/src/Engine/Window/CMakeLists.txt b/src/Engine/Window/CMakeLists.txt index 4a935bb..3108eb9 100644 --- a/src/Engine/Window/CMakeLists.txt +++ b/src/Engine/Window/CMakeLists.txt @@ -6,8 +6,8 @@ target_sources(Engine View.cpp Window.cpp Window.h - WindowManager.h - WindowManager.cpp) + Application.h + Application.cpp) target_sources(Engine PUBLIC FILE_SET HEADERS @@ -15,4 +15,4 @@ target_sources(Engine GameView.h View.h Window.h - WindowManager.h) + Application.h) diff --git a/src/Engine/Window/Window.cpp b/src/Engine/Window/Window.cpp index 0d8118a..731dce7 100644 --- a/src/Engine/Window/Window.cpp +++ b/src/Engine/Window/Window.cpp @@ -1,10 +1,10 @@ #include "Window.h" -#include "WindowManager.h" +#include "Application.h" #include using namespace Seele; -Window::Window(PWindowManager owner, Gfx::OWindow handle) : owner(owner), gfxHandle(std::move(handle)) { +Window::Window(PApplication owner, Gfx::OWindow handle) : owner(owner), gfxHandle(std::move(handle)) { gfxHandle->setResizeCallback([this](uint32 w, uint32 h) { onResize(w, h); }); } diff --git a/src/Engine/Window/Window.h b/src/Engine/Window/Window.h index d65161f..bcc4b7b 100644 --- a/src/Engine/Window/Window.h +++ b/src/Engine/Window/Window.h @@ -3,11 +3,11 @@ #include "View.h" namespace Seele { -DECLARE_REF(WindowManager) +DECLARE_REF(Application) // The logical window, with the graphics proxy class Window { public: - Window(PWindowManager owner, Gfx::OWindow handle); + Window(PApplication owner, Gfx::OWindow handle); ~Window(); void show() { gfxHandle->show(); } void addView(PView view); @@ -20,7 +20,7 @@ class Window { constexpr bool shouldClose() const { return gfxHandle->shouldClose(); } protected: - PWindowManager owner; + PApplication owner; Array views; Gfx::OWindow gfxHandle; }; diff --git a/src/Engine/Window/WindowManager.cpp b/src/Engine/Window/WindowManager.cpp deleted file mode 100644 index 91efa36..0000000 --- a/src/Engine/Window/WindowManager.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "WindowManager.h" -#include "Graphics/Enums.h" -#include "Graphics/Graphics.h" -#include "System/SystemGraph.h" - -using namespace Seele; - -WindowManager::WindowManager(PSystemGraph systemGraph) : systemGraph(systemGraph) {} - -WindowManager::~WindowManager() {} - -OWindow WindowManager::addWindow(Gfx::PGraphics graphics, const WindowCreateInfo& createInfo) { - OWindow window = new Window(this, graphics->createWindow(createInfo)); - windows.add(window); - return window; -} - -void WindowManager::render() { - for (auto& window : windows) { - window->pollInputs(); - if (window->isPaused()) - continue; - systemGraph->run(Gfx::getCurrentFrameDelta()); - window->render(); - } -} - -void WindowManager::notifyWindowClosed(PWindow window) { windows.remove(window); } diff --git a/src/Engine/Window/WindowManager.h b/src/Engine/Window/WindowManager.h deleted file mode 100644 index 185798f..0000000 --- a/src/Engine/Window/WindowManager.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once -#include "Containers/Array.h" -#include "System/SystemGraph.h" -#include "Window.h" - -namespace Seele { -DECLARE_NAME_REF(Gfx, Graphics) -class WindowManager { - public: - WindowManager(PSystemGraph systemGraph); - ~WindowManager(); - OWindow addWindow(Gfx::PGraphics graphics, const WindowCreateInfo& createInfo); - void render(); - void notifyWindowClosed(PWindow window); - bool isActive() const { return getGlobals().running && windows.size(); } - - private: - Array windows; - PSystemGraph systemGraph; -}; -DEFINE_REF(WindowManager) -} // namespace Seele \ No newline at end of file