Skip to main content

clairlune_render/
lib.rs

1//! Rendering for Clairlune: glyph atlas, indexed palette, static bake, frame
2//! compositing and PNG export.
3//!
4//! Every pixel is a palette index. Colours are looked up, never blended at
5//! render time, so a scene has at most 255 colours (index 255 is reserved for GIF
6//! transparency) and the still PNG, GIF frames and the live renderer can match
7//! exactly (PLAN.md section 4.5).
8
9/// Compiles the README example as a doctest so it can't go stale.
10#[cfg(doctest)]
11#[doc = include_str!("../README.md")]
12struct ReadmeDoctests;
13
14pub mod art;
15pub mod atlas;
16pub mod bake;
17pub mod clock;
18pub mod encode;
19pub mod frame;
20pub mod gif_export;
21pub mod package;
22pub mod palette;
23mod tether;
24
25pub use art::{BuiltinArt, OwnedPixmap, Pixmap, SceneArt};
26pub use atlas::GlyphAtlas;
27pub use bake::bake_static;
28pub use clock::Clock;
29pub use encode::{encode_png, save_png, to_rgba};
30pub use frame::{
31    IncrementalFrame, PixelRect, hero_time, hero_time_with_art, render_frame, render_frame_clocked,
32    render_frame_with_art,
33};
34pub use gif_export::{
35    GifError, GifStats, Smoothness, encode_gif, encode_package_gif, frame_delays, valid_loop_ms,
36};
37pub use package::{PACKAGE_FORMAT, PackageError, ScenePackage};
38pub use palette::IndexedPalette;
39
40use clairlune_scene::activity::builtin_catalog;
41use clairlune_scene::{BuildError, BuildOptions, SceneGraph, SceneRecipe, build};
42use clairlune_sprites::{Character, builtin_character};
43use std::time::Instant;
44
45/// An image of palette indices, row-major.
46#[derive(Clone, Debug, PartialEq, Eq)]
47pub struct IndexedImage {
48    pub width: u32,
49    pub height: u32,
50    pub pixels: Vec<u8>,
51}
52
53impl IndexedImage {
54    pub fn new(width: u32, height: u32) -> Self {
55        Self {
56            width,
57            height,
58            pixels: vec![0; width as usize * height as usize],
59        }
60    }
61}
62
63/// A rendered still frame with everything needed to encode or inspect it.
64#[derive(Clone, Debug)]
65pub struct Still {
66    pub image: IndexedImage,
67    pub palette: IndexedPalette,
68    pub graph: SceneGraph,
69    /// The animation time that was rendered.
70    pub t_ms: u64,
71}
72
73/// Renders a recipe to a still frame at `t_ms`, or at the hero time when `None`.
74pub fn render_still(
75    recipe: &SceneRecipe,
76    opts: &BuildOptions,
77    t_ms: Option<u64>,
78) -> Result<Still, BuildError> {
79    let started = Instant::now();
80    let graph = build(recipe, opts)?;
81    if opts.buddy
82        && graph.character.is_some()
83        && let Some(a) = graph
84            .activity
85            .as_deref()
86            .and_then(|id| builtin_catalog().get(id))
87        && builtin_character()
88            .animation(&a.id, &a.animation.start)
89            .is_some_and(|an| an.name == "placeholder")
90    {
91        tracing::warn!(target: "clairlune_render", activity = %a.id, "no drawn art for this activity; drawing the placeholder character");
92    }
93    let atlas = GlyphAtlas::for_grid(&graph.grid);
94    let palette = IndexedPalette::for_graph(&graph);
95    let background = bake_static(&graph, &atlas);
96    let t = t_ms.unwrap_or_else(|| hero_time(&graph));
97    let image = render_frame(&background, &graph, &atlas, &palette, t);
98    tracing::info!(
99        target: "clairlune_render",
100        seed = recipe.seed,
101        recipe_hash = %recipe.hash_hex(),
102        width = opts.width,
103        height = opts.height,
104        theme = opts.theme.as_str(),
105        buddy = opts.buddy,
106        t_ms = t,
107        palette = palette.len(),
108        elapsed_ms = started.elapsed().as_secs_f64() * 1e3,
109        "still rendered"
110    );
111    Ok(Still {
112        image,
113        palette,
114        graph,
115        t_ms: t,
116    })
117}