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 band_top(&self) -> u32 {
100 self.y0 as u32
101 }
102
103 pub fn draw(
106 &self,
107 graph: &SceneGraph,
108 still: &IndexedImage,
109 t_ms: u64,
110 out: &mut IndexedImage,
111 ) {
112 out.pixels.copy_from_slice(&still.pixels);
113 self.lay_strips(graph, t_ms, out);
114 }
115
116 pub fn draw_band(
119 &self,
120 graph: &SceneGraph,
121 still: &IndexedImage,
122 t_ms: u64,
123 out: &mut IndexedImage,
124 ) {
125 let from = self.y0.min(out.height as usize) * out.width as usize;
126 out.pixels[from..].copy_from_slice(&still.pixels[from..]);
127 self.lay_strips(graph, t_ms, out);
128 }
129
130 fn lay_strips(&self, graph: &SceneGraph, t_ms: u64, out: &mut IndexedImage) {
131 let Some(journey) = graph.journey.as_ref() else {
132 return;
133 };
134 let g = &graph.grid;
135 let w = graph.width as usize;
136 let (gx, gw) = (g.origin_x as usize, (g.cols * g.cell_w) as usize);
137 for (i, s) in self.strips.iter().enumerate() {
138 if s.rows == 0 || s.period < gw {
139 continue;
140 }
141 let off = journey.offset_px(i, g.cell_w, t_ms) as usize;
142 let first = (s.period - off).min(gw);
144 for r in 0..s.rows {
145 let y = s.y0 + r;
146 if y >= graph.height as usize {
147 break;
148 }
149 let src = &s.levels[r * s.period..(r + 1) * s.period];
150 let band = &self.bands[(y - self.y0) * w + gx..][..gw];
151 let dst = &mut out.pixels[y * w + gx..][..gw];
152 lay(&mut dst[..first], &src[off..off + first], &band[..first]);
153 if first < gw {
154 lay(&mut dst[first..], &src[..gw - first], &band[first..]);
155 }
156 }
157 }
158 }
159
160 pub fn background_at(
162 &self,
163 graph: &SceneGraph,
164 still: &IndexedImage,
165 t_ms: u64,
166 ) -> IndexedImage {
167 let mut out = still.clone();
168 self.draw(graph, still, t_ms, &mut out);
169 out
170 }
171}
172
173fn lay(dst: &mut [u8], src: &[u8], band: &[u8]) {
175 for ((d, &v), &b) in dst.iter_mut().zip(src).zip(band) {
176 if v != CLEAR {
177 *d = if v == 0 { b } else { IndexedPalette::ink(b, v) };
178 }
179 }
180}