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.

fix(native): a packaged game starts drawing, in the orientation it was authored in

yhhdab62d2325f2
diff --git a/build-tools/tasks/native.js b/build-tools/tasks/native.js
index 4b9ef3dd4..de0cd2edb 100644
--- a/build-tools/tasks/native.js
+++ b/build-tools/tasks/native.js
@@ -154,8 +154,6 @@ async function generateSdkBundle(rootDir, genDir) {
async function precompileBundleBytecode(rootDir, genDir, quickjs) {
const source = path.join(genDir, 'esengine_bundle.embedded.js');
const out = path.join(genDir, BYTECODE_FILE);
- const exe = process.platform === 'win32' ? '.exe' : '';
- const tool = path.join(genDir, `mkbc${exe}`);
// The very sources the host links, so the bytecode it writes is the bytecode
// the host reads. Anything else is a format gamble.
@@ -167,15 +165,40 @@ async function precompileBundleBytecode(rootDir, genDir, quickjs) {
return null;
}
- const cc = process.platform === 'win32' ? 'gcc' : 'cc';
+ // Through CMake, which is already required to be here — the NDK toolchain
+ // builds the host with it. Invoking a compiler directly means naming one, and
+ // the name that was here (`gcc` on Windows) is the one a Windows machine set
+ // up for this build is least likely to have: it has MSVC, or the NDK's clang,
+ // under neither of those names. Skipping is silent apart from a warning, and
+ // what it costs is ~14s of black screen on first launch — a packaged game that
+ // looks hung. CMake is the one thing that finds whatever compiler is here.
+ const buildDir = path.join(genDir, 'mkbc-build');
try {
- await runCommand(cc, [
- '-std=c11', '-O2', '-w',
- '-I', quickjs,
- '-o', tool,
- path.join(rootDir, 'native', 'tools', 'mkbc.c'),
- ...qjsSources,
- ], { cwd: rootDir, silent: true });
+ await mkdir(buildDir, { recursive: true });
+ const sources = [path.join(rootDir, 'native', 'tools', 'mkbc.c'), ...qjsSources]
+ .map((f) => `"${fwd(f)}"`).join(' ');
+ writeFileSync(path.join(buildDir, 'CMakeLists.txt'), [
+ 'cmake_minimum_required(VERSION 3.20)',
+ 'project(mkbc C)',
+ 'set(CMAKE_C_STANDARD 11)',
+ `add_executable(mkbc ${sources})`,
+ `target_include_directories(mkbc PRIVATE "${fwd(quickjs)}")`,
+ 'if(MSVC)',
+ ' target_compile_options(mkbc PRIVATE /w)',
+ 'else()',
+ ' target_compile_options(mkbc PRIVATE -w)',
+ 'endif()',
+ ].join('\n') + '\n', 'utf8');
+ await runCommand('cmake', ['-S', buildDir, '-B', buildDir, '-DCMAKE_BUILD_TYPE=Release'],
+ { cwd: rootDir, silent: true });
+ await runCommand('cmake', ['--build', buildDir, '--config', 'Release'],
+ { cwd: rootDir, silent: true });
+ const exe = process.platform === 'win32' ? '.exe' : '';
+ // Single-config generators put it in the build dir; multi-config (MSBuild,
+ // Xcode) put it under the configuration.
+ const tool = [path.join(buildDir, `mkbc${exe}`), path.join(buildDir, 'Release', `mkbc${exe}`)]
+ .find(existsSync);
+ if (!tool) throw new Error('mkbc built but not found');
await runCommand(tool, [source, out], { cwd: rootDir, silent: true });
} catch (err) {
logger.warn('Bytecode precompile skipped (no host compiler?) — the app will '
diff --git a/desktop/scripts/export-project.mjs b/desktop/scripts/export-project.mjs
index 509e2928c..56b96fd71 100644
--- a/desktop/scripts/export-project.mjs
+++ b/desktop/scripts/export-project.mjs
@@ -73,6 +73,29 @@ async function loadExportGame() {
return { exportGame: mod.exportGame, cleanup: () => rmSync(dir, { recursive: true, force: true }) };
}
+/**
+ * The editor's own project-format helpers, bundled the same way.
+ *
+ * Reading the manifest by hand here is how this script drifts from the editor,
+ * which is the one thing it exists not to do: orientation was read as a
+ * top-level `orientation` field, a key the format does not have, so every
+ * project packaged landscape — a 600x1080 shmup included.
+ */
+async function loadProjectFormat() {
+ const require = createRequire(path.join(DESKTOP, 'package.json'));
+ const esbuild = require('esbuild');
+ const dir = mkdtempSync(path.join(DESKTOP, '.format-'));
+ const outfile = path.join(dir, 'format.mjs');
+ await esbuild.build({
+ entryPoints: [path.join(DESKTOP, 'src', 'project', 'format.ts')],
+ outfile, bundle: true, format: 'esm', platform: 'node', target: 'node20',
+ external: ['esbuild', 'electron', 'sharp'], logLevel: 'error',
+ });
+ const mod = await import(fileUrl(outfile));
+ rmSync(dir, { recursive: true, force: true });
+ return mod;
+}
+
/** The project's own settings, so a headless export matches what the editor would do. */
function projectSettings(projectDir) {
for (const name of ['project.esproject', 'project.esproj', 'project.json']) {
@@ -121,6 +144,7 @@ const androidTemplate = platform === 'android'
? path.resolve(opts.template)
: firstExisting([installedTemplateDir(engineVersion, 'android')]) ?? null)
: null;
+const { resolveOrientation } = await loadProjectFormat();
const { exportGame, cleanup } = await loadExportGame();
let code = 1;
try {
@@ -134,7 +158,7 @@ try {
outDir,
platform,
title: opts.title ?? project.name ?? path.basename(opts.projectDir),
- orientation: project.orientation ?? 'landscape',
+ orientation: resolveOrientation(project),
androidTemplate,
androidOutput: opts.output === 'project' ? 'project' : undefined,
});