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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
* @file What the quick-game hosts needed that no other vendor did, each found
* by running a package on vivo's engine: a window measured in physical
* pixels (touches too), WebAssembly behind a switch, and a WebGL2 context that offers
* an extension WebGL2 does not have.
*/
import { describe, it, expect, vi, afterEach } from 'vitest';
import { MiniGamePlatformAdapter } from '../src/platform/minigame/adapter';
import type { MiniGameGlobal, MiniGameProfile } from '../src/platform/minigame/api';
import { quickgameProfile } from '../src/platform/quickgame/profile';
import { hideCoreExtensions } from '../src/runtime/miniGameRuntime';
const EMPTY_WASM = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]).buffer;
afterEach(() => { delete (globalThis as { qg?: unknown }).qg; });
describe('the quick-game hosts', () => {
it('size the screen from a window already in physical pixels', () => {
const host = (): MiniGameGlobal => ({
createCanvas: () => ({ width: 0, height: 0 }),
getSystemInfoSync: () => ({ windowWidth: 2340, windowHeight: 1080, pixelRatio: 2.75 }),
} as unknown as MiniGameGlobal);
const screen = (over: Partial<MiniGameProfile>) =>
new MiniGamePlatformAdapter({ id: 'x', hostLabel: 'x', global: host(), ...over } as MiniGameProfile).createScreenCanvas();
expect(screen({ windowInPhysicalPixels: true })).toMatchObject({ width: 2340, height: 1080 });
expect(screen({})).toMatchObject({ width: 6435, height: 2970 });
});
it('take a touch measured in physical pixels as the logical position every pointer reader scales', () => {
let touchStart: ((res: unknown) => void) | undefined;
const downs: number[][] = [];
const adapter = new MiniGamePlatformAdapter({
id: 'x', hostLabel: 'x', windowInPhysicalPixels: true,
global: {
getSystemInfoSync: () => ({ windowWidth: 2340, windowHeight: 1080, pixelRatio: 2.75 }),
onTouchStart: (cb: (res: unknown) => void) => { touchStart = cb; },
onTouchMove: () => {}, onTouchEnd: () => {},
} as unknown as MiniGameGlobal,
} as MiniGameProfile);
adapter.bindInputEvents({
onKeyDown: () => {}, onKeyUp: () => {}, onPointerMove: () => {}, onPointerUp: () => {}, onWheel: () => {},
onPointerDown: (_b, x, y) => downs.push([x, y]),
});
touchStart!({ touches: [], changedTouches: [{ identifier: 0, clientX: 2200, clientY: 990 }] });
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 = {
setWasmTaskCompile: (on: boolean) => order.push(`switch ${on}`),
getFileSystemManager: () => ({ readFileSync: (p: string) => { order.push(`read ${p}`); return EMPTY_WASM; } }),
};
const res = await quickgameProfile.instantiateWasm!('wasm/esengine.wxgame.wasm', {});
expect(order).toEqual(['switch true', 'read wasm/esengine.wxgame.wasm']);
expect(res.instance).toBeInstanceOf(WebAssembly.Instance);
});
it('withhold from a WebGL2 context the WebGL1 extensions it made core', () => {
const vao = { createVertexArrayOES: vi.fn() };
const other = {};
const gl = { getExtension: (name: string) => (name === 'OES_vertex_array_object' ? vao : other) };
hideCoreExtensions(gl as unknown as WebGLRenderingContext);
expect(gl.getExtension('OES_vertex_array_object')).toBeNull();
expect(gl.getExtension('ANGLE_instanced_arrays')).toBeNull();
expect(gl.getExtension('EXT_color_buffer_float')).toBe(other);
});
});