Skip to main content

clairlune_render/
frame.rs

1//! Frame compositing: the baked background plus animated elements at time `t`.
2//! Particles snap to the cell grid (exact palette, small GIF diffs); sprites use
3//! integer nearest-neighbour scaling.
4
5use crate::IndexedImage;
6use crate::atlas::GlyphAtlas;
7use crate::palette::{IndexedPalette, LEVELS, ink_level};
8use clairlune_scene::activity::builtin_catalog;
9use clairlune_scene::anim::state_at;
10use clairlune_scene::graph::CHARACTER_BOX;
11use clairlune_scene::{SceneGraph, SpriteInstance, SpriteKind};
12use clairlune_sprites::{Character, builtin_character};
13
14/// Composites one frame at `t_ms` over a baked background.
15pub fn render_frame(
16    background: &IndexedImage,
17    graph: &SceneGraph,
18    atlas: &GlyphAtlas,
19    palette: &IndexedPalette,
20    t_ms: u64,
21) -> IndexedImage {
22    let mut img = background.clone();
23    for (col, row, p) in graph.particles_at(t_ms) {
24        draw_particle(&mut img, graph, atlas, col, row, p.glyph, p.level);
25    }
26    for sprite in graph.sprites_at(t_ms) {
27        if sprite.kind == SpriteKind::Character {
28            draw_character(&mut img, graph, &sprite, palette, t_ms);
29            continue;
30        }
31        draw_sprite(&mut img, &sprite, palette);
32    }
33    img
34}
35
36/// A particle replaces its whole cell: background band, then the glyph in ember colour.
37fn draw_particle(
38    img: &mut IndexedImage,
39    graph: &SceneGraph,
40    atlas: &GlyphAtlas,
41    col: u32,
42    row: u32,
43    glyph: u8,
44    level: u8,
45) {
46    let g = &graph.grid;
47    let shade =
48        ((u32::from(level.min(LEVELS)) * 255 + u32::from(LEVELS) / 2) / u32::from(LEVELS)) as u8;
49    let mask = atlas.mask(glyph);
50    let (x0, y0) = (g.origin_x + col * g.cell_w, g.origin_y + row * g.cell_h);
51    let w = img.width as usize;
52    for my in 0..g.cell_h {
53        let y = y0 + my;
54        let rt = graph.lighting.row_terms(y);
55        for mx in 0..g.cell_w {
56            let x = x0 + mx;
57            let band = graph.lighting.band_in_row(&rt, x);
58            let lvl = ink_level(mask[(my * g.cell_w + mx) as usize], shade);
59            img.pixels[y as usize * w + x as usize] = if lvl > 0 {
60                IndexedPalette::ember(band, lvl)
61            } else {
62                band
63            };
64        }
65    }
66}
67
68/// Draws the character's animation frame for the activity state at `t`,
69/// aligned on the art's origin (feet row, body centre) inside its layout box,
70/// mirrored when it faces left. All character art comes through `Character`.
71fn draw_character(
72    img: &mut IndexedImage,
73    graph: &SceneGraph,
74    s: &SpriteInstance,
75    palette: &IndexedPalette,
76    t_ms: u64,
77) {
78    let character = builtin_character();
79    let activity = graph
80        .activity
81        .as_deref()
82        .and_then(|id| builtin_catalog().get(id));
83    let (anim, elapsed, looping) = match activity {
84        Some(a) => {
85            let st = state_at(a, graph.seed, t_ms, &|state| {
86                character.one_shot_ms(&a.id, state)
87            });
88            let looping = a.animation.states.get(&st.state).is_some_and(|d| d.looping);
89            (
90                character.animation(&a.id, &st.state),
91                t_ms - st.since_ms,
92                looping,
93            )
94        }
95        None => (character.animation("", "idle"), t_ms, true),
96    };
97    let Some(anim) = anim else { return };
98    let f = anim.frame_at(elapsed, looping);
99    let (ox, oy) = f.origin();
100    let sc = s.scale as i32;
101    // Origin pixel's top-left on screen: box centre column, box bottom row.
102    let x0 = s.x + (CHARACTER_BOX.0 as i32 / 2) * sc;
103    let y0 = s.y + (CHARACTER_BOX.1 as i32 - 1) * sc;
104    let (w, h) = (img.width as i32, img.height as i32);
105    for row in 0..f.height {
106        for col in 0..f.width {
107            let Some(rgb) = f.pixel(col, row) else {
108                continue;
109            };
110            let idx = palette
111                .sprite_index(rgb)
112                .expect("character colour in palette");
113            let dx = (col as i32 - ox) * if s.mirror { -1 } else { 1 };
114            let px = x0 + dx * sc;
115            let py = y0 + (row as i32 - oy) * sc;
116            for y in py.max(0)..(py + sc).min(h) {
117                let line = (y * w) as usize;
118                for x in px.max(0)..(px + sc).min(w) {
119                    img.pixels[line + x as usize] = idx;
120                }
121            }
122        }
123    }
124}
125
126fn draw_sprite(img: &mut IndexedImage, s: &SpriteInstance, palette: &IndexedPalette) {
127    let map = s.kind.map();
128    let (w, h) = (img.width as i32, img.height as i32);
129    let scale = s.scale as i32;
130    for my in 0..map.height {
131        for mx in 0..map.width {
132            let sx = if s.mirror { map.width - 1 - mx } else { mx };
133            let Some(rgb) = map.pixel(sx, my) else {
134                continue;
135            };
136            let idx = palette.sprite_index(rgb).expect("sprite colour in palette");
137            let px = s.x + mx as i32 * scale;
138            let py = s.y + my as i32 * scale;
139            for y in py.max(0)..(py + scale).min(h) {
140                let line = (y * w) as usize;
141                for x in px.max(0)..(px + scale).min(w) {
142                    img.pixels[line + x as usize] = idx;
143                }
144            }
145        }
146    }
147}