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