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(native): real image assets — stb_image decode -> native texture (Stage B)

yhhf88de5efd3d1
diff --git a/native/js/game.js b/native/js/game.js
index cdd8e4fd6..ec3012a7e 100644
--- a/native/js/game.js
+++ b/native/js/game.js
@@ -15,6 +15,7 @@ function init() {
world = app.world;
input = app.getResource(ESEngine.Input);
+ // Fallback 2x2 checker while the real image decodes.
var tex = es_createTexture(2, 2, [
255, 0, 0, 255, 0, 255, 0, 255,
0, 0, 255, 255, 255, 255, 0, 255 ]);
@@ -22,7 +23,16 @@ function init() {
follower = world.spawn();
world.insert(follower, ESEngine.Transform, { position: { x: W * 0.5, y: H * 0.5, z: 0 } });
world.insert(follower, ESEngine.Sprite, {
- texture: tex, color: { r: 1, g: 1, b: 1, a: 1 }, size: { x: S * 0.2, y: S * 0.2 } });
+ texture: tex, color: { r: 1, g: 1, b: 1, a: 1 }, size: { x: S * 0.28, y: S * 0.28 } });
+
+ // Load a REAL image (logo.png) from the APK through the SDK platform
+ // (NativeBridge.loadImagePixels), decoded by the host with stb_image, and swap
+ // it onto the follower — proving the native image-decode -> texture path.
+ globalThis.__esNativeBridge.loadImagePixels('logo.png').then(function (img) {
+ var logo = es_createTexture(img.width, img.height, img.pixels);
+ world.insert(follower, ESEngine.Sprite, {
+ texture: logo, color: { r: 1, g: 1, b: 1, a: 1 }, size: { x: S * 0.28, y: S * 0.28 } });
+ });
orbit = world.spawn();
world.insert(orbit, ESEngine.Transform, { position: { x: W * 0.5, y: H * 0.75, z: 0 } });
@@ -33,9 +43,12 @@ function init() {
function update(dt) {
t += dt;
app.tick(dt); // run the App loop (host pumps its async jobs)
- // Follow the touch. Android touch is top-left origin; the world is y-up.
+ // Follow the touch once there's been one (Android touch is top-left origin; the
+ // world is y-up). Before any touch, the logo sits centered.
var p = input.getMousePosition();
- world.insert(follower, ESEngine.Transform, { position: { x: p.x, y: H - p.y, z: 0 } });
+ if (p.x !== 0 || p.y !== 0) {
+ world.insert(follower, ESEngine.Transform, { position: { x: p.x, y: H - p.y, z: 0 } });
+ }
world.insert(orbit, ESEngine.Transform, {
position: { x: W * 0.5 + Math.cos(t * 1.2) * S * 0.3, y: H * 0.75, z: 0 } });
}
diff --git a/native/js/logo.png b/native/js/logo.png
new file mode 100644
index 000000000..adeaedd80
Binary files /dev/null and b/native/js/logo.png differ
diff --git a/native/js/main.cpp b/native/js/main.cpp
index 1f6e82244..db568f159 100644
--- a/native/js/main.cpp
+++ b/native/js/main.cpp
@@ -38,6 +38,11 @@
#include <glm/gtc/matrix_transform.hpp>
+// stb_image for the native image-decode path (NativeBridge.loadImagePixels). The
+// engine core doesn't use stb_image, so this TU owns the implementation.
+#define STB_IMAGE_IMPLEMENTATION
+#include "stb_image.h"
+
#include <cstdint>
#include <cstdio>
#include <cstring>
@@ -70,7 +75,11 @@ globalThis.__esNativeBridge = {
},
fileExists: function (path) { return Promise.resolve(es_readAsset(path) != null); },
fetch: function () { return Promise.resolve({ ok: false, status: 404 }); },
- loadImagePixels: function () { return Promise.reject(new Error('image decode not wired')); },
+ loadImagePixels: function (path) {
+ var r = es_loadImagePixels(path);
+ return r ? Promise.resolve({ width: r.width, height: r.height, pixels: new Uint8Array(r.pixels) })
+ : Promise.reject(new Error('image decode failed: ' + path));
+ },
getStorageItem: function () { return null; },
setStorageItem: function () {},
removeStorageItem: function () {},
@@ -237,6 +246,27 @@ JSValue js_readAsset(JSContext* ctx, JSValueConst, int, JSValueConst* argv) {
return JS_NewArrayBufferCopy(ctx, bytes.data(), bytes.size());
}
+// es_loadImagePixels(path) -> { width, height, pixels: ArrayBuffer(RGBA) } | null.
+// Decodes an APK image asset to top-first RGBA via stb_image — the native
+// NativeBridge.loadImagePixels ("Path 2": decode -> createTextureFromPixels).
+JSValue js_loadImagePixels(JSContext* ctx, JSValueConst, int, JSValueConst* argv) {
+ const char* path = JS_ToCString(ctx, argv[0]);
+ if (!path) return JS_NULL;
+ std::vector<u8> file = readAsset(*g_app, path);
+ JS_FreeCString(ctx, path);
+ if (file.empty()) return JS_NULL;
+ int w = 0, h = 0, ch = 0;
+ stbi_uc* px = stbi_load_from_memory(file.data(), (int)file.size(), &w, &h, &ch, 4);
+ if (!px) { LOGE("stb_image decode failed"); return JS_NULL; }
+ JSValue buf = JS_NewArrayBufferCopy(ctx, px, (size_t)w * (size_t)h * 4);
+ stbi_image_free(px);
+ JSValue obj = JS_NewObject(ctx);
+ JS_SetPropertyStr(ctx, obj, "width", JS_NewInt32(ctx, w));
+ JS_SetPropertyStr(ctx, obj, "height", JS_NewInt32(ctx, h));
+ JS_SetPropertyStr(ctx, obj, "pixels", buf); // ArrayBuffer; the bootstrap wraps it Uint8Array
+ return obj;
+}
+
void logJsError(JSContext* ctx, const char* where) {
JSValue e = JS_GetException(ctx);
const char* s = JS_ToCString(ctx, e);
@@ -378,6 +408,7 @@ void initJS(App& a) {
bindGlobal(a, global, "es_setClear", js_setClear, 3);
bindGlobal(a, global, "es_createTexture", js_createTexture, 3);
bindGlobal(a, global, "es_readAsset", js_readAsset, 1);
+ bindGlobal(a, global, "es_loadImagePixels", js_loadImagePixels, 1);
// Per-component bindings (EHT-generated): es_set_<C> / es_<C>_buffer / _has / _remove.
esn_register(a.js, global);
JS_FreeValue(a.js, global);