Skip to content

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.

refactor: decouple binding layer from EngineContext singleton

esenginea12e488defb6
diff --git a/src/esengine/bindings/ActiveContext.hpp b/src/esengine/bindings/ActiveContext.hpp
new file mode 100644
index 000000000..8a51bc412
--- /dev/null
+++ b/src/esengine/bindings/ActiveContext.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#ifdef ES_PLATFORM_WEB
+
+#include "../core/EstellaContext.hpp"
+
+namespace esengine {
+
+/**
+ * @brief Active engine context for the current thread/call
+ * @details All binding functions access the engine through this pointer.
+ * Set by initRenderer / setActiveContext before any rendering calls.
+ */
+inline EstellaContext* g_activeContext = nullptr;
+
+/**
+ * @brief Get the active context (asserts non-null in debug)
+ */
+inline EstellaContext& activeCtx() {
+ return *g_activeContext;
+}
+
+} // namespace esengine
+
+#endif // ES_PLATFORM_WEB
diff --git a/src/esengine/bindings/GeometryBindings.cpp b/src/esengine/bindings/GeometryBindings.cpp
index f13d9fc8e..7453ad653 100644
--- a/src/esengine/bindings/GeometryBindings.cpp
+++ b/src/esengine/bindings/GeometryBindings.cpp
@@ -1,7 +1,7 @@
#ifdef ES_PLATFORM_WEB
#include "GeometryBindings.hpp"
-#include "EngineContext.hpp"
+#include "ActiveContext.hpp"
#include "../renderer/OpenGLHeaders.hpp"
#include "../renderer/GfxDevice.hpp"
#include "../renderer/CustomGeometry.hpp"
@@ -23,7 +23,7 @@
namespace esengine {
-static EngineContext& ctx() { return EngineContext::instance(); }
+static EstellaContext& ctx() { return activeCtx(); }
#define g_device (ctx().tryGet<GfxDevice>())
#define g_initialized (ctx().state().initialized)
diff --git a/src/esengine/bindings/ImmediateDrawBindings.cpp b/src/esengine/bindings/ImmediateDrawBindings.cpp
index f025f0458..630adec10 100644
--- a/src/esengine/bindings/ImmediateDrawBindings.cpp
+++ b/src/esengine/bindings/ImmediateDrawBindings.cpp
@@ -1,7 +1,7 @@
#ifdef ES_PLATFORM_WEB
#include "ImmediateDrawBindings.hpp"
-#include "EngineContext.hpp"
+#include "ActiveContext.hpp"
#include "../renderer/OpenGLHeaders.hpp"
#include "../renderer/GfxDevice.hpp"
#include "../renderer/BlendMode.hpp"
@@ -21,7 +21,7 @@
namespace esengine {
-static EngineContext& ctx() { return EngineContext::instance(); }
+static EstellaContext& ctx() { return activeCtx(); }
#define g_device (ctx().tryGet<GfxDevice>())
#define g_initialized (ctx().state().initialized)
diff --git a/src/esengine/bindings/PostProcessBindings.cpp b/src/esengine/bindings/PostProcessBindings.cpp
index 543dc47a6..aa37240aa 100644
--- a/src/esengine/bindings/PostProcessBindings.cpp
+++ b/src/esengine/bindings/PostProcessBindings.cpp
@@ -2,7 +2,7 @@
#ifdef ES_ENABLE_POSTPROCESS
#include "PostProcessBindings.hpp"
-#include "EngineContext.hpp"
+#include "ActiveContext.hpp"
#include "../renderer/OpenGLHeaders.hpp"
#include "../renderer/GfxDevice.hpp"
#include "../renderer/PostProcessPipeline.hpp"
@@ -21,7 +21,7 @@
namespace esengine {
-static EngineContext& ctx() { return EngineContext::instance(); }
+static EstellaContext& ctx() { return activeCtx(); }
#define g_device (ctx().tryGet<GfxDevice>())
#define g_initialized (ctx().state().initialized)
diff --git a/src/esengine/bindings/RendererBindings.cpp b/src/esengine/bindings/RendererBindings.cpp
index 5432c43c6..c05f2ab5a 100644
--- a/src/esengine/bindings/RendererBindings.cpp
+++ b/src/esengine/bindings/RendererBindings.cpp
@@ -1,7 +1,7 @@
#ifdef ES_PLATFORM_WEB
#include "RendererBindings.hpp"
-#include "EngineContext.hpp"
+#include "ActiveContext.hpp"
#include "../renderer/OpenGLHeaders.hpp"
#include "../renderer/GfxDevice.hpp"
#include "../renderer/RenderFrame.hpp"
@@ -33,7 +33,7 @@
namespace esengine {
-static EngineContext& ctx() { return EngineContext::instance(); }
+static EstellaContext& ctx() { return activeCtx(); }
#define g_device (ctx().tryGet<GfxDevice>())
#define g_initialized (ctx().state().initialized)
diff --git a/src/esengine/bindings/TilemapBindings.cpp b/src/esengine/bindings/TilemapBindings.cpp
index 10795f90c..74cfaaac5 100644
--- a/src/esengine/bindings/TilemapBindings.cpp
+++ b/src/esengine/bindings/TilemapBindings.cpp
@@ -1,7 +1,7 @@
#ifdef ES_PLATFORM_WEB
#ifdef ES_ENABLE_TILEMAP
-#include "EngineContext.hpp"
+#include "ActiveContext.hpp"
#include "../tilemap/TilemapSystem.hpp"
#include "../tilemap/TiledMapLoader.hpp"
#include "../renderer/RenderContext.hpp"
@@ -29,7 +29,7 @@ tilemap::TilemapSystem& getTilemapSystem() {
}
static tilemap::TiledMapLoader s_tiledLoader;
-static EngineContext& ctx() { return EngineContext::instance(); }
+static EstellaContext& ctx() { return activeCtx(); }
void tilemap_initLayer(u32 entity, u32 width, u32 height,
f32 tileWidth, f32 tileHeight) {
diff --git a/src/esengine/bindings/TimelineBindings.cpp b/src/esengine/bindings/TimelineBindings.cpp
index b48410a59..890db6ebd 100644
--- a/src/esengine/bindings/TimelineBindings.cpp
+++ b/src/esengine/bindings/TimelineBindings.cpp
@@ -4,7 +4,7 @@
#include <emscripten.h>
#include <emscripten/bind.h>
-#include "EngineContext.hpp"
+#include "ActiveContext.hpp"
#include "../animation/TimelineSystem.hpp"
#include <cstring>
@@ -12,7 +12,7 @@
namespace esengine {
-static animation::TimelineSystem* tlSys() { return EngineContext::instance().tryGet<animation::TimelineSystem>(); }
+static animation::TimelineSystem* tlSys() { return activeCtx().tryGet<animation::TimelineSystem>(); }
// ---------------------------------------------------------------------------
// Create / Destroy
diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cpp
index 5846acc6f..ef4af13c0 100644
--- a/src/esengine/bindings/WebSDKEntry.cpp
+++ b/src/esengine/bindings/WebSDKEntry.cpp
@@ -16,6 +16,7 @@
#include <emscripten/html5.h>
#include "EngineContext.hpp"
+#include "ActiveContext.hpp"
#include "ResourceManagerBindings.hpp"
#include "RendererBindings.hpp"
#include "ImmediateDrawBindings.hpp"
@@ -65,7 +66,8 @@ static_assert(sizeof(void*) == 4, "EM_JS pointer passing assumes wasm32 (4-byte
namespace esengine {
-static EngineContext& ctx() { return EngineContext::instance(); }
+static EngineContext& legacyCtx() { return EngineContext::instance(); }
+static EstellaContext& ctx() { return activeCtx(); }
#define g_initialized (ctx().state().initialized)
#define g_resourceManager (ctx().tryGet<resource::ResourceManager>())
@@ -205,7 +207,8 @@ bool initRendererInternal(const char* canvasSelector) {
}
ES_LOG_INFO("WebGL2 context created for '{}'", canvasSelector);
- return ctx().context().init(static_cast<int>(webglCtx));
+ g_activeContext = &legacyCtx().context();
+ return g_activeContext->init(static_cast<int>(webglCtx));
}
void initRenderer() {
@@ -223,11 +226,15 @@ bool initRendererWithContext(int contextHandle) {
return false;
}
- return ctx().context().init(contextHandle);
+ g_activeContext = &legacyCtx().context();
+ return g_activeContext->init(contextHandle);
}
void shutdownRenderer() {
- ctx().shutdown();
+ if (g_activeContext) {
+ g_activeContext->shutdown();
+ g_activeContext = nullptr;
+ }
}
resource::ResourceManager* getResourceManager() {