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