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 atlas;
15pub mod bake;
16pub mod encode;
17pub mod frame;
18pub mod palette;
19
20pub use atlas::GlyphAtlas;
21pub use bake::bake_static;
22pub use encode::{encode_png, save_png, to_rgba};
23pub use frame::render_frame;
24pub use palette::IndexedPalette;
25
26use clairlune_scene::{BuildError, BuildOptions, SceneGraph, SceneRecipe, build};
27
28/// An image of palette indices, row-major.
29#[derive(Clone, Debug, PartialEq, Eq)]
30pub struct IndexedImage {
31    pub width: u32,
32    pub height: u32,
33    pub pixels: Vec<u8>,
34}
35
36impl IndexedImage {
37    pub fn new(width: u32, height: u32) -> Self {
38        Self {
39            width,
40            height,
41            pixels: vec![0; width as usize * height as usize],
42        }
43    }
44}
45
46/// A rendered still frame with everything needed to encode or inspect it.
47#[derive(Clone, Debug)]
48pub struct Still {
49    pub image: IndexedImage,
50    pub palette: IndexedPalette,
51    pub graph: SceneGraph,
52    /// The animation time that was rendered.
53    pub t_ms: u64,
54}
55
56/// Renders a recipe to a still frame at `t_ms`, or at the hero time when `None`.
57pub fn render_still(
58    recipe: &SceneRecipe,
59    opts: &BuildOptions,
60    t_ms: Option<u64>,
61) -> Result<Still, BuildError> {
62    let graph = build(recipe, opts)?;
63    let atlas = GlyphAtlas::for_grid(&graph.grid);
64    let palette = IndexedPalette::new(&graph.family);
65    let background = bake_static(&graph, &atlas);
66    let t = t_ms.unwrap_or(graph.t_hero_ms);
67    let image = render_frame(&background, &graph, &atlas, &palette, t);
68    Ok(Still {
69        image,
70        palette,
71        graph,
72        t_ms: t,
73    })
74}