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