emscripten friendly sdl loop

This commit is contained in:
lizzie 2026-06-10 00:03:43 +00:00
parent 6247795f12
commit 9da56ebaba
6 changed files with 155 additions and 146 deletions

99
tools/miniserver.js Normal file → Executable file
View file

@ -1,4 +1,4 @@
#!/usr/local/env node
#!/usr/bin/env node
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
@ -21,51 +21,61 @@ const server = createServer((req, res) => {
<html>
<head>
<title>eden-cli</title>
</head>
<body style="margin:0;padding:0;background-color:black;font-family:Monospace,Tahoma,Arial;">
<script>
var Module = {}; // MUST be global otherwise wasm does wawawawa
function finished_body() {
Module = { //do not prepend var
print: (e) => {
e = e.replace('[1;31m', '<span style="color:red;font-weight:bold;">');
e = e.replace('[0;37m', '<span style="color:white;font-weight:bold;">');
e = e.replace('[1;35m', '<span style="color:pink;font-weight:bold;">');
e = e.replace('[1;33m', '<span style="color:yellow;font-weight:bold;">');
e = e.replace('[0m', '</span>');
tty_stdout.innerHTML += e + '</br>';
},
printErr: (e) => {
tty_stdout.innerHTML += '<span style="color:red">' + e + '</span></br>';
},
// not a wasm func but idc
printInternal: (e) => {
tty_stdout.innerHTML += '<span style="color:pink">Internal WASM: ' + e + '</span></br>';
},
onRuntimeInitialized: () => { Module.printInternal("runtime ok"); },
setStatus: (e) => { Module.printInternal(e); },
monitorRunDependencies: (e) => { Module.printInternal("monitor deps: " + e); },
__wasm_call_ctors: () => { Module.printInternal("ctors beep"); },
};
var Module = { //do not prepend var
mainScriptUrlOrBlob: 'eden-cli.js',
print: (e) => {
e = e.replace('[1;31m', '<span style="color:red;font-weight:bold;">');
e = e.replace('[0;37m', '<span style="color:white;font-weight:bold;">');
e = e.replace('[1;35m', '<span style="color:pink;font-weight:bold;">');
e = e.replace('[1;33m', '<span style="color:yellow;font-weight:bold;">');
e = e.replace('[0m', '</span>');
tty_stdout.innerHTML += \`\${e}</br>\`;
},
printErr: (e) => {
tty_stdout.innerHTML += \`<span style="color:red">\${e}</span></br>\`;
},
// not a wasm func but idc
printInternal: (e) => {
tty_stdout.innerHTML += \`<span style="color:pink">Internal WASM: \${e}</span></br>\`;
},
onRuntimeInitialized: () => { Module.printInternal("runtime ok"); },
setStatus: (e) => { Module.printInternal(e); },
monitorRunDependencies: (e) => { Module.printInternal("monitor deps: " + e); },
__wasm_call_ctors: () => { Module.printInternal("ctors beep"); },
};
var tty_stdout = document.createElement('div');
document.body.appendChild(tty_stdout);
Module.arguments = ['game.nro'];
var tty_stdout = document.createElement('div');
document.body.appendChild(tty_stdout);
Module.printInternal("loading from ${build_dir}/eden-cli.js");
var gameNroFileBuffer = {};
Module.arguments = ['game.nro'];
Module.printInternal("Atomics: " + window.Atomics);
Module.printInternal("SharedArrayBuffer: " + window.SharedArrayBuffer);
Module.printInternal("trying to load script (if it hangs here check console)");
fetch('game.nro').then((resp) => {
if (!resp.ok)
throw Error(\`\${resp.status}\`);
return resp.arrayBuffer();
}).then((buffer) => {
gameNroFileBuffer = buffer;
Module.printInternal("buffer: " + gameNroFileBuffer);
// load the thingy AFTER loading the nro
Module.printInternal(\`loading from ${build_dir}\${Module.mainScriptUrlOrBlob}\`);
var script = document.createElement('script');
script.src = '/eden-cli.js';
script.onload = () => {
Module.printInternal("Atomics: " + window.Atomics);
Module.printInternal("SharedArrayBuffer: " + window.SharedArrayBuffer);
Module.printInternal("trying to load script (if it hangs here check console)");
Module.FS.writeFile('/game.nro', [1,2,3]);
// copy just most relevant :)
console.log(Module);
Module.FS.writeFile('/game.nro', new DataView(gameNroFileBuffer));
};
document.head.appendChild(script);
}
}).catch(Module.printErr);
</script>
</head>
<body style="margin:0;padding:0;background-color:black;font-family:Monospace,Tahoma,Arial;" onload="finished_body();">
</body>
</html>`);
} else if (req.url === '/eden-cli.js') {
@ -86,12 +96,25 @@ function finished_body() {
});
res.end(content, 'utf-8');
});
} else if (req.url === '/game.nro') {
readFile(nro_file, (err, content) => {
res.writeHead(200, {
'Content-Type': 'application/wasm',
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp'
});
res.end(content, 'utf-8');
});
} else {
res.writeHead(404, {});
res.end('', 'utf-8');
}
});
const build_dir = process.argv[2];
if (build_dir === undefined) {
console.log(`Usage: ${process.argv[0]} ${process.argv[1]} [build directory]`);
const nro_file = process.argv[3];
if (typeof build_dir == "undefined" || typeof nro_file == "undefined") {
console.log(`Usage: ${process.argv[0]} ${process.argv[1]} [build directory] [NRO file]`);
} else {
server.listen(2210, () => {
console.log(`${process.argv[0]} ${process.argv[1]} http://localhost:2210`);