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
fix(minigame): the pixel ratio is read once, not on every frame
esenginea1f9159b7f6a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8a65318d2..9c3a92573 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,6 +24,7 @@ published separately; it ships inside the editor.### Fixed+- A mini-game reads the host's pixel ratio once and again after a resize, instead of calling getSystemInfoSync several times a frame (on vivo, a synchronous call across its bridge each time).- `createButton({ userGesture: true })` runs its click inside the host's own release event: on a WeChat phone, sharing a recording from a button was refused ("require user interaction (only in touchend event)"), because a click ran on the frame after the tap.- Mini-game exports name assets by their content, as web exports already did: a hot update to a remote texture now shows on a phone (it kept the old picture under the same URL), and a CDN can hold two versions without v1 players getting v2 files.- On a WeChat phone, a socket whose connection is refused now reports that it closed, so a development build keeps redialing the editor (and a game's socket reconnects) instead of giving up after the first refusal.diff --git a/sdk/src/platform/minigame/adapter.ts b/sdk/src/platform/minigame/adapter.tsindex 5751f4736..33667f3b9 100644--- a/sdk/src/platform/minigame/adapter.ts+++ b/sdk/src/platform/minigame/adapter.ts@@ -56,6 +56,7 @@ export class MiniGamePlatformAdapter implements PlatformAdapter {private readonly g_: MiniGameGlobal;private fs_: MiniGameFileSystemManager | null = null;private inputCleanup_: (() => void) | null = null;+ private pixelRatio_: number | null = null;private openDataCtx_: MiniGameOpenDataContext | null = null;private openDataResolved_ = false;private recorder_: PlatformScreenRecorder | null | undefined;@@ -449,7 +450,8 @@ export class MiniGamePlatformAdapter implements PlatformAdapter {canvas: MiniGameCanvas,size: { windowWidth?: number; windowHeight?: number },): void {- const dpr = this.profile_.windowInPhysicalPixels ? 1 : (this.g_.getSystemInfoSync().pixelRatio ?? 1);+ this.pixelRatio_ = null;+ const dpr = this.profile_.windowInPhysicalPixels ? 1 : this.devicePixelRatio();if (size.windowWidth) canvas.width = size.windowWidth * dpr;if (size.windowHeight) canvas.height = size.windowHeight * dpr;}@@ -562,12 +564,16 @@ export class MiniGamePlatformAdapter implements PlatformAdapter {}}+ /** Read once and again after a resize: the UI, the cameras and text ask every+ * frame, and on vivo each read is a synchronous call across its bridge. */devicePixelRatio(): number {+ if (this.pixelRatio_ !== null) return this.pixelRatio_;try {- return this.g_.getSystemInfoSync?.()?.pixelRatio ?? 1;+ this.pixelRatio_ = this.g_.getSystemInfoSync?.()?.pixelRatio ?? 1;} catch {- return 1;+ this.pixelRatio_ = 1;}+ return this.pixelRatio_;}deviceName(): string {diff --git a/sdk/tests/quickgame-profile.test.ts b/sdk/tests/quickgame-profile.test.tsindex 8f32e5d59..18e9702c9 100644--- a/sdk/tests/quickgame-profile.test.ts+++ b/sdk/tests/quickgame-profile.test.ts@@ -47,6 +47,28 @@ describe('the quick-game hosts', () => {expect(downs).toEqual([[800, 360]]);});+ it('ask the host for the pixel ratio once, and again after the window changes', () => {+ let reads = 0;+ let ratio = 2.75;+ let onResize: ((res: { windowWidth: number; windowHeight: number }) => void) | undefined;+ const adapter = new MiniGamePlatformAdapter({+ id: 'x', hostLabel: 'x', windowInPhysicalPixels: true,+ global: {+ createCanvas: () => ({ width: 0, height: 0 }),+ getSystemInfoSync: () => { reads++; return { windowWidth: 2340, windowHeight: 1080, pixelRatio: ratio }; },+ onWindowResize: (cb: typeof onResize) => { onResize = cb; },+ } as unknown as MiniGameGlobal,+ } as MiniGameProfile);+ adapter.createScreenCanvas();+ const before = reads;+ for (let i = 0; i < 60; i++) adapter.devicePixelRatio();+ expect(reads - before).toBeLessThanOrEqual(1);+ expect(adapter.devicePixelRatio()).toBe(2.75);+ ratio = 3;+ onResize!({ windowWidth: 1080, windowHeight: 2340 });+ expect(adapter.devicePixelRatio()).toBe(3);+ });+it('turn WebAssembly on before the engine is instantiated', async () => {const order: string[] = [];(globalThis as { qg?: unknown }).qg = {diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex a839d9bd0..36c5d282a 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -1116,4 +1116,6 @@ export const NOTED = {{ note: 'Mini-game exports name assets by their content, as web exports already did: a hot update to a remote texture now shows on a phone (it kept the old picture under the same URL), and a CDN can hold two versions without v1 players getting v2 files.' },'fix(ui): a button can act inside the player\'s tap, where WeChat requires a share':{ note: '`createButton({ userGesture: true })` runs its click inside the host\'s own release event: on a WeChat phone, sharing a recording from a button was refused ("require user interaction (only in touchend event)"), because a click ran on the frame after the tap.' },+ 'fix(minigame): the pixel ratio is read once, not on every frame':+ { note: 'A mini-game reads the host\'s pixel ratio once and again after a resize, instead of calling getSystemInfoSync several times a frame (on vivo, a synchronous call across its bridge each time).' },};