pixel8-console 0.1.0

Pixel8: a PICO-8-like fantasy console for Rust games
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Pixel8: a PICO-8-like fantasy console for Rust games.
//!
//! `pixel8` opens the console; `pixel8 <dir|cart.png>` opens it with a cart
//! loaded. A few headless subcommands (`new`, `build`, `export`,
//! `extract`, `import-pico8`) support the external-editor workflow and CI.

use anyhow::{anyhow, bail, Context, Result};
use pixel8_console::{
    builder, sdk_path,
    shell::{self, Shell},
    webexport,
};
#[cfg(feature = "window")]
use pixel8_console::{
    frame_duration, gpu,
    shell::{Key, Mods},
};
use pixel8_runtime::{
    cart::{self, Cart},
    project::Project,
};
#[cfg(feature = "window")]
use std::sync::Arc;
use std::{
    path::{Path, PathBuf},
    time::Instant,
};
#[cfg(feature = "window")]
use winit::{
    application::ApplicationHandler,
    dpi::LogicalSize,
    event::{ElementState, MouseButton, WindowEvent},
    event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
    keyboard::{KeyCode, NamedKey, PhysicalKey},
    window::{Window, WindowId},
};

fn main() -> Result<()> {
    let args: Vec<String> = std::env::args().skip(1).collect();
    let strs: Vec<&str> = args.iter().map(String::as_str).collect();
    match strs.as_slice() {
        ["help" | "--help" | "-h"] => {
            print_help();
            Ok(())
        }
        ["new", dir] => headless_new(Path::new(dir)),
        ["build", dir] => headless_build(Path::new(dir)),
        ["export", dir, out, rest @ ..] => headless_export(
            Path::new(dir),
            Path::new(out),
            !rest.contains(&"--no-source"),
        ),
        ["extract", png, dir] => headless_extract(Path::new(png), Path::new(dir)),
        ["import-pico8", rest @ ..] => headless_import_pico8_cli(rest),
        ["export-web", input, out] => headless_export_web(Path::new(input), Path::new(out)),
        ["verify", png] => headless_verify(Path::new(png)),
        ["snap", project, outdir] => headless_snap(Path::new(project), Path::new(outdir)),
        ["run", path] => run_windowed(Some(path.to_string()), true),
        ["run"] => {
            print_help();
            bail!("Usage: pixel8 run <dir|cart.png>");
        }
        // The terminal frontend lives in its own crate, so this binary
        // never builds it — point people there.
        ["tui", ..] => {
            bail!("The terminal frontend is the separate `pixel8-tui` binary (cargo install pixel8-tui)")
        }
        [] => run_windowed(None, false),
        [path] => run_windowed(Some(path.to_string()), false),
        _ => {
            print_help();
            bail!("Unrecognized arguments: {args:?}");
        }
    }
}

fn print_help() {
    println!(
        "Pixel8 {} - A fantasy console for Rust\n\n\
         Usage:\n\
         \x20 pixel8                      Boot the console\n\
         \x20 pixel8 <dir|cart.png>       Boot with a cart loaded\n\
         \x20 pixel8 run <dir|cart.png>   Boot, load, and run immediately\n\
         \x20 pixel8 new <dir>            Create a project (headless)\n\
         \x20 pixel8 build <dir>          Compile a project to wasm (headless)\n\
         \x20 pixel8 export <dir> <out.png> [--no-source]\n\
         \x20                            Build + export a PNG cart (headless)\n\
         \x20 pixel8 extract <cart.png> <dir>\n\
         \x20                            Turn an editable cart into a project\n\
         \x20 pixel8 import-pico8 <cart.p8|.p8.png> [dir]\n\
         \x20                            Import a PICO-8 cart's assets into a new project\n\
         \x20                            (dir defaults to the cart's name)\n\
         \x20 pixel8 import-pico8 <cart.p8|.p8.png> --into <project-dir>\n\
         \x20                            [--sprites R] [--sfx R] [--music R]\n\
         \x20                            Append selected assets into an existing project\n\
         \x20 pixel8 export-web <dir|cart.png> <out.html>\n\
         \x20                            Export a self-contained playable web page\n\
         \x20 pixel8 verify <cart.png>    Load a cart and run 60 frames headless",
        shell::VERSION
    );
}

// ---------------------------------------------------------------------------
// Headless subcommands
// ---------------------------------------------------------------------------

fn headless_new(dir: &Path) -> Result<()> {
    let name = dir
        .file_name()
        .ok_or_else(|| anyhow!("Bad directory name"))?
        .to_string_lossy()
        .into_owned();
    Project::create(dir, &name)?;
    println!("Created {}", dir.display());
    Ok(())
}

fn headless_build(dir: &Path) -> Result<()> {
    let project = Project::load(dir)?;
    let result = builder::run_build(dir, Instant::now());
    if !result.success {
        for line in &result.errors {
            eprintln!("{line}");
        }
        bail!("Build failed");
    }
    println!(
        "Built {} ({:.1}s)",
        project.wasm_path().display(),
        result.duration.as_secs_f32()
    );
    for line in &result.warnings {
        eprintln!("{line}");
    }
    Ok(())
}

fn headless_export(dir: &Path, out: &Path, include_source: bool) -> Result<()> {
    let project = Project::load(dir)?;
    let result = builder::run_build(dir, Instant::now());
    if !result.success {
        for line in &result.errors {
            eprintln!("{line}");
        }
        bail!("Build failed");
    }
    let wasm = std::fs::read(project.wasm_path()).context("Reading built wasm")?;
    let cart = Cart {
        wasm,
        assets: project.assets.clone(),
        source: include_source.then(|| project.code.clone()),
    };
    cart::save_png(&cart, out)?;
    println!("Exported {}", out.display());
    Ok(())
}

fn headless_extract(png: &Path, dir: &Path) -> Result<()> {
    let cart = cart::load_png(png)?;
    let source = cart
        .source
        .ok_or_else(|| anyhow!("Cart has no embedded source (playable-only cart)"))?;
    let mut project = Project::create(dir, &cart.assets.meta.name)?;
    project.code = source;
    project.assets = cart.assets;
    project.save()?;
    println!("Extracted into {}", dir.display());
    Ok(())
}

/// Import a PICO-8 cart (`.p8` text or `.p8.png`) into a new project. Only
/// the assets — graphics, map, sound and music — transfer; the cart's Lua
/// code is ignored.
fn headless_import_pico8(src: &Path, dir: &Path) -> Result<()> {
    pixel8_runtime::pico8::import_project(src, dir)?;
    println!("Imported {} into {}", src.display(), dir.display());
    Ok(())
}

/// The value following a flag, rejecting a missing value or another flag taken
/// as the value (e.g. `--into --sfx 0`).
fn flag_value<'a>(next: Option<&'a &'a str>, flag: &str) -> Result<&'a str> {
    match next {
        Some(&v) if !v.starts_with("--") => Ok(v),
        _ => bail!("{flag} needs a value"),
    }
}

/// Parse `import-pico8` arguments and dispatch to create-new vs additive
/// (`--into`) mode. Create: `<src> [dir]`. Additive: `<src> --into <dir>
/// [--sprites R] [--sfx R] [--music R]`.
fn headless_import_pico8_cli(args: &[&str]) -> Result<()> {
    let (mut src, mut dir, mut into) = (None, None, None);
    let (mut sprites, mut sfx, mut music) = (None, None, None);
    let mut it = args.iter();
    // Iterating `&[&str]` yields `&&str`; `flag_value(it.next(), ...)` gives a `&str`.
    while let Some(&a) = it.next() {
        match a {
            "--into" => into = Some(flag_value(it.next(), "--into")?),
            "--sprites" => sprites = Some(flag_value(it.next(), "--sprites")?),
            "--sfx" => sfx = Some(flag_value(it.next(), "--sfx")?),
            "--music" => music = Some(flag_value(it.next(), "--music")?),
            flag if flag.starts_with("--") => bail!("unknown flag {flag}"),
            pos if src.is_none() => src = Some(pos),
            pos if dir.is_none() => dir = Some(pos),
            pos => bail!("unexpected argument {pos}"),
        }
    }
    let Some(src) = src else {
        bail!(
            "Usage: pixel8 import-pico8 <cart.p8|.p8.png> [dir]\n   or: \
             pixel8 import-pico8 <cart.p8|.p8.png> --into <project-dir> \
             [--sprites R] [--sfx R] [--music R]"
        );
    };
    let src = Path::new(src);
    match into {
        Some(into) => {
            if dir.is_some() {
                bail!("--into supplies the destination; do not also pass a positional dir");
            }
            let sel = pixel8_runtime::pico8::Selection::parse(sprites, sfx, music)?;
            headless_import_pico8_into(src, Path::new(into), &sel)
        }
        None => {
            if sprites.is_some() || sfx.is_some() || music.is_some() {
                bail!("--sprites/--sfx/--music only apply with --into <project-dir>");
            }
            let dir = dir
                .map(PathBuf::from)
                .unwrap_or_else(|| PathBuf::from(pixel8_runtime::pico8::default_dir_name(src)));
            headless_import_pico8(src, &dir)
        }
    }
}

/// Append selected PICO-8 assets into the existing project at `dir`.
fn headless_import_pico8_into(
    src: &Path,
    dir: &Path,
    sel: &pixel8_runtime::pico8::Selection,
) -> Result<()> {
    let mut project = Project::load(dir)?;
    let assets = pixel8_runtime::pico8::parse_file(src)?;
    let report = pixel8_runtime::pico8::append_pico8_assets(&mut project.assets, &assets, sel)?;
    project.save()?;
    for line in report.summary_lines() {
        println!("Imported {line} into {}", dir.display());
    }
    for w in &report.warnings {
        eprintln!("warning: {w}");
    }
    Ok(())
}

/// Export a project or cart as a single playable HTML file.
fn headless_export_web(input: &Path, out: &Path) -> Result<()> {
    let cart = if input.extension().is_some_and(|e| e == "png") {
        cart::load_png(input)?
    } else {
        let project = Project::load(input)?;
        let result = builder::run_build(input, Instant::now());
        if !result.success {
            for line in &result.errors {
                eprintln!("{line}");
            }
            bail!("Build failed");
        }
        let wasm = std::fs::read(project.wasm_path()).context("Reading built wasm")?;
        Cart {
            wasm,
            assets: project.assets.clone(),
            // Web players can't edit; keep the page lean.
            source: None,
        }
    };
    webexport::export_html(&cart, out, &webexport::web_crate_dir(&sdk_path()))?;
    println!("Exported {}", out.display());
    Ok(())
}

/// Load a cart and run a second of frames without a window — a smoke
/// test for carts and for the console itself (used by CI).
fn headless_verify(png: &Path) -> Result<()> {
    use pixel8_runtime::{audio::AudioHandle, storage::Storage, vm::GameVm};
    let cart = cart::load_png(png)?;
    // In-memory storage: verify runs must be hermetic (CI, scripted checks).
    let mut vm = GameVm::load(
        &cart.wasm,
        &cart.assets,
        AudioHandle::dummy(),
        Storage::default(),
    )
    .context("Loading cart into the VM")?;
    for frame in 0..60 {
        vm.call_update()
            .and_then(|()| vm.call_draw())
            .map_err(|e| anyhow!("Frame {frame}: {e}"))?;
    }
    let drew_something = vm.state().fb.pixels().iter().any(|&p| p != 0);
    println!(
        "OK: {} ran 60 frames{}",
        cart.assets.meta.name,
        if drew_something {
            ""
        } else {
            " (blank screen)"
        }
    );
    Ok(())
}

/// Render the console and each editor headless and save screenshots.
/// Undocumented helper for docs and visual checks.
fn headless_snap(project: &Path, outdir: &Path) -> Result<()> {
    use pixel8_runtime::{audio::AudioHandle, cart::encode_screen_png};
    std::fs::create_dir_all(outdir)?;
    let mut shell = Shell::new(AudioHandle::dummy(), sdk_path());
    shell.startup_load(&project.to_string_lossy());
    let shots = [
        (shell::Mode::Console, "console"),
        (shell::Mode::Code, "code"),
        (shell::Mode::Sprite, "sprite"),
        (shell::Mode::Map, "map"),
        (shell::Mode::Sfx, "sfx"),
        (shell::Mode::Music, "music"),
    ];
    for (mode, name) in shots {
        if mode == shell::Mode::Console {
            shell.mode = mode;
        } else {
            shell.switch_editor(mode);
        }
        for _ in 0..3 {
            shell.tick();
        }
        let png = encode_screen_png(shell.draw(), 3);
        std::fs::write(outdir.join(format!("{name}.png")), png)?;
    }
    println!("Wrote screenshots to {}", outdir.display());
    Ok(())
}

// ---------------------------------------------------------------------------
// Windowed console
// ---------------------------------------------------------------------------

#[cfg(feature = "window")]
fn run_windowed(load: Option<String>, auto_run: bool) -> Result<()> {
    #[cfg(feature = "audio")]
    let audio_out = pixel8_runtime::audio::AudioOutput::start();
    #[cfg(feature = "audio")]
    let audio = audio_out
        .as_ref()
        .map(|a| a.handle())
        .unwrap_or_else(pixel8_runtime::audio::AudioHandle::dummy);
    #[cfg(not(feature = "audio"))]
    let audio = pixel8_runtime::audio::AudioHandle::dummy();

    let mut shell = Shell::new(audio, sdk_path());
    if let Some(path) = load {
        shell.startup_load(&path);
        if auto_run {
            shell.startup_run();
        }
    }

    let event_loop = EventLoop::new()?;
    event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now()));
    let mut app = App {
        window: None,
        gpu: None,
        shell,
        mods: Mods::default(),
        last_title: String::new(),
        next_tick: Instant::now(),
        #[cfg(feature = "audio")]
        _audio_out: audio_out,
    };
    event_loop.run_app(&mut app)?;
    Ok(())
}

/// Opening the console needs the windowed frontend; everything headless
/// still works in a build without it (how CI orchestrates cart builds).
#[cfg(not(feature = "window"))]
fn run_windowed(_load: Option<String>, _auto_run: bool) -> Result<()> {
    bail!(
        "This pixel8 build has no windowed frontend (`window` feature off). The headless \
         subcommands still work, and `pixel8-tui` runs the console in a terminal."
    );
}

#[cfg(feature = "window")]
struct App {
    window: Option<Arc<Window>>,
    gpu: Option<gpu::Gpu>,
    shell: Shell,
    mods: Mods,
    last_title: String,
    next_tick: Instant,
    #[cfg(feature = "audio")]
    _audio_out: Option<pixel8_runtime::audio::AudioOutput>,
}

#[cfg(feature = "window")]
impl App {
    /// Map physical keys to the six game buttons (active in run mode).
    fn game_button(code: KeyCode) -> Option<usize> {
        Some(match code {
            KeyCode::ArrowLeft => 0,
            KeyCode::ArrowRight => 1,
            KeyCode::ArrowUp => 2,
            KeyCode::ArrowDown => 3,
            KeyCode::KeyZ | KeyCode::KeyC | KeyCode::KeyN => 4,
            KeyCode::KeyX | KeyCode::KeyV | KeyCode::KeyM => 5,
            _ => return None,
        })
    }

    fn shell_key(logical: &winit::keyboard::Key) -> Option<Key> {
        use winit::keyboard::Key as WKey;
        Some(match logical {
            WKey::Named(NamedKey::ArrowLeft) => Key::Left,
            WKey::Named(NamedKey::ArrowRight) => Key::Right,
            WKey::Named(NamedKey::ArrowUp) => Key::Up,
            WKey::Named(NamedKey::ArrowDown) => Key::Down,
            WKey::Named(NamedKey::Backspace) => Key::Backspace,
            WKey::Named(NamedKey::Delete) => Key::Delete,
            WKey::Named(NamedKey::Enter) => Key::Enter,
            WKey::Named(NamedKey::Tab) => Key::Tab,
            WKey::Named(NamedKey::Escape) => Key::Escape,
            WKey::Named(NamedKey::Home) => Key::Home,
            WKey::Named(NamedKey::End) => Key::End,
            WKey::Named(NamedKey::PageUp) => Key::PageUp,
            WKey::Named(NamedKey::PageDown) => Key::PageDown,
            WKey::Named(NamedKey::Space) => Key::Char(' '),
            WKey::Named(NamedKey::F1) => Key::ToggleStats,
            WKey::Named(NamedKey::F6) => Key::CaptureLabel,
            WKey::Character(s) => Key::Char(s.chars().next()?),
            _ => return None,
        })
    }
}

#[cfg(feature = "window")]
impl ApplicationHandler for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        if self.window.is_some() {
            return;
        }
        let attrs = Window::default_attributes()
            .with_title("Pixel8")
            .with_inner_size(LogicalSize::new(512.0, 512.0))
            .with_min_inner_size(LogicalSize::new(128.0, 128.0));
        let window = match event_loop.create_window(attrs) {
            Ok(w) => Arc::new(w),
            Err(e) => {
                eprintln!("pixel8: Could not open a window: {e}");
                event_loop.exit();
                return;
            }
        };
        // The console draws its own pixel-art cursor into the framebuffer, so
        // hide the OS cursor to avoid showing two cursors at once.
        window.set_cursor_visible(false);
        match gpu::Gpu::new(window.clone(), event_loop.owned_display_handle()) {
            Ok(g) => {
                self.gpu = Some(g);
                self.window = Some(window);
            }
            Err(e) => {
                eprintln!("pixel8: Graphics init failed: {e:#}");
                event_loop.exit();
            }
        }
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
        match event {
            WindowEvent::CloseRequested => event_loop.exit(),
            WindowEvent::Resized(size) => {
                if let Some(g) = &mut self.gpu {
                    g.resize(size.width, size.height);
                }
            }
            WindowEvent::ModifiersChanged(m) => {
                let s = m.state();
                self.mods = Mods {
                    ctrl: s.control_key(),
                    shift: s.shift_key(),
                    alt: s.alt_key(),
                };
            }
            WindowEvent::KeyboardInput { event, .. } => {
                if let PhysicalKey::Code(code) = event.physical_key {
                    if let Some(b) = Self::game_button(code) {
                        self.shell
                            .set_button(b, event.state == ElementState::Pressed);
                    }
                }
                if event.state == ElementState::Pressed {
                    if let Some(key) = Self::shell_key(&event.logical_key) {
                        self.shell.key(key, self.mods);
                    }
                }
            }
            WindowEvent::CursorMoved { position, .. } => {
                if let Some(g) = &self.gpu {
                    let (x, y) = g.viewport().window_to_screen(position.x, position.y);
                    self.shell.mouse.x = x;
                    self.shell.mouse.y = y;
                }
            }
            WindowEvent::MouseInput { state, button, .. } => {
                let down = state == ElementState::Pressed;
                match button {
                    MouseButton::Left => {
                        if down {
                            self.shell.mouse.left_pressed = true;
                        }
                        self.shell.mouse.left = down;
                    }
                    MouseButton::Right => {
                        if down {
                            self.shell.mouse.right_pressed = true;
                        }
                        self.shell.mouse.right = down;
                    }
                    _ => {}
                }
            }
            WindowEvent::RedrawRequested => {
                let shell = &mut self.shell;
                let fb = shell.draw();
                if let Some(g) = &mut self.gpu {
                    if let Err(e) = g.render(fb) {
                        eprintln!("pixel8: Render error: {e:#}");
                    }
                }
            }
            _ => {}
        }
    }

    fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
        let now = Instant::now();
        let mut ticked = false;
        let frame = frame_duration(self.shell.tick_fps());
        while Instant::now() >= self.next_tick {
            self.shell.tick();
            self.next_tick += frame;
            ticked = true;
            // Don't death-spiral after a long stall.
            if now > self.next_tick + frame * 10 {
                self.next_tick = now + frame;
            }
        }
        if self.shell.want_exit {
            event_loop.exit();
            return;
        }
        if ticked {
            let title = self.shell.window_title();
            if title != self.last_title {
                if let Some(w) = &self.window {
                    w.set_title(&title);
                }
                self.last_title = title;
            }
            if let Some(w) = &self.window {
                w.request_redraw();
            }
        }
        event_loop.set_control_flow(ControlFlow::WaitUntil(self.next_tick));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn into_rejects_positional_dir() {
        // `--into` supplies the destination, so a positional dir is ambiguous.
        let err = headless_import_pico8_cli(&["c.p8", "mydir", "--into", "dest", "--sfx", "0"])
            .unwrap_err();
        assert!(err.to_string().contains("positional"), "got: {err}");
    }

    #[test]
    fn selection_flags_require_into() {
        let err = headless_import_pico8_cli(&["c.p8", "--sprites", "0-3"]).unwrap_err();
        assert!(err.to_string().contains("--into"), "got: {err}");
    }

    #[test]
    fn into_requires_a_selection() {
        let err = headless_import_pico8_cli(&["c.p8", "--into", "dest"]).unwrap_err();
        assert!(err.to_string().contains("at least one"), "got: {err}");
    }

    #[test]
    fn unknown_flag_is_rejected() {
        let err = headless_import_pico8_cli(&["c.p8", "--into", "dest", "--bogus"]).unwrap_err();
        assert!(err.to_string().contains("bogus"), "got: {err}");
    }

    #[test]
    fn flag_without_value_is_rejected() {
        let err = headless_import_pico8_cli(&["c.p8", "--into", "--sfx", "0"]).unwrap_err();
        assert!(
            err.to_string().contains("--into needs a value"),
            "got: {err}"
        );
    }
}