A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor: extract EstellaContext as instance-based engine context
esengine2659b401b945
diff --git a/CMakeLists.txt b/CMakeLists.txtindex df7955976..73295b532 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -35,6 +35,7 @@ add_subdirectory(third_party)set(ESENGINE_SOURCESsrc/esengine/core/Engine.cpp+ src/esengine/core/EstellaContext.cppsrc/esengine/core/Log.cppsrc/esengine/app/App.cppsrc/esengine/resource/ResourceManager.cppdiff --git a/src/esengine/bindings/EngineContext.cpp b/src/esengine/bindings/EngineContext.cppindex d70ca7f6c..be8928b90 100644--- a/src/esengine/bindings/EngineContext.cpp+++ b/src/esengine/bindings/EngineContext.cpp@@ -1,65 +1,14 @@#ifdef ES_PLATFORM_WEB#include "EngineContext.hpp"-#include "../renderer/RenderContext.hpp"-#include "../renderer/RenderFrame.hpp"-#include "../renderer/ImmediateDraw.hpp"-#include "../resource/ResourceManager.hpp"-#ifdef ES_ENABLE_SPINE-#include "../spine/SpineResourceManager.hpp"-#endif-#include <emscripten/html5.h>namespace esengine {-EngineContext::EngineContext() {- services_.registerOwned<EngineState>(makeUnique<EngineState>());- state_ = services_.getService<EngineState>();-}-EngineContext& EngineContext::instance() {static EngineContext ctx;return ctx;}-void EngineContext::shutdown() {- if (!state_->initialized) return;-- if (auto* rf = tryGet<RenderFrame>()) {- rf->shutdown();- }-- if (auto* id = tryGet<ImmediateDraw>()) {- id->shutdown();- }--#ifdef ES_ENABLE_SPINE- if (auto* srm = tryGet<spine::SpineResourceManager>()) {- srm->shutdown();- }-#endif-- if (auto* rc = tryGet<RenderContext>()) {- rc->shutdown();- }-- if (auto* rm = tryGet<resource::ResourceManager>()) {- rm->shutdown();- }-- auto webglCtx = state_->webgl_context;-- services_.removeService<EngineState>();- services_.clear();-- if (webglCtx > 0) {- emscripten_webgl_destroy_context(webglCtx);- }-- services_.registerOwned<EngineState>(makeUnique<EngineState>());- state_ = services_.getService<EngineState>();-}-} // namespace esengine#endif // ES_PLATFORM_WEBdiff --git a/src/esengine/bindings/EngineContext.hpp b/src/esengine/bindings/EngineContext.hppindex 8975505be..7d0a3509a 100644--- a/src/esengine/bindings/EngineContext.hpp+++ b/src/esengine/bindings/EngineContext.hpp@@ -4,10 +4,16 @@#include "../core/Types.hpp"#include "../core/ServiceRegistry.hpp"-#include "EngineState.hpp"+#include "../core/EstellaContext.hpp"namespace esengine {+/**+ * @brief Legacy singleton wrapper around EstellaContext+ * @details Provides backward compatibility for binding files that use ctx().+ * Internally delegates to an owned EstellaContext instance.+ * Will be removed once all bindings switch to direct context access.+ */class EngineContext {public:static EngineContext& instance();@@ -15,25 +21,26 @@ public:EngineContext(const EngineContext&) = delete;EngineContext& operator=(const EngineContext&) = delete;- void shutdown();+ EstellaContext& context() { return context_; }- ServiceRegistry& services() { return services_; }+ ServiceRegistry& services() { return context_.services(); }template<typename T>- T& require() { return services_.require<T>(); }+ T& require() { return context_.require<T>(); }template<typename T>- T* tryGet() { return services_.getService<T>(); }+ T* tryGet() { return context_.tryGet<T>(); }- EngineState& state() { return *state_; }- const EngineState& state() const { return *state_; }+ EngineState& state() { return context_.state(); }+ const EngineState& state() const { return context_.state(); }++ void shutdown() { context_.shutdown(); }private:- EngineContext();+ EngineContext() = default;~EngineContext() = default;- ServiceRegistry services_;- EngineState* state_ = nullptr;+ EstellaContext context_;};} // namespace esenginediff --git a/src/esengine/bindings/EngineState.hpp b/src/esengine/bindings/EngineState.hppindex 8edd7af44..f62f6b089 100644--- a/src/esengine/bindings/EngineState.hpp+++ b/src/esengine/bindings/EngineState.hpp@@ -1,8 +1,5 @@#pragma once-#ifdef ES_PLATFORM_WEB--#include <emscripten/html5.h>#include <glm/glm.hpp>#include "../core/Types.hpp"#include "MaterialCache.hpp"@@ -10,7 +7,7 @@namespace esengine {struct EngineState {- EMSCRIPTEN_WEBGL_CONTEXT_HANDLE webgl_context = 0;+ int webgl_context = 0;bool initialized = false;bool immediate_draw_active = false;bool gl_error_check_enabled = true;@@ -24,5 +21,3 @@ struct EngineState {};} // namespace esengine--#endif // ES_PLATFORM_WEBdiff --git a/src/esengine/bindings/MaterialCache.hpp b/src/esengine/bindings/MaterialCache.hppindex e90d2338c..ba10b4a93 100644--- a/src/esengine/bindings/MaterialCache.hpp+++ b/src/esengine/bindings/MaterialCache.hpp@@ -1,7 +1,5 @@#pragma once-#ifdef ES_PLATFORM_WEB-#include "../core/Types.hpp"#include <unordered_map>#include <vector>@@ -41,5 +39,3 @@ private:};} // namespace esengine--#endif // ES_PLATFORM_WEBdiff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cppindex 00257c98e..5846acc6f 100644--- a/src/esengine/bindings/WebSDKEntry.cpp+++ b/src/esengine/bindings/WebSDKEntry.cpp@@ -30,25 +30,11 @@#include "../ecs/UIFlexLayoutSystem.hpp"#include "../renderer/OpenGLHeaders.hpp"-#include "../renderer/GLDevice.hpp"+#include "../renderer/GfxDevice.hpp"#include "../renderer/RenderContext.hpp"#include "../renderer/RenderFrame.hpp"#include "../renderer/ImmediateDraw.hpp"#include "../renderer/CustomGeometry.hpp"-#include "../renderer/plugins/SpritePlugin.hpp"-#include "../renderer/plugins/UIElementPlugin.hpp"-#include "../renderer/plugins/TextPlugin.hpp"-#include "../renderer/plugins/ShapePlugin.hpp"-#ifdef ES_ENABLE_PARTICLES-#include "../renderer/plugins/ParticlePlugin.hpp"-#endif-#ifdef ES_ENABLE_TILEMAP-#include "../renderer/plugins/TilemapRenderPlugin.hpp"-#include "../tilemap/TilemapSystem.hpp"-#endif-#ifdef ES_ENABLE_SPINE-#include "../renderer/plugins/SpinePlugin.hpp"-#endif#include "../resource/ResourceManager.hpp"#include "../ecs/TransformSystem.hpp"#include "../core/World.hpp"@@ -79,14 +65,9 @@ static_assert(sizeof(void*) == 4, "EM_JS pointer passing assumes wasm32 (4-bytenamespace esengine {-#ifdef ES_ENABLE_TILEMAP-tilemap::TilemapSystem& getTilemapSystem();-#endif-static EngineContext& ctx() { return EngineContext::instance(); }#define g_initialized (ctx().state().initialized)-#define g_webglContext (ctx().state().webgl_context)#define g_resourceManager (ctx().tryGet<resource::ResourceManager>())#ifdef ES_ENABLE_SPINE#define g_spineResourceManager (ctx().tryGet<spine::SpineResourceManager>())@@ -201,81 +182,6 @@ bool getMaterialDataWithUniforms(u32 materialId, u32& shaderId, u32& blendMode,return true;}-static void initSubsystems() {- auto& svc = ctx().services();-- auto resourceManager = makeUnique<resource::ResourceManager>();- resourceManager->init();- svc.registerOwned<resource::ResourceManager>(std::move(resourceManager));-- auto gfxDevice = makeUnique<GLDevice>();- auto* gfxDevicePtr = gfxDevice.get();- svc.registerOwned<GfxDevice>(std::move(gfxDevice));-- auto renderContext = makeUnique<RenderContext>(*gfxDevicePtr);- renderContext->init();- svc.registerOwned<RenderContext>(std::move(renderContext));-- svc.registerOwned<ecs::TransformSystem>(makeUnique<ecs::TransformSystem>());- svc.registerOwned<animation::TweenSystem>(makeUnique<animation::TweenSystem>());-#ifdef ES_ENABLE_TIMELINE- svc.registerOwned<animation::TimelineSystem>(makeUnique<animation::TimelineSystem>());-#endif-#ifdef ES_ENABLE_PARTICLES- svc.registerOwned<particle::ParticleSystem>(makeUnique<particle::ParticleSystem>());-#endif--#ifdef ES_ENABLE_SPINE- auto spineResourceManager = makeUnique<spine::SpineResourceManager>(*g_resourceManager);- spineResourceManager->init();- svc.registerOwned<spine::SpineResourceManager>(std::move(spineResourceManager));- svc.registerOwned<spine::SpineSystem>(makeUnique<spine::SpineSystem>(*g_spineResourceManager));-#endif-- auto immediateDraw = makeUnique<ImmediateDraw>(*gfxDevicePtr, *g_renderContext, *g_resourceManager);- immediateDraw->init();- svc.registerOwned<ImmediateDraw>(std::move(immediateDraw));-- svc.registerOwned<GeometryManager>(makeUnique<GeometryManager>());-- auto renderFrame = makeUnique<RenderFrame>(ctx().require<GfxDevice>(), *g_renderContext, *g_resourceManager);-- renderFrame->addPlugin(std::make_unique<SpritePlugin>());- renderFrame->addPlugin(std::make_unique<UIElementPlugin>());- renderFrame->addPlugin(std::make_unique<TextPlugin>());- renderFrame->addPlugin(std::make_unique<ShapePlugin>());-#ifdef ES_ENABLE_TILEMAP- {- auto tilemapPlugin = std::make_unique<TilemapRenderPlugin>();- tilemapPlugin->setTilemapSystem(&getTilemapSystem());- renderFrame->addPlugin(std::move(tilemapPlugin));- }-#endif-#ifdef ES_ENABLE_SPINE- {- auto spinePlugin = std::make_unique<SpinePlugin>();- spinePlugin->setSpineSystem(ctx().tryGet<spine::SpineSystem>());- renderFrame->addPlugin(std::move(spinePlugin));- }-#endif-#ifdef ES_ENABLE_PARTICLES- {- auto particlePlugin = std::make_unique<ParticlePlugin>();- particlePlugin->setParticleSystem(ctx().tryGet<particle::ParticleSystem>());- renderFrame->addPlugin(std::move(particlePlugin));- }-#endif- renderFrame->init(1280, 720);- svc.registerOwned<RenderFrame>(std::move(renderFrame));-- ctx().state().initialized = true;-- const auto& cc = ctx().state().clear_color;- auto* dev = ctx().tryGet<GfxDevice>();- dev->setClearColor(cc.r, cc.g, cc.b, cc.a);- dev->clear(true, true, false);-}-bool initRendererInternal(const char* canvasSelector) {if (g_initialized) return true;@@ -297,18 +203,9 @@ bool initRendererInternal(const char* canvasSelector) {ES_LOG_ERROR("Failed to create WebGL2 context for '{}': {}", canvasSelector, webglCtx);return false;}- ctx().state().webgl_context = webglCtx;-- EMSCRIPTEN_RESULT result = emscripten_webgl_make_context_current(g_webglContext);- if (result != EMSCRIPTEN_RESULT_SUCCESS) {- ES_LOG_ERROR("Failed to make WebGL context current: {}", result);- return false;- }ES_LOG_INFO("WebGL2 context created for '{}'", canvasSelector);-- initSubsystems();- return true;+ return ctx().context().init(static_cast<int>(webglCtx));}void initRenderer() {@@ -326,18 +223,7 @@ bool initRendererWithContext(int contextHandle) {return false;}- ctx().state().webgl_context = contextHandle;-- EMSCRIPTEN_RESULT result = emscripten_webgl_make_context_current(g_webglContext);- if (result != EMSCRIPTEN_RESULT_SUCCESS) {- ES_LOG_ERROR("Failed to make WebGL context current: {}", result);- return false;- }-- ES_LOG_INFO("WebGL context set from external handle: {}", contextHandle);-- initSubsystems();- return true;+ return ctx().context().init(contextHandle);}void shutdownRenderer() {diff --git a/src/esengine/core/EstellaContext.cpp b/src/esengine/core/EstellaContext.cppnew file mode 100644index 000000000..a73e150aa--- /dev/null+++ b/src/esengine/core/EstellaContext.cpp@@ -0,0 +1,191 @@+/**+ * @file EstellaContext.cpp+ * @brief Instance-based engine context implementation+ *+ * @author ESEngine Team+ * @date 2026+ */++#include "EstellaContext.hpp"+#include "Log.hpp"++#include "../renderer/GLDevice.hpp"+#include "../renderer/RenderContext.hpp"+#include "../renderer/RenderFrame.hpp"+#include "../renderer/ImmediateDraw.hpp"+#include "../renderer/CustomGeometry.hpp"+#include "../renderer/plugins/SpritePlugin.hpp"+#include "../renderer/plugins/UIElementPlugin.hpp"+#include "../renderer/plugins/TextPlugin.hpp"+#include "../renderer/plugins/ShapePlugin.hpp"+#ifdef ES_ENABLE_PARTICLES+#include "../renderer/plugins/ParticlePlugin.hpp"+#include "../particle/ParticleSystem.hpp"+#endif+#ifdef ES_ENABLE_TILEMAP+#include "../renderer/plugins/TilemapRenderPlugin.hpp"+#include "../tilemap/TilemapSystem.hpp"+#endif+#ifdef ES_ENABLE_SPINE+#include "../renderer/plugins/SpinePlugin.hpp"+#include "../spine/SpineResourceManager.hpp"+#include "../spine/SpineSystem.hpp"+#endif+#include "../resource/ResourceManager.hpp"+#include "../ecs/TransformSystem.hpp"+#include "../animation/TweenSystem.hpp"+#ifdef ES_ENABLE_TIMELINE+#include "../animation/TimelineSystem.hpp"+#endif++#ifdef ES_PLATFORM_WEB+#include <emscripten/html5.h>+#endif++namespace esengine {++#ifdef ES_ENABLE_TILEMAP+// Forward declaration — defined in TilemapBindings.cpp for web builds,+// needs local instance for non-web. For now we create a fresh system.+#endif++EstellaContext::EstellaContext() = default;++EstellaContext::~EstellaContext() {+ if (state_.initialized) {+ shutdown();+ }+}++bool EstellaContext::init(int webglContextHandle) {+ if (state_.initialized) {+ ES_LOG_WARN("EstellaContext already initialized");+ return true;+ }++ state_.webgl_context = webglContextHandle;++#ifdef ES_PLATFORM_WEB+ EMSCRIPTEN_RESULT result = emscripten_webgl_make_context_current(+ static_cast<EMSCRIPTEN_WEBGL_CONTEXT_HANDLE>(webglContextHandle));+ if (result != EMSCRIPTEN_RESULT_SUCCESS) {+ ES_LOG_ERROR("Failed to make WebGL context current: {}", result);+ return false;+ }+#endif++ initSubsystems();+ return true;+}++void EstellaContext::initSubsystems() {+ auto resourceManager = makeUnique<resource::ResourceManager>();+ resourceManager->init();+ services_.registerOwned<resource::ResourceManager>(std::move(resourceManager));++ auto gfxDevice = makeUnique<GLDevice>();+ auto* gfxDevicePtr = gfxDevice.get();+ services_.registerOwned<GfxDevice>(std::move(gfxDevice));++ auto renderContext = makeUnique<RenderContext>(*gfxDevicePtr);+ renderContext->init();+ services_.registerOwned<RenderContext>(std::move(renderContext));++ services_.registerOwned<ecs::TransformSystem>(makeUnique<ecs::TransformSystem>());+ services_.registerOwned<animation::TweenSystem>(makeUnique<animation::TweenSystem>());++#ifdef ES_ENABLE_TIMELINE+ services_.registerOwned<animation::TimelineSystem>(makeUnique<animation::TimelineSystem>());+#endif+#ifdef ES_ENABLE_PARTICLES+ services_.registerOwned<particle::ParticleSystem>(makeUnique<particle::ParticleSystem>());+#endif++#ifdef ES_ENABLE_SPINE+ {+ auto* rm = services_.getService<resource::ResourceManager>();+ auto spineRM = makeUnique<spine::SpineResourceManager>(*rm);+ spineRM->init();+ auto* spineRMPtr = spineRM.get();+ services_.registerOwned<spine::SpineResourceManager>(std::move(spineRM));+ services_.registerOwned<spine::SpineSystem>(makeUnique<spine::SpineSystem>(*spineRMPtr));+ }+#endif++ auto* rm = services_.getService<resource::ResourceManager>();+ auto* rc = services_.getService<RenderContext>();+ auto immediateDraw = makeUnique<ImmediateDraw>(*gfxDevicePtr, *rc, *rm);+ immediateDraw->init();+ services_.registerOwned<ImmediateDraw>(std::move(immediateDraw));++ services_.registerOwned<GeometryManager>(makeUnique<GeometryManager>());++ auto renderFrame = makeUnique<RenderFrame>(*gfxDevicePtr, *rc, *rm);+ renderFrame->addPlugin(std::make_unique<SpritePlugin>());+ renderFrame->addPlugin(std::make_unique<UIElementPlugin>());+ renderFrame->addPlugin(std::make_unique<TextPlugin>());+ renderFrame->addPlugin(std::make_unique<ShapePlugin>());++#ifdef ES_ENABLE_TILEMAP+ {+ auto tilemapPlugin = std::make_unique<TilemapRenderPlugin>();+ auto* ts = services_.getService<tilemap::TilemapSystem>();+ if (ts) tilemapPlugin->setTilemapSystem(ts);+ renderFrame->addPlugin(std::move(tilemapPlugin));+ }+#endif+#ifdef ES_ENABLE_SPINE+ {+ auto spinePlugin = std::make_unique<SpinePlugin>();+ spinePlugin->setSpineSystem(services_.getService<spine::SpineSystem>());+ renderFrame->addPlugin(std::move(spinePlugin));+ }+#endif+#ifdef ES_ENABLE_PARTICLES+ {+ auto particlePlugin = std::make_unique<ParticlePlugin>();+ particlePlugin->setParticleSystem(services_.getService<particle::ParticleSystem>());+ renderFrame->addPlugin(std::move(particlePlugin));+ }+#endif++ renderFrame->init(state_.viewport_width, state_.viewport_height);+ services_.registerOwned<RenderFrame>(std::move(renderFrame));++ state_.initialized = true;++ gfxDevicePtr->setClearColor(+ state_.clear_color.r, state_.clear_color.g,+ state_.clear_color.b, state_.clear_color.a);+ gfxDevicePtr->clear(true, true, false);++ ES_LOG_INFO("EstellaContext initialized");+}++void EstellaContext::shutdown() {+ if (!state_.initialized) return;++ if (auto* rf = tryGet<RenderFrame>()) rf->shutdown();+ if (auto* id = tryGet<ImmediateDraw>()) id->shutdown();+#ifdef ES_ENABLE_SPINE+ if (auto* srm = tryGet<spine::SpineResourceManager>()) srm->shutdown();+#endif+ if (auto* rc = tryGet<RenderContext>()) rc->shutdown();+ if (auto* rm = tryGet<resource::ResourceManager>()) rm->shutdown();++ int webglCtx = state_.webgl_context;++ services_.clear();+ state_ = EngineState{};++#ifdef ES_PLATFORM_WEB+ if (webglCtx > 0) {+ emscripten_webgl_destroy_context(+ static_cast<EMSCRIPTEN_WEBGL_CONTEXT_HANDLE>(webglCtx));+ }+#endif++ ES_LOG_INFO("EstellaContext shutdown");+}++} // namespace esenginediff --git a/src/esengine/core/EstellaContext.hpp b/src/esengine/core/EstellaContext.hppnew file mode 100644index 000000000..45db1234c--- /dev/null+++ b/src/esengine/core/EstellaContext.hpp@@ -0,0 +1,62 @@+/**+ * @file EstellaContext.hpp+ * @brief Instance-based engine context for estella renderer+ * @details Owns all engine subsystems (GfxDevice, RenderContext, RenderFrame,+ * ResourceManager, etc.) with explicit lifecycle management.+ * Replaces the EngineContext singleton for multi-instance support.+ *+ * @author ESEngine Team+ * @date 2026+ */+#pragma once++#include "Types.hpp"+#include "ServiceRegistry.hpp"+#include "../bindings/EngineState.hpp"++namespace esengine {++class GfxDevice;++class EstellaContext {+public:+ EstellaContext();+ ~EstellaContext();++ EstellaContext(const EstellaContext&) = delete;+ EstellaContext& operator=(const EstellaContext&) = delete;++ /**+ * @brief Initialize with an existing WebGL context handle+ * @param webglContextHandle Valid WebGL context (from emscripten_webgl_create_context)+ * @return True on success+ */+ bool init(int webglContextHandle);++ /**+ * @brief Shut down all subsystems and release resources+ */+ void shutdown();++ /** @brief Check if the context is initialized */+ bool isInitialized() const { return state_.initialized; }++ ServiceRegistry& services() { return services_; }++ template<typename T>+ T& require() { return services_.require<T>(); }++ template<typename T>+ T* tryGet() { return services_.getService<T>(); }++ EngineState& state() { return state_; }+ const EngineState& state() const { return state_; }++private:+ void initSubsystems();++ ServiceRegistry services_;+ EngineState state_;+};++} // namespace esengine