clairlune_render/
journey.rs1use crate::IndexedImage;
8use crate::atlas::GlyphAtlas;
9use crate::palette::{IndexedPalette, ink_level};
10use clairlune_scene::glyphs::EMPTY;
11use clairlune_scene::{Cell, SceneGraph};
12
13const CLEAR: u8 = u8::MAX;
15
16#[derive(Clone, Debug, Default)]
19struct LevelStrip {
20 period: usize,
22 y0: usize,
23 rows: usize,
24 levels: Vec<u8>,
25}
26
27#[derive(Clone, Debug)]
29pub struct JourneyBake {
30 strips: Vec<LevelStrip>,
31 bands: Vec<u8>,
34 y0: usize,
35}
36
37impl JourneyBake {
38 pub fn new(graph: &SceneGraph, atlas: &GlyphAtlas) -> Option<Self> {
40 let journey = graph.journey.as_ref()?;
41 let g = &graph.grid;
42 let (cw, ch) = (g.cell_w as usize, g.cell_h as usize);
43 let mut strips = Vec::with_capacity(journey.layers.len());
44 for layer in &journey.layers {
45 let lc = layer.cols as usize;
46 let cell = |col: usize, row: usize| layer.cells[row * lc + col];
47 let Some(first) =
48 (0..g.rows as usize).find(|&r| (0..lc).any(|c| cell(c, r) != Cell::default()))
49 else {
50 strips.push(LevelStrip::default());
51 continue;
52 };
53 let period = lc * cw;
54 let rows = (g.rows as usize - first) * ch;
55 let mut levels = vec![CLEAR; period * rows];
56 for row in first..g.rows as usize {
57 for col in 0..lc {
58 let c = cell(col, row);
59 if c == Cell::default() {
60 continue;
61 }
62 let mask = atlas.mask(c.glyph);
63 let (x0, y0) = (col * cw, (row - first) * ch);
64 for my in 0..ch {
65 let line = &mut levels[(y0 + my) * period + x0..][..cw];
66 for (p, &a) in line.iter_mut().zip(&mask[my * cw..(my + 1) * cw]) {
67 *p = if c.glyph == EMPTY || a == 0 {
68 0
69 } else {
70 ink_level(a, c.shade)
71 };
72 }
73 }
74 }
75 }
76 strips.push(LevelStrip {
77 period,
78 y0: g.origin_y as usize + first * ch,
79 rows,
80 levels,
81 });
82 }
83 let (w, h) = (graph.width as usize, graph.height as usize);
84 let y0 = strips
85 .iter()
86 .filter(|s| s.rows > 0)
87 .map(|s| s.y0)
88 .min()
89 .unwrap_or(h);
90 let mut bands = vec![0u8; (h - y0) * w];
91 for (i, row) in bands.chunks_exact_mut(w).enumerate() {
92 graph.lighting.fill_row((y0 + i) as u32, row);
93 }
94 Some(JourneyBake { strips, bands, y0 })
95 }
96
97 pub fn draw(
100 &self,
101 graph: &SceneGraph,
102 still: &IndexedImage,
103 t_ms: u64,
104 out: &mut IndexedImage,
105 ) {
106 out.pixels.copy_from_slice(&still.pixels);
107 let Some(journey) = graph.journey.as_ref() else {
108 return;
109 };
110 let g = &graph.grid;
111 let w = graph.width as usize;
112 let (gx, gw) = (g.origin_x as usize, (g.cols * g.cell_w) as usize);
113 for (i, s) in self.strips.iter().enumerate() {
114 if s.rows == 0 || s.period < gw {
115 continue;
116 }
117 let off = journey.offset_px(i, g.cell_w, t_ms) as usize;
118 let first = (s.period - off).min(gw);
120 for r in 0..s.rows {
121 let y = s.y0 + r;
122 if y >= graph.height as usize {
123 break;
124 }
125 let src = &s.levels[r * s.period..(r + 1) * s.period];
126 let band = &self.bands[(y - self.y0) * w + gx..][..gw];
127 let dst = &mut out.pixels[y * w + gx..][..gw];
128 lay(&mut dst[..first], &src[off..off + first], &band[..first]);
129 if first < gw {
130 lay(&mut dst[first..], &src[..gw - first], &band[first..]);
131 }
132 }
133 }
134 }
135
136 pub fn background_at(
138 &self,
139 graph: &SceneGraph,
140 still: &IndexedImage,
141 t_ms: u64,
142 ) -> IndexedImage {
143 let mut out = still.clone();
144 self.draw(graph, still, t_ms, &mut out);
145 out
146 }
147}
148
149fn lay(dst: &mut [u8], src: &[u8], band: &[u8]) {
151 for ((d, &v), &b) in dst.iter_mut().zip(src).zip(band) {
152 if v != CLEAR {
153 *d = if v == 0 { b } else { IndexedPalette::ink(b, v) };
154 }
155 }
156}