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;
23
24pub use art::{BuiltinArt, OwnedPixmap, Pixmap, SceneArt};
25pub use atlas::GlyphAtlas;
26pub use bake::bake_static;
27pub use clock::Clock;
28pub use encode::{encode_png, save_png, to_rgba};
29pub use frame::{
30    IncrementalFrame, PixelRect, hero_time, hero_time_with_art, render_frame, render_frame_clocked,
31    render_frame_with_art,
32};
33pub use gif_export::{
34    GifError, GifStats, Smoothness, encode_gif, encode_package_gif, frame_delays, valid_loop_ms,
35};
36pub use package::{PACKAGE_FORMAT, PackageError, ScenePackage};
37pub use palette::IndexedPalette;
38
39use clairlune_scene::activity::builtin_catalog;
40use clairlune_scene::{BuildError, BuildOptions, SceneGraph, SceneRecipe, build};
41use clairlune_sprites::{Character, builtin_character};
42use std::time::Instant;
43
44/// An image of palette indices, row-major.
45#[derive(Clone, Debug, PartialEq, Eq)]
46pub struct IndexedImage {
47    pub width: u32,
48    pub height: u32,
49    pub pixels: Vec<u8>,
50}
51
52impl IndexedImage {
53    pub fn new(width: u32, height: u32) -> Self {
54        Self {
55            width,
56            height,
57            pixels: vec![0; width as usize * height as usize],
58        }
59    }
60}
61
62/// A rendered still frame with everything needed to encode or inspect it.
63#[derive(Clone, Debug)]
64pub struct Still {
65    pub image: IndexedImage,
66    pub palette: IndexedPalette,
67    pub graph: SceneGraph,
68    /// The animation time that was rendered.
69    pub t_ms: u64,
70}
71
72/// Renders a recipe to a still frame at `t_ms`, or at the hero time when `None`.
73pub fn render_still(
74    recipe: &SceneRecipe,
75    opts: &BuildOptions,
76    t_ms: Option<u64>,
77) -> Result<Still, BuildError> {
78    let started = Instant::now();
79    let graph = build(recipe, opts)?;
80    if opts.buddy
81        && graph.character.is_some()
82        && let Some(a) = graph
83            .activity
84            .as_deref()
85            .and_then(|id| builtin_catalog().get(id))
86        && builtin_character()
87            .animation(&a.id, &a.animation.start)
88            .is_some_and(|an| an.name == "placeholder")
89    {
90        tracing::warn!(target: "clairlune_render", activity = %a.id, "no drawn art for this activity; drawing the placeholder character");
91    }
92    let atlas = GlyphAtlas::for_grid(&graph.grid);
93    let palette = IndexedPalette::new(&graph.family);
94    let background = bake_static(&graph, &atlas);
95    let t = t_ms.unwrap_or_else(|| hero_time(&graph));
96    let image = render_frame(&background, &graph, &atlas, &palette, t);
97    tracing::info!(
98        target: "clairlune_render",
99        seed = recipe.seed,
100        recipe_hash = %recipe.hash_hex(),
101        width = opts.width,
102        height = opts.height,
103        theme = opts.theme.as_str(),
104        buddy = opts.buddy,
105        t_ms = t,
106        palette = palette.len(),
107        elapsed_ms = started.elapsed().as_secs_f64() * 1e3,
108        "still rendered"
109    );
110    Ok(Still {
111        image,
112        palette,
113        graph,
114        t_ms: t,
115    })
116}