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.

feat: Add Web platform font rendering support

esengine8e7394c6957c
diff --git a/bindings/platform/web/index.html b/bindings/platform/web/index.html
index 1fd677c0c..b2df6585d 100644
--- a/bindings/platform/web/index.html
+++ b/bindings/platform/web/index.html
@@ -5,11 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>ESEngine</title>
<style>
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
+ * { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
@@ -31,44 +27,43 @@
font-family: sans-serif;
font-size: 18px;
}
- #loading.hidden {
- display: none;
- }
+ #loading.hidden { display: none; }
</style>
</head>
<body>
<div id="loading">Loading ESEngine...</div>
<canvas id="canvas"></canvas>
- <script>
- // ESEngine Web Loader
- var Module = {
- canvas: (function() {
- return document.getElementById('canvas');
- })(),
- onRuntimeInitialized: function() {
- document.getElementById('loading').classList.add('hidden');
- console.log('ESEngine initialized');
- },
- print: function(text) {
- console.log(text);
- },
- printErr: function(text) {
- console.error(text);
- }
- };
+ <script type="module">
+ // ESEngine ES6 Module Loader
+ import ESEngineModule from './hello_triangle.js';
- // Handle canvas resizing
function resizeCanvas() {
- var canvas = document.getElementById('canvas');
- var dpr = window.devicePixelRatio || 1;
+ const canvas = document.getElementById('canvas');
+ const dpr = window.devicePixelRatio || 1;
canvas.width = window.innerWidth * dpr;
canvas.height = window.innerHeight * dpr;
}
-
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
+
+ ESEngineModule({
+ canvas: document.getElementById('canvas'),
+ print: (text) => console.log(text),
+ printErr: (text) => console.error(text)
+ }).then((Module) => {
+ console.log('ESEngine module loaded');
+ document.getElementById('loading').classList.add('hidden');
+ Module._es_app_init();
+ }).catch((err) => {
+ // "unwind" is expected for Emscripten main loop
+ if (err === 'unwind' || (err.message && err.message.includes('unwind'))) {
+ console.log('App running');
+ return;
+ }
+ console.error('Failed:', err);
+ document.getElementById('loading').textContent = 'Error: ' + err.message;
+ });
</script>
- <script src="hello_triangle.js"></script>
</body>
</html>
diff --git a/cmake/Emscripten.cmake b/cmake/Emscripten.cmake
index bc6da2680..63e833f75 100644
--- a/cmake/Emscripten.cmake
+++ b/cmake/Emscripten.cmake
@@ -28,9 +28,11 @@ set(ES_EMSCRIPTEN_LINK_FLAGS
-sEXPORT_ES6=1 # Export as ES6 module
-sMODULARIZE=1 # Wrap in module factory function
"-sEXPORT_NAME='ESEngineModule'" # Module name
- # EMSCRIPTEN_KEEPALIVE functions are auto-exported, only need to add stdlib functions
- "-sEXPORTED_FUNCTIONS=['_malloc','_free']"
+ # Exported functions (EMSCRIPTEN_KEEPALIVE + stdlib)
+ "-sEXPORTED_FUNCTIONS=['_malloc','_free','_es_app_init']"
"-sEXPORTED_RUNTIME_METHODS=['ccall','cwrap','HEAPF32','HEAPU8','HEAPU32']"
+ # Embed assets (fonts, etc.)
+ "--embed-file=${CMAKE_SOURCE_DIR}/assets/fonts@/assets/fonts"
)
# Debug-specific flags
diff --git a/examples/hello_triangle/main.cpp b/examples/hello_triangle/main.cpp
index 2c2750a20..9bf0f7224 100644
--- a/examples/hello_triangle/main.cpp
+++ b/examples/hello_triangle/main.cpp
@@ -32,7 +32,9 @@ protected:
// Load SDF font with CJK support
const char* fontPaths[] = {
-#ifdef _WIN32
+#ifdef ES_PLATFORM_WEB
+ "/assets/fonts/msyh.ttc", // Embedded font for web
+#elif defined(_WIN32)
"C:/Windows/Fonts/msyh.ttc", // Microsoft YaHei (中文)
"C:/Windows/Fonts/simhei.ttf", // SimHei
"C:/Windows/Fonts/simsun.ttc", // SimSun
diff --git a/src/esengine/ui/font/SDFFont.cpp b/src/esengine/ui/font/SDFFont.cpp
index 9af8bb287..7491f328e 100644
--- a/src/esengine/ui/font/SDFFont.cpp
+++ b/src/esengine/ui/font/SDFFont.cpp
@@ -202,11 +202,12 @@ bool SDFFont::loadFromFile(const std::string& path, f32 sdfSize, f32 sdfSpread)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
#ifdef ES_PLATFORM_WEB
- glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, atlasWidth_, atlasHeight_, 0,
- GL_LUMINANCE, GL_UNSIGNED_BYTE, atlasData_.data());
+ // WebGL 2 (GLES 3.0) uses GL_R8 format for single-channel textures
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, atlasWidth_, atlasHeight_, 0,
+ GL_RED, GL_UNSIGNED_BYTE, atlasData_.data());
#else
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, atlasWidth_, atlasHeight_, 0,
GL_RED, GL_UNSIGNED_BYTE, atlasData_.data());
#endif
@@ -321,13 +322,8 @@ SDFGlyphInfo* SDFFont::loadGlyph(u32 codepoint) {
glyph.v1 = static_cast<f32>(atlasY + glyphH) / static_cast<f32>(atlasHeight_);
glBindTexture(GL_TEXTURE_2D, atlasTextureId_);
-#ifdef ES_PLATFORM_WEB
- glTexSubImage2D(GL_TEXTURE_2D, 0, atlasX, atlasY, glyphW, glyphH,
- GL_LUMINANCE, GL_UNSIGNED_BYTE, bitmap.buffer);
-#else
glTexSubImage2D(GL_TEXTURE_2D, 0, atlasX, atlasY, glyphW, glyphH,
GL_RED, GL_UNSIGNED_BYTE, bitmap.buffer);
-#endif
glBindTexture(GL_TEXTURE_2D, 0);
markAtlasRegionUsed(atlasX, atlasY, glyphW + padding, glyphH + padding);
@@ -408,8 +404,8 @@ void SDFFont::rebuildAtlasTexture() {
glBindTexture(GL_TEXTURE_2D, atlasTextureId_);
#ifdef ES_PLATFORM_WEB
- glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, atlasWidth_, atlasHeight_, 0,
- GL_LUMINANCE, GL_UNSIGNED_BYTE, atlasData_.data());
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, atlasWidth_, atlasHeight_, 0,
+ GL_RED, GL_UNSIGNED_BYTE, atlasData_.data());
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, atlasWidth_, atlasHeight_, 0,
GL_RED, GL_UNSIGNED_BYTE, atlasData_.data());