c64-ready 0.6.2 → 0.10.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/src/main.ts CHANGED
@@ -53,15 +53,15 @@ updateFavicon();
53
53
  // Listen for files selected via the UI and load into the emulator
54
54
  window.addEventListener('c64-load-file', async (e: Event) => {
55
55
  const detail = (e as CustomEvent).detail;
56
- const file: File | undefined = detail?.file;
56
+ const file: File | undefined = detail.file;
57
57
  if (!file) return;
58
58
  try {
59
- renderer.showLoader?.();
59
+ renderer.showLoader();
60
60
  await player.loadFile(file);
61
- renderer.hideLoader?.();
61
+ renderer.hideLoader();
62
62
  } catch (err) {
63
63
  console.error(err);
64
- renderer.setError?.('LOAD ERROR');
64
+ renderer.setError('LOAD ERROR');
65
65
  status.textContent = `Load error: ${err}`;
66
66
  status.style.color = '#f44';
67
67
  }
@@ -74,7 +74,7 @@ window.addEventListener('c64-load-error', (e: Event) => {
74
74
  | undefined;
75
75
  const msg = detail?.error ?? 'Unknown load error';
76
76
  console.error('C64 load error event:', detail);
77
- renderer.setError?.('LOAD ERROR');
77
+ renderer.setError('LOAD ERROR');
78
78
  status.textContent = `Load error: ${msg}`;
79
79
  status.style.color = '#f44';
80
80
  });
@@ -1,6 +1,8 @@
1
1
  import { C64Emulator } from '../emulator/c64-emulator';
2
+ import { parseCrtInfo } from '../emulator/crt-info';
2
3
  import type { GameLoadOptions } from '../types';
3
4
  import type { JoystickPort } from '../emulator/constants';
5
+ import type { InputMode } from '../emulator/input';
4
6
  import type CanvasRenderer from './canvas-renderer';
5
7
  import { AudioEngine } from './audio-engine';
6
8
  import InputHandler from './input-handler';
@@ -125,6 +127,11 @@ export class C64Player {
125
127
  console.error(err);
126
128
  throw err;
127
129
  }
130
+ if (type === 'crt') {
131
+ const filename = url.split('/').pop() ?? url;
132
+ const info = parseCrtInfo(data, filename);
133
+ if (info) console.log(info.line);
134
+ }
128
135
  try {
129
136
  this.emulator.loadGame({ type, data });
130
137
  onProgress?.(100, 'READY!');
@@ -156,6 +163,10 @@ export class C64Player {
156
163
  console.error(err);
157
164
  throw err;
158
165
  }
166
+ if (type === 'crt') {
167
+ const info = parseCrtInfo(data, file.name);
168
+ if (info) console.log(info.line);
169
+ }
159
170
  try {
160
171
  this.emulator.loadGame({ type, data });
161
172
  onProgress?.(100, 'READY!');
@@ -210,13 +221,20 @@ export class C64Player {
210
221
  setKeyboardJoystickPort(port: JoystickPort): void {
211
222
  this.inputHandler?.setKeyboardJoystickPort(port);
212
223
  }
224
+
225
+ /**
226
+ * Change the input mode ('joystick' | 'keyboard' | 'mixed').
227
+ * - 'joystick' — arrows + ControlLeft control the joystick only (default)
228
+ * - 'keyboard' — all keys go to the C64 keyboard matrix
229
+ * - 'mixed' — arrows + Z + ControlLeft drive the joystick; all other
230
+ * keys route to the C64 keyboard matrix simultaneously
231
+ */
232
+ setInputMode(mode: InputMode): void {
233
+ this.inputHandler?.setInputMode(mode);
234
+ }
213
235
  }
214
236
 
215
- // Simple CRT format validator
237
+ // CRT format validator — delegates to parseCrtInfo so magic-check logic lives in one place.
216
238
  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');
239
+ return parseCrtInfo(data) !== null;
222
240
  }
@@ -1,4 +1,5 @@
1
- import { EmulatorInput, JOYSTICK_KEY_CODES } from '../emulator/input';
1
+ import { EmulatorInput, JOYSTICK_KEY_CODES, MIXED_JOYSTICK_KEYS } from '../emulator/input';
2
+ import type { InputMode } from '../emulator/input';
2
3
  import type { JoystickPort } from '../emulator/constants';
3
4
  import type { C64Emulator } from '../emulator/c64-emulator';
4
5
 
@@ -30,10 +31,22 @@ export default class InputHandler {
30
31
  this.emulatorInput.setKeyboardPort(port);
31
32
  }
32
33
 
34
+ /** Change the input mode ('joystick' | 'keyboard' | 'mixed') */
35
+ setInputMode(mode: InputMode): void {
36
+ this.emulatorInput.setInputMode(mode);
37
+ }
38
+
39
+ getInputMode(): InputMode {
40
+ return this.emulatorInput.getInputMode();
41
+ }
42
+
33
43
  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[]);
44
+ // Block joystick-mapped keys (standard + mixed mode) when overlays are
45
+ // visible or the document is unfocused. Use the union of both key sets.
46
+ const JOY_KEYS = new Set([
47
+ ...(JOYSTICK_KEY_CODES as string[]),
48
+ ...Object.keys(MIXED_JOYSTICK_KEYS),
49
+ ]);
37
50
  if (!JOY_KEYS.has(e.code)) return;
38
51
 
39
52
  // Block if document is unfocused. Prefer the Visibility API (document.hidden)
@@ -80,8 +93,11 @@ export default class InputHandler {
80
93
  }
81
94
 
82
95
  private onCaptureKeyUp(e: KeyboardEvent): void {
83
- // Only block joystick-mapped keys
84
- const JOY_KEYS = new Set(JOYSTICK_KEY_CODES as string[]);
96
+ // Only block joystick-mapped keys (standard + mixed mode)
97
+ const JOY_KEYS = new Set([
98
+ ...(JOYSTICK_KEY_CODES as string[]),
99
+ ...Object.keys(MIXED_JOYSTICK_KEYS),
100
+ ]);
85
101
  if (!JOY_KEYS.has(e.code)) return;
86
102
 
87
103
  // Mirror the same blocking logic for keyup events
@@ -163,6 +163,17 @@ export default class UIController {
163
163
  <label><input type="radio" name="c64-joy-port" value="2" checked /> Port 2</label>
164
164
  </div>
165
165
  <div style="height:8px"></div>
166
+ <label style="font-size:13px;color:#aaa">Input Mode</label>
167
+ <div style="margin-top:8px; display:flex; gap:12px; align-items:center; flex-wrap:wrap;">
168
+ <label><input type="radio" name="c64-input-mode" value="joystick" checked /> Joystick</label>
169
+ <label><input type="radio" name="c64-input-mode" value="keyboard" /> Keyboard</label>
170
+ <label><input type="radio" name="c64-input-mode" value="mixed" /> Mixed</label>
171
+ </div>
172
+ <div style="height:4px"></div>
173
+ <div id="c64-input-mode-hint" style="font-size:11px;color:#888;line-height:1.4">
174
+ Arrows + Ctrl = joystick
175
+ </div>
176
+ <div style="height:8px"></div>
166
177
  <label style="font-size:13px;color:#aaa">Display</label>
167
178
  <div style="margin-top:8px; display:flex; gap:12px; align-items:center;">
168
179
  <label><input type="radio" name="c64-display-mode" value="standard" checked /> Standard</label>
@@ -297,6 +308,33 @@ export default class UIController {
297
308
  });
298
309
  });
299
310
 
311
+ // ── Input mode wiring ──────────────────────────────────────────────────
312
+ const inputModeHints: Record<string, string> = {
313
+ joystick: 'Arrows + Ctrl = joystick only',
314
+ keyboard: 'All keys &rarr; C64 keyboard matrix',
315
+ mixed: 'Arrows + Z + Ctrl = joystick &amp; all other keys &rarr; C64 keyboard',
316
+ };
317
+ const inputModeRadios = panel.querySelectorAll(
318
+ 'input[name="c64-input-mode"]',
319
+ ) as NodeListOf<HTMLInputElement>;
320
+ const hintEl = panel.querySelector('#c64-input-mode-hint') as HTMLElement | null;
321
+ const applyInputMode = (mode: string) => {
322
+ if (hintEl) hintEl.innerHTML = inputModeHints[mode] ?? '';
323
+ window.dispatchEvent(new CustomEvent('c64-set-input-mode', { detail: { mode } }));
324
+ if (this.player && typeof this.player.setInputMode === 'function') {
325
+ try {
326
+ this.player.setInputMode(mode as import('../emulator/input').InputMode);
327
+ } catch {
328
+ // ignore
329
+ }
330
+ }
331
+ };
332
+ inputModeRadios.forEach((r) => {
333
+ r.addEventListener('change', () => {
334
+ if (r.checked) applyInputMode(r.value);
335
+ });
336
+ });
337
+
300
338
  // ── Display section wiring ─────────────────────────────────────────────
301
339
  const displayRadios = panel.querySelectorAll(
302
340
  'input[name="c64-display-mode"]',