winzik/engine/web/tauri/game.html
David Nurdin 40fe26d4f8 Hybrid games in one window: the SDL game drawn inside its Tauri window (app.embed)
- game.yaml ui: hybrid + app: {embed: true}: SDL draws into memory (no SDL window, video driver dummy), each picture
  (a BMP) goes to the runtime through jt_frame(); JumPHP's page engine/web/tauri/game.html (served as
  /__jumphp/game.html) draws the newest one on a canvas, sharp and letterboxed, and sends keyboard, mouse and focus
  back into the engine's InputManager (JumPHP\Tauri\EmbeddedGame); F11 makes the Tauri window fullscreen
- runtime: jt_frame(), the jumphpframe:// scheme (204 while no newer picture), engine pages under /__jumphp/
- Engine emits 'rendered' after each frame and 'fullscreen-request' without an SDL window; Sdl::isEmbedded();
  NativeRuntime::picture()
- stage-game.sh: apps and hybrid games carry engine/web/tauri (games' packages unchanged)
- tried for real with Pong on Xvfb: the menu in the Tauri window, Enter from the page started a game

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01JkYTqpYNZKwTnRZHQRD5HP
2026-11-01 08:38:55 +01:00

91 lines
3.8 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JumPHP</title>
<!--
A hybrid game embedded in its Tauri window (game.yaml ui: hybrid + app: {embed: true}; JumPHP\Tauri\EmbeddedGame):
the game's pictures come from the runtime (jumphpframe://), drawn sharp and letterboxed; the keyboard, the mouse
and the focus go back to PHP (jumphp.send('__input')). Served as /__jumphp/game.html.
-->
<style>
html, body { margin: 0; height: 100%; overflow: hidden; background: #000; }
body { display: flex; align-items: center; justify-content: center; }
canvas { image-rendering: pixelated; image-rendering: crisp-edges; display: block; outline: none; }
</style>
</head>
<body>
<canvas id="game" tabindex="0"></canvas>
<script>
(async () => {
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const info = await jumphp.invoke('__embed.info');
canvas.width = info.width;
canvas.height = info.height;
const bg = Array.isArray(info.background) ? info.background : [0, 0, 0];
document.body.style.background = `rgb(${bg[0] | 0}, ${bg[1] | 0}, ${bg[2] | 0})`;
// letterboxed, as big as the window allows (whole factors with integer_scale)
const fit = () => {
let s = Math.min(innerWidth / info.width, innerHeight / info.height);
if (info.integer_scale && s >= 1) s = Math.floor(s);
canvas.style.width = `${Math.round(info.width * s)}px`;
canvas.style.height = `${Math.round(info.height * s)}px`;
};
addEventListener('resize', fit);
fit();
canvas.focus();
// the pictures: the newest one, once per screen refresh (a 204 while there is none newer)
const base = navigator.userAgent.includes('Windows') ? 'http://jumphpframe.localhost/frame' : 'jumphpframe://localhost/frame';
let last = 0;
const nextPaint = () => new Promise((resolve) => requestAnimationFrame(resolve));
for (;;) {
try {
const r = await fetch(`${base}?after=${last}`, {cache: 'no-store'});
if (r.status === 200) {
last = Number(r.headers.get('X-Frame')) || last + 1;
const picture = await createImageBitmap(await r.blob());
ctx.drawImage(picture, 0, 0);
picture.close();
}
} catch (e) {
// the runtime is closing, or a picture was cut: the next one
}
await nextPaint();
}
})();
// input -> PHP (EmbeddedGame::input)
(() => {
const canvas = document.getElementById('game');
const send = (e) => jumphp.send('__input', e);
// keys the page would otherwise use itself (scrolling, focus moves, find...)
const keep = new Set(['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' ', 'Tab', 'Backspace', 'F1', 'F3', 'F5', 'F7', 'F11', 'Escape', 'Enter']);
addEventListener('keydown', (e) => {
if (keep.has(e.key) || e.key.length === 1) e.preventDefault();
if (!e.repeat) send({t: 'down', key: e.key, code: e.code});
});
addEventListener('keyup', (e) => send({t: 'up', key: e.key, code: e.code}));
addEventListener('blur', () => send({t: 'blur'}));
// the mouse, in the game's own pixels; moves once per screen refresh at most
let move = null;
const at = (e) => {
const r = canvas.getBoundingClientRect();
return {x: (e.clientX - r.left) * canvas.width / r.width, y: (e.clientY - r.top) * canvas.height / r.height};
};
canvas.addEventListener('mousemove', (e) => {
const first = move === null;
move = at(e);
if (first) requestAnimationFrame(() => { send({t: 'move', ...move}); move = null; });
});
canvas.addEventListener('mousedown', (e) => { canvas.focus(); send({t: 'move', ...at(e)}); send({t: 'mdown', b: e.button + 1}); });
addEventListener('mouseup', (e) => send({t: 'mup', b: e.button + 1}));
canvas.addEventListener('contextmenu', (e) => e.preventDefault());
})();
</script>
</body>
</html>