Skip to main content

clairlune_render/
bake.rs

1//! Static bake: background bands plus every glyph cell, rendered once per scene.
2
3use crate::IndexedImage;
4use crate::atlas::GlyphAtlas;
5use crate::palette::{IndexedPalette, ink_level};
6use clairlune_scene::SceneGraph;
7use clairlune_scene::glyphs::EMPTY;
8
9/// Renders the static background of a scene graph into palette indices.
10pub fn bake_static(graph: &SceneGraph, atlas: &GlyphAtlas) -> IndexedImage {
11    let mut img = IndexedImage::new(graph.width, graph.height);
12    let w = graph.width as usize;
13    for (y, row) in img.pixels.chunks_exact_mut(w).enumerate() {
14        graph.lighting.fill_row(y as u32, row);
15    }
16
17    let g = &graph.grid;
18    let (cw, ch) = (g.cell_w as usize, g.cell_h as usize);
19    for row in 0..g.rows {
20        for col in 0..g.cols {
21            let cell = graph.cell(col, row);
22            if cell.glyph == EMPTY || cell.shade == 0 {
23                continue;
24            }
25            let mask = atlas.mask(cell.glyph);
26            let x0 = g.origin_x as usize + col as usize * cw;
27            let y0 = g.origin_y as usize + row as usize * ch;
28            for my in 0..ch {
29                let line = (y0 + my) * w + x0;
30                let dst = &mut img.pixels[line..line + cw];
31                let src = &mask[my * cw..(my + 1) * cw];
32                for (p, &a) in dst.iter_mut().zip(src) {
33                    if a == 0 {
34                        continue;
35                    }
36                    let level = ink_level(a, cell.shade);
37                    if level > 0 {
38                        // Cells never overlap, so `*p` is still the background band.
39                        *p = IndexedPalette::ink(*p, level);
40                    }
41                }
42            }
43        }
44    }
45    img
46}