c64-ready 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +332 -96
- package/bin/headless.mjs +7 -3
- package/bin/serve.mjs +162 -0
- package/dist/assets/CHANGELOG-C1nvP1iO.js +96 -0
- package/dist/assets/index-Cioamf6E.js +394 -0
- package/dist/assets/marked.esm-BOXuzyNF.js +37 -0
- package/dist/assets/purify.es-y41lKIN9.js +2 -0
- package/dist/audio-worklet-processor.js +83 -0
- package/dist/c64-ready/c64-ready.gif +0 -0
- package/dist/c64-ready/index.html +58 -0
- package/dist/c64.wasm +0 -0
- package/dist/games/cartridges/legend-of-wilf.crt +0 -0
- package/dist/index.html +38 -0
- package/dist/led-off.svg +20 -0
- package/dist/led-on.svg +27 -0
- package/dist/vite.svg +15 -0
- package/dist-ts/emulator/c64-emulator.d.ts +107 -0
- package/dist-ts/emulator/c64-emulator.d.ts.map +1 -0
- package/dist-ts/emulator/c64-emulator.js +13 -1
- package/dist-ts/emulator/c64-emulator.js.map +1 -0
- package/dist-ts/emulator/c64-wasm.d.ts +42 -0
- package/dist-ts/emulator/c64-wasm.d.ts.map +1 -0
- package/dist-ts/emulator/c64-wasm.js +1 -0
- package/dist-ts/emulator/c64-wasm.js.map +1 -0
- package/dist-ts/emulator/constants.d.ts +14 -0
- package/dist-ts/emulator/constants.d.ts.map +1 -0
- package/dist-ts/emulator/constants.js +1 -0
- package/dist-ts/emulator/constants.js.map +1 -0
- package/dist-ts/emulator/input.d.ts +33 -0
- package/dist-ts/emulator/input.d.ts.map +1 -0
- package/dist-ts/emulator/input.js +26 -5
- package/dist-ts/emulator/input.js.map +1 -0
- package/dist-ts/emulator/memory.d.ts +2 -0
- package/dist-ts/emulator/memory.d.ts.map +1 -0
- package/dist-ts/emulator/memory.js +9 -0
- package/dist-ts/emulator/memory.js.map +1 -0
- package/dist-ts/headless/audio-capture.d.ts +13 -0
- package/dist-ts/headless/audio-capture.d.ts.map +1 -0
- package/dist-ts/headless/audio-capture.js +24 -0
- package/dist-ts/headless/audio-capture.js.map +1 -0
- package/dist-ts/headless/c64-headless.d.ts +36 -0
- package/dist-ts/headless/c64-headless.d.ts.map +1 -0
- package/dist-ts/headless/c64-headless.js +87 -0
- package/dist-ts/headless/c64-headless.js.map +1 -0
- package/dist-ts/headless/ffmpeg-runner.d.ts +18 -0
- package/dist-ts/headless/ffmpeg-runner.d.ts.map +1 -0
- package/dist-ts/headless/ffmpeg-runner.js +67 -0
- package/dist-ts/headless/ffmpeg-runner.js.map +1 -0
- package/dist-ts/headless/frame-capture.d.ts +13 -0
- package/dist-ts/headless/frame-capture.d.ts.map +1 -0
- package/dist-ts/headless/frame-capture.js +24 -0
- package/dist-ts/headless/frame-capture.js.map +1 -0
- package/dist-ts/headless/input-bridge.d.ts +21 -0
- package/dist-ts/headless/input-bridge.d.ts.map +1 -0
- package/dist-ts/headless/input-bridge.js +41 -0
- package/dist-ts/headless/input-bridge.js.map +1 -0
- package/dist-ts/src/counter.js +9 -0
- package/dist-ts/src/emulator/c64-emulator.js +308 -0
- package/dist-ts/src/emulator/c64-emulator.test.js +104 -0
- package/dist-ts/src/emulator/c64-wasm.js +149 -0
- package/dist-ts/src/emulator/c64-wasm.test.js +65 -0
- package/dist-ts/src/emulator/constants.js +9 -0
- package/dist-ts/src/emulator/input.js +62 -0
- package/dist-ts/src/emulator/input.test.js +53 -0
- package/dist-ts/src/emulator/memory.js +1 -0
- package/dist-ts/src/headless/audio-capture.js +23 -0
- package/dist-ts/src/headless/c64-headless.js +85 -0
- package/dist-ts/src/headless/ffmpeg-runner.js +33 -0
- package/dist-ts/src/headless/frame-capture.js +23 -0
- package/dist-ts/src/headless/headless.cli.test.js +46 -0
- package/dist-ts/src/headless/input-bridge.js +37 -0
- package/dist-ts/src/main.js +57 -0
- package/dist-ts/src/player/audio-engine.js +175 -0
- package/dist-ts/src/player/audio-engine.test.js +284 -0
- package/dist-ts/src/player/c64-player.js +105 -0
- package/dist-ts/src/player/c64-player.test.js +165 -0
- package/dist-ts/src/player/canvas-renderer.js +148 -0
- package/dist-ts/src/player/canvas-renderer.test.js +135 -0
- package/dist-ts/src/player/input-handler.js +13 -0
- package/dist-ts/src/player/ui-controller.js +575 -0
- package/dist-ts/src/player/ui-controller.test.js +67 -0
- package/dist-ts/src/types/emulator.js +1 -0
- package/dist-ts/src/types/index.js +4 -0
- package/dist-ts/tools/run-wrapper.js +36 -0
- package/dist-ts/types/emulator.d.ts +128 -0
- package/dist-ts/types/emulator.d.ts.map +1 -0
- package/dist-ts/types/emulator.js +2 -0
- package/dist-ts/types/emulator.js.map +1 -0
- package/dist-ts/types/index.d.ts +51 -0
- package/dist-ts/types/index.d.ts.map +1 -0
- package/dist-ts/types/index.js +5 -0
- package/dist-ts/types/index.js.map +1 -0
- package/package.json +40 -6
- package/public/audio-worklet-processor.js +83 -0
- package/public/led-off.svg +20 -0
- package/public/led-on.svg +27 -0
- package/src/counter.ts +9 -0
- package/src/emulator/c64-emulator.ts +357 -0
- package/src/emulator/c64-wasm.ts +180 -0
- package/src/emulator/constants.ts +22 -0
- package/src/emulator/input.ts +105 -0
- package/src/emulator/memory.ts +9 -0
- package/src/headless/headless-cli.mjs +105 -19
- package/src/headless/input-bridge.ts +13 -4
- package/src/headless/input-server.mjs +76 -0
- package/src/main.ts +80 -0
- package/src/player/audio-engine.ts +216 -0
- package/src/player/c64-player.ts +222 -0
- package/src/player/canvas-renderer.ts +139 -0
- package/src/player/input-handler.ts +116 -0
- package/src/player/styles/canvas-renderer.css +41 -0
- package/src/player/styles/ui-controller.css +286 -0
- package/src/player/ui-controller.ts +485 -0
- package/src/style.css +96 -0
- package/src/types/emulator.ts +167 -0
- package/src/types/index.ts +62 -0
- package/src/typescript.svg +1 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { C64Emulator } from '../emulator/c64-emulator';
|
|
2
|
+
import type { GameLoadOptions } from '../types';
|
|
3
|
+
import type { JoystickPort } from '../emulator/constants';
|
|
4
|
+
import type CanvasRenderer from './canvas-renderer';
|
|
5
|
+
import { AudioEngine } from './audio-engine';
|
|
6
|
+
import InputHandler from './input-handler';
|
|
7
|
+
|
|
8
|
+
export type ProgressCallback = (percent: number, label: string) => void;
|
|
9
|
+
|
|
10
|
+
export interface C64PlayerOptions {
|
|
11
|
+
wasmUrl: string;
|
|
12
|
+
gameUrl: string;
|
|
13
|
+
gameType?: GameLoadOptions['type'];
|
|
14
|
+
renderer: CanvasRenderer;
|
|
15
|
+
onProgress?: ProgressCallback;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class C64Player {
|
|
19
|
+
private emulator: C64Emulator | null = null;
|
|
20
|
+
private inputHandler: InputHandler | null = null;
|
|
21
|
+
readonly audio = new AudioEngine();
|
|
22
|
+
private readonly options: Required<Pick<C64PlayerOptions, 'wasmUrl' | 'gameUrl' | 'gameType'>> &
|
|
23
|
+
C64PlayerOptions;
|
|
24
|
+
|
|
25
|
+
constructor(options: C64PlayerOptions) {
|
|
26
|
+
this.options = { gameType: 'crt', ...options };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async start(): Promise<void> {
|
|
30
|
+
const { wasmUrl, gameUrl, gameType, renderer, onProgress } = this.options;
|
|
31
|
+
|
|
32
|
+
onProgress?.(10, 'INITIALISING WASM...');
|
|
33
|
+
this.emulator = await C64Emulator.load(wasmUrl);
|
|
34
|
+
|
|
35
|
+
this.inputHandler = new InputHandler(this.emulator);
|
|
36
|
+
this.inputHandler.attach();
|
|
37
|
+
renderer.attachTo(this.emulator);
|
|
38
|
+
|
|
39
|
+
await this.loadGame(gameUrl, gameType, onProgress);
|
|
40
|
+
|
|
41
|
+
this.emulator.start();
|
|
42
|
+
|
|
43
|
+
// Initialise audio in the background — never blocks game startup
|
|
44
|
+
this.initAudio();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Set up the AudioEngine and wire it to the emulator.
|
|
49
|
+
*
|
|
50
|
+
* The SID fills its audio buffer continuously as CPU cycles run.
|
|
51
|
+
* The AudioEngine pulls a snapshot of that buffer on its own timer
|
|
52
|
+
* (matching the rate of the original ScriptProcessorNode), keeping
|
|
53
|
+
* audio and emulation timing independent.
|
|
54
|
+
*/
|
|
55
|
+
private initAudio(): void {
|
|
56
|
+
if (!this.emulator) return;
|
|
57
|
+
const emulator = this.emulator;
|
|
58
|
+
|
|
59
|
+
// Notify the UI whenever audio state changes
|
|
60
|
+
this.audio.onStateChange = (state) => {
|
|
61
|
+
window.dispatchEvent(new CustomEvent('c64-audio-state', { detail: state }));
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Fire-and-forget init
|
|
65
|
+
this.audio.init().then((autoplayOk) => {
|
|
66
|
+
// Tell the SID what sample rate we're using (matches AudioContext)
|
|
67
|
+
emulator.setSampleRate(this.audio.sampleRate);
|
|
68
|
+
|
|
69
|
+
// Give the engine a reader that snapshots the SID circular buffer
|
|
70
|
+
this.audio.setSidBufferReader(() => emulator.getSidBuffer());
|
|
71
|
+
|
|
72
|
+
if (!autoplayOk) {
|
|
73
|
+
window.dispatchEvent(new CustomEvent('c64-audio-suspended'));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async loadGame(
|
|
79
|
+
url: string,
|
|
80
|
+
type: GameLoadOptions['type'] = 'crt',
|
|
81
|
+
onProgress?: ProgressCallback,
|
|
82
|
+
): Promise<void> {
|
|
83
|
+
if (!this.emulator) throw new Error('Emulator not initialised — call start() first');
|
|
84
|
+
|
|
85
|
+
onProgress?.(20, 'LOADING GAME...');
|
|
86
|
+
|
|
87
|
+
const response = await fetch(url);
|
|
88
|
+
if (!response.ok) {
|
|
89
|
+
throw new Error(`Failed to fetch game: ${response.status}`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const contentLength = Number(response.headers.get('content-length') ?? 0);
|
|
93
|
+
let data: Uint8Array;
|
|
94
|
+
|
|
95
|
+
if (contentLength > 0 && response.body) {
|
|
96
|
+
const reader = response.body.getReader();
|
|
97
|
+
const chunks: Uint8Array[] = [];
|
|
98
|
+
let loaded = 0;
|
|
99
|
+
|
|
100
|
+
let result = await reader.read();
|
|
101
|
+
while (!result.done) {
|
|
102
|
+
chunks.push(result.value);
|
|
103
|
+
loaded += result.value.byteLength;
|
|
104
|
+
const pct = 20 + Math.round((loaded / contentLength) * 65);
|
|
105
|
+
onProgress?.(pct, `LOADING GAME... ${Math.round((loaded / contentLength) * 100)}%`);
|
|
106
|
+
result = await reader.read();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
data = new Uint8Array(loaded);
|
|
110
|
+
let offset = 0;
|
|
111
|
+
for (const chunk of chunks) {
|
|
112
|
+
data.set(chunk, offset);
|
|
113
|
+
offset += chunk.byteLength;
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
data = new Uint8Array(await response.arrayBuffer());
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
onProgress?.(90, 'INSERTING CARTRIDGE...');
|
|
120
|
+
// Basic validation for cartridge files — some malformed .crt files can
|
|
121
|
+
// silently fail inside the WASM loader; catch common format problems here
|
|
122
|
+
if (type === 'crt' && !isValidCRT(data)) {
|
|
123
|
+
onProgress?.(0, 'INVALID CRT');
|
|
124
|
+
const err = new Error('Invalid CRT file format');
|
|
125
|
+
console.error(err);
|
|
126
|
+
throw err;
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
this.emulator.loadGame({ type, data });
|
|
130
|
+
onProgress?.(100, 'READY!');
|
|
131
|
+
// Notify UI that load succeeded
|
|
132
|
+
window.dispatchEvent(new CustomEvent('c64-load-success', { detail: { url, type } }));
|
|
133
|
+
} catch (err) {
|
|
134
|
+
onProgress?.(0, 'FAILED');
|
|
135
|
+
console.error('Failed to insert cartridge or load game:', err);
|
|
136
|
+
// Surface a global event so UI can react if needed
|
|
137
|
+
window.dispatchEvent(
|
|
138
|
+
new CustomEvent('c64-load-error', { detail: { error: String(err), url, type } }),
|
|
139
|
+
);
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Load a game from a File/Blob provided by the user (drag & drop / file input)
|
|
145
|
+
async loadFile(file: File, type: GameLoadOptions['type'] = 'crt'): Promise<void> {
|
|
146
|
+
if (!this.emulator) throw new Error('Emulator not initialised — call start() first');
|
|
147
|
+
const onProgress = this.options.onProgress;
|
|
148
|
+
try {
|
|
149
|
+
onProgress?.(20, `READING FILE ${file.name}...`);
|
|
150
|
+
const ab = await file.arrayBuffer();
|
|
151
|
+
const data = new Uint8Array(ab);
|
|
152
|
+
onProgress?.(90, 'INSERTING CARTRIDGE...');
|
|
153
|
+
if (type === 'crt' && !isValidCRT(data)) {
|
|
154
|
+
onProgress?.(0, 'INVALID CRT');
|
|
155
|
+
const err = new Error('Invalid CRT file format');
|
|
156
|
+
console.error(err);
|
|
157
|
+
throw err;
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
this.emulator.loadGame({ type, data });
|
|
161
|
+
onProgress?.(100, 'READY!');
|
|
162
|
+
window.dispatchEvent(
|
|
163
|
+
new CustomEvent('c64-close-dialog', { detail: { file: file.name, type } }),
|
|
164
|
+
);
|
|
165
|
+
} catch (err) {
|
|
166
|
+
onProgress?.(0, 'FAILED');
|
|
167
|
+
console.error('Failed to insert cartridge from file:', err);
|
|
168
|
+
window.dispatchEvent(
|
|
169
|
+
new CustomEvent('c64-load-error', {
|
|
170
|
+
detail: { error: String(err), file: file.name, type },
|
|
171
|
+
}),
|
|
172
|
+
);
|
|
173
|
+
throw err;
|
|
174
|
+
}
|
|
175
|
+
} catch (err) {
|
|
176
|
+
onProgress?.(0, 'FAILED');
|
|
177
|
+
throw err;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Expose cartridge / reset controls for UI
|
|
182
|
+
detachCartridge(): void {
|
|
183
|
+
if (!this.emulator) return;
|
|
184
|
+
try {
|
|
185
|
+
this.emulator.removeCartridge();
|
|
186
|
+
window.dispatchEvent(new CustomEvent('c64-detach', { detail: {} }));
|
|
187
|
+
window.dispatchEvent(new CustomEvent('c64-close-menu'));
|
|
188
|
+
} catch (e) {
|
|
189
|
+
console.error('Failed to detach cartridge:', e);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
hardReset(): void {
|
|
194
|
+
if (!this.emulator) return;
|
|
195
|
+
try {
|
|
196
|
+
this.emulator.reset();
|
|
197
|
+
// Ensure emulator resumes after reset
|
|
198
|
+
this.emulator.start();
|
|
199
|
+
window.dispatchEvent(new CustomEvent('c64-hard-reset', { detail: {} }));
|
|
200
|
+
window.dispatchEvent(new CustomEvent('c64-close-menu'));
|
|
201
|
+
} catch (e) {
|
|
202
|
+
console.error('Failed to perform hard reset:', e);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Change which joystick port keyboard events map to (1 or 2).
|
|
208
|
+
* Delegates to the InputHandler created during start().
|
|
209
|
+
*/
|
|
210
|
+
setKeyboardJoystickPort(port: JoystickPort): void {
|
|
211
|
+
this.inputHandler?.setKeyboardJoystickPort(port);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Simple CRT format validator
|
|
216
|
+
function isValidCRT(data: Uint8Array): boolean {
|
|
217
|
+
// CRT files typically start with the ASCII header 'C64 CARTRIDGE' within the first 16 bytes
|
|
218
|
+
if (!data || data.length < 16) return false;
|
|
219
|
+
const header = String.fromCharCode(...Array.from(data.slice(0, 16)));
|
|
220
|
+
console.log('[c64-player]: header=', header);
|
|
221
|
+
return header.includes('C64 CARTRIDGE') || header.includes('C64 CARTRIDGE');
|
|
222
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { FrameBuffer } from '../types';
|
|
2
|
+
import type { C64Emulator } from '../emulator/c64-emulator';
|
|
3
|
+
|
|
4
|
+
import loaderCss from './styles/canvas-renderer.css?raw';
|
|
5
|
+
|
|
6
|
+
export default class CanvasRenderer {
|
|
7
|
+
private readonly canvas: HTMLCanvasElement;
|
|
8
|
+
private readonly ctx: CanvasRenderingContext2D;
|
|
9
|
+
private animationFrameId: number | null = null;
|
|
10
|
+
|
|
11
|
+
// Loader elements (created lazily)
|
|
12
|
+
private loaderEl: HTMLElement | null = null;
|
|
13
|
+
private loaderLabel: HTMLElement | null = null;
|
|
14
|
+
private loaderFill: HTMLElement | null = null;
|
|
15
|
+
|
|
16
|
+
constructor(canvasOrId: string | HTMLCanvasElement) {
|
|
17
|
+
const canvas =
|
|
18
|
+
typeof canvasOrId === 'string' ? document.getElementById(canvasOrId) : canvasOrId;
|
|
19
|
+
|
|
20
|
+
if (!(canvas instanceof HTMLCanvasElement)) {
|
|
21
|
+
throw new Error(`Canvas not found: ${String(canvasOrId)}`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const ctx = canvas.getContext('2d');
|
|
25
|
+
if (!ctx) {
|
|
26
|
+
throw new Error('2D canvas context not available');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.canvas = canvas;
|
|
30
|
+
this.ctx = ctx;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ── Loading progress bar ──────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
private ensureLoader(): void {
|
|
36
|
+
if (this.loaderEl) return;
|
|
37
|
+
|
|
38
|
+
if (!document.querySelector('style[data-c64-loader]')) {
|
|
39
|
+
const style = document.createElement('style');
|
|
40
|
+
style.setAttribute('data-c64-loader', '');
|
|
41
|
+
style.textContent = loaderCss;
|
|
42
|
+
document.head.appendChild(style);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const overlay = document.createElement('div');
|
|
46
|
+
overlay.className = 'c64-loader';
|
|
47
|
+
|
|
48
|
+
const label = document.createElement('div');
|
|
49
|
+
label.className = 'c64-loader-label';
|
|
50
|
+
label.textContent = 'LOADING...';
|
|
51
|
+
|
|
52
|
+
const track = document.createElement('div');
|
|
53
|
+
track.className = 'c64-loader-track';
|
|
54
|
+
|
|
55
|
+
const fill = document.createElement('div');
|
|
56
|
+
fill.className = 'c64-loader-fill';
|
|
57
|
+
|
|
58
|
+
track.appendChild(fill);
|
|
59
|
+
overlay.appendChild(label);
|
|
60
|
+
overlay.appendChild(track);
|
|
61
|
+
|
|
62
|
+
this.canvas.insertAdjacentElement('afterend', overlay);
|
|
63
|
+
|
|
64
|
+
this.loaderEl = overlay;
|
|
65
|
+
this.loaderLabel = label;
|
|
66
|
+
this.loaderFill = fill;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
setProgress(percent: number, label: string): void {
|
|
70
|
+
this.ensureLoader();
|
|
71
|
+
this.loaderFill!.style.width = `${Math.min(percent, 100)}%`;
|
|
72
|
+
this.loaderLabel!.textContent = label;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setError(message: string): void {
|
|
76
|
+
this.ensureLoader();
|
|
77
|
+
this.loaderFill!.style.width = '0';
|
|
78
|
+
this.loaderFill!.style.background = '#f44';
|
|
79
|
+
this.loaderLabel!.textContent = message;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
showLoader(): void {
|
|
83
|
+
this.ensureLoader();
|
|
84
|
+
if (!this.loaderEl) return;
|
|
85
|
+
// Ensure it's visible (remove hidden class) and reset styles
|
|
86
|
+
this.loaderEl.classList.remove('hidden');
|
|
87
|
+
if (this.loaderFill) {
|
|
88
|
+
this.loaderFill.style.width = '0';
|
|
89
|
+
this.loaderFill.style.background = '';
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
hideLoader(delayMs: number = 600): void {
|
|
94
|
+
if (!this.loaderEl) return;
|
|
95
|
+
const el = this.loaderEl;
|
|
96
|
+
setTimeout(() => {
|
|
97
|
+
el.classList.add('hidden');
|
|
98
|
+
el.addEventListener('transitionend', () => el.remove(), { once: true });
|
|
99
|
+
}, delayMs);
|
|
100
|
+
this.loaderEl = null;
|
|
101
|
+
this.loaderLabel = null;
|
|
102
|
+
this.loaderFill = null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ── Rendering ─────────────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
render(frame: FrameBuffer): void {
|
|
108
|
+
if (this.canvas.width !== frame.width || this.canvas.height !== frame.height) {
|
|
109
|
+
this.canvas.width = frame.width;
|
|
110
|
+
this.canvas.height = frame.height;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const imageData = new ImageData(new Uint8ClampedArray(frame.data), frame.width, frame.height);
|
|
114
|
+
|
|
115
|
+
this.ctx.putImageData(imageData, 0, 0);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
attachTo(emulator: C64Emulator): void {
|
|
119
|
+
emulator.onFrame = (frame) => {
|
|
120
|
+
this.render(frame);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
if (this.animationFrameId !== null) {
|
|
124
|
+
cancelAnimationFrame(this.animationFrameId);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let lastTimestamp = 0;
|
|
128
|
+
|
|
129
|
+
const runFrame = (timestamp: number) => {
|
|
130
|
+
const dTime = lastTimestamp ? timestamp - lastTimestamp : 0;
|
|
131
|
+
lastTimestamp = timestamp;
|
|
132
|
+
|
|
133
|
+
emulator.tick(dTime);
|
|
134
|
+
this.animationFrameId = requestAnimationFrame(runFrame);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
this.animationFrameId = requestAnimationFrame(runFrame);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { EmulatorInput, JOYSTICK_KEY_CODES } from '../emulator/input';
|
|
2
|
+
import type { JoystickPort } from '../emulator/constants';
|
|
3
|
+
import type { C64Emulator } from '../emulator/c64-emulator';
|
|
4
|
+
|
|
5
|
+
export default class InputHandler {
|
|
6
|
+
private readonly emulatorInput: EmulatorInput;
|
|
7
|
+
// capture-phase blockers
|
|
8
|
+
private readonly captureKeyDown = (e: KeyboardEvent) => this.onCaptureKeyDown(e);
|
|
9
|
+
private readonly captureKeyUp = (e: KeyboardEvent) => this.onCaptureKeyUp(e);
|
|
10
|
+
|
|
11
|
+
constructor(emulator: C64Emulator, target: EventTarget = window) {
|
|
12
|
+
this.emulatorInput = new EmulatorInput(emulator, target);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
attach(): void {
|
|
16
|
+
// Install capture-phase listeners to block input when UI is open or tab unfocused
|
|
17
|
+
window.addEventListener('keydown', this.captureKeyDown, true);
|
|
18
|
+
window.addEventListener('keyup', this.captureKeyUp, true);
|
|
19
|
+
this.emulatorInput.attach();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
detach(): void {
|
|
23
|
+
window.removeEventListener('keydown', this.captureKeyDown, true);
|
|
24
|
+
window.removeEventListener('keyup', this.captureKeyUp, true);
|
|
25
|
+
this.emulatorInput.detach();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Change which joystick port keyboard input maps to (1 or 2) */
|
|
29
|
+
setKeyboardJoystickPort(port: JoystickPort): void {
|
|
30
|
+
this.emulatorInput.setKeyboardPort(port);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private onCaptureKeyDown(e: KeyboardEvent): void {
|
|
34
|
+
// Only block joystick-mapped keys to avoid interfering with other browser/UI shortcuts
|
|
35
|
+
// Use the canonical mapping from the emulator to decide which keys to block
|
|
36
|
+
const JOY_KEYS = new Set(JOYSTICK_KEY_CODES as string[]);
|
|
37
|
+
if (!JOY_KEYS.has(e.code)) return;
|
|
38
|
+
|
|
39
|
+
// Block if document is unfocused. Prefer the Visibility API (document.hidden)
|
|
40
|
+
// which is stable under JSDOM; fall back to hasFocus() only if hidden is unavailable.
|
|
41
|
+
// NOTE: We cast `document` to `Document` when checking `hasFocus` so TypeScript
|
|
42
|
+
// understands the property exists at runtime. In some test environments (JSDOM)
|
|
43
|
+
// the DOM typing may be looser or mocked; the cast keeps the code typed while
|
|
44
|
+
// still allowing tests to override `document.hidden` or provide a `hasFocus`
|
|
45
|
+
// implementation. We still prefer `document.hidden` when present.
|
|
46
|
+
try {
|
|
47
|
+
if (typeof document !== 'undefined') {
|
|
48
|
+
if ('hidden' in document) {
|
|
49
|
+
if ((document as Document & { hidden: boolean }).hidden) {
|
|
50
|
+
e.stopImmediatePropagation();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
} else if (
|
|
54
|
+
typeof (document as Document).hasFocus === 'function' &&
|
|
55
|
+
!(document as Document).hasFocus()
|
|
56
|
+
) {
|
|
57
|
+
e.stopImmediatePropagation();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
// ignore DOM errors
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Block if UI overlays are visible
|
|
66
|
+
try {
|
|
67
|
+
if (typeof document !== 'undefined') {
|
|
68
|
+
if (
|
|
69
|
+
document.querySelector('.c64-help-overlay.visible') ||
|
|
70
|
+
document.querySelector('.c64-menu-overlay.visible')
|
|
71
|
+
) {
|
|
72
|
+
e.stopImmediatePropagation();
|
|
73
|
+
e.preventDefault();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
// ignore DOM errors
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private onCaptureKeyUp(e: KeyboardEvent): void {
|
|
83
|
+
// Only block joystick-mapped keys
|
|
84
|
+
const JOY_KEYS = new Set(JOYSTICK_KEY_CODES as string[]);
|
|
85
|
+
if (!JOY_KEYS.has(e.code)) return;
|
|
86
|
+
|
|
87
|
+
// Mirror the same blocking logic for keyup events
|
|
88
|
+
try {
|
|
89
|
+
if (typeof document !== 'undefined') {
|
|
90
|
+
if ('hidden' in document) {
|
|
91
|
+
if ((document as Document & { hidden: boolean }).hidden) {
|
|
92
|
+
e.stopImmediatePropagation();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
} else if (
|
|
96
|
+
typeof (document as Document).hasFocus === 'function' &&
|
|
97
|
+
!(document as Document).hasFocus()
|
|
98
|
+
) {
|
|
99
|
+
e.stopImmediatePropagation();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (
|
|
104
|
+
document.querySelector('.c64-help-overlay.visible') ||
|
|
105
|
+
document.querySelector('.c64-menu-overlay.visible')
|
|
106
|
+
) {
|
|
107
|
+
e.stopImmediatePropagation();
|
|
108
|
+
e.preventDefault();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} catch {
|
|
113
|
+
// ignore DOM errors
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/* Loader styles extracted from canvas-renderer.ts */
|
|
2
|
+
.c64-loader {
|
|
3
|
+
position: relative;
|
|
4
|
+
width: 768px;
|
|
5
|
+
margin-top: 12px;
|
|
6
|
+
transition: opacity 0.6s ease, transform 0.6s ease;
|
|
7
|
+
}
|
|
8
|
+
.c64-loader.hidden {
|
|
9
|
+
opacity: 0;
|
|
10
|
+
transform: translateY(-6px);
|
|
11
|
+
pointer-events: none;
|
|
12
|
+
}
|
|
13
|
+
.c64-loader-label {
|
|
14
|
+
font-family: monospace;
|
|
15
|
+
font-size: 12px;
|
|
16
|
+
color: #7b71d5;
|
|
17
|
+
margin-bottom: 4px;
|
|
18
|
+
letter-spacing: 1px;
|
|
19
|
+
}
|
|
20
|
+
.c64-loader-track {
|
|
21
|
+
width: 100%;
|
|
22
|
+
height: 10px;
|
|
23
|
+
background: #222;
|
|
24
|
+
border: 1px solid #444;
|
|
25
|
+
border-radius: 2px;
|
|
26
|
+
overflow: hidden;
|
|
27
|
+
}
|
|
28
|
+
.c64-loader-fill {
|
|
29
|
+
height: 100%;
|
|
30
|
+
width: 0;
|
|
31
|
+
background: linear-gradient(90deg, #6c6cff 0%, #a8a8ff 50%, #6c6cff 100%);
|
|
32
|
+
background-size: 200% 100%;
|
|
33
|
+
border-radius: 2px;
|
|
34
|
+
transition: width 0.5s cubic-bezier(.22,.61,.36,1);
|
|
35
|
+
animation: c64-shimmer 1.5s linear infinite;
|
|
36
|
+
}
|
|
37
|
+
@keyframes c64-shimmer {
|
|
38
|
+
0% { background-position: 200% 0; }
|
|
39
|
+
100% { background-position: -200% 0; }
|
|
40
|
+
}
|
|
41
|
+
|