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//!
5//! The activity, character and prop art come through [`SceneArt`], so the same
6//! code plays live scenes (built-in art) and saved ones (their own copy).
7//!
8//! Every draw records the rectangle it writes, so [`IncrementalFrame`] can make
9//! the next frame from the last one by restoring and redrawing only those
10//! rectangles (the live wallpaper, PLAN.md section 5.3), and report what changed.
11
12use crate::IndexedImage;
13use crate::art::{BuiltinArt, SceneArt};
14use crate::atlas::GlyphAtlas;
15use crate::clock::{Clock, LoopScript, loop_track};
16use crate::palette::{IndexedPalette, LEVELS, ink_level};
17use crate::projectile::draw_projectile;
18use crate::tether::{Anchor, draw_tether};
19use clairlune_scene::anim::state_at;
20use clairlune_scene::graph::CHARACTER_BOX;
21use clairlune_scene::particles::{Preset, preset_particles};
22use clairlune_scene::{SceneGraph, SpriteInstance, SpriteKind};
23use clairlune_sprites::Frame;
24
25/// Composites one frame at `t_ms` over a baked background (live clock).
26pub fn render_frame(
27    background: &IndexedImage,
28    graph: &SceneGraph,
29    atlas: &GlyphAtlas,
30    palette: &IndexedPalette,
31    t_ms: u64,
32) -> IndexedImage {
33    render_frame_clocked(background, graph, atlas, palette, t_ms, Clock::Live)
34}
35
36/// Like [`render_frame`], with an explicit character clock: `Clock::Loop` makes
37/// the character follow the activity's loop script for a seamless GIF.
38pub fn render_frame_clocked(
39    background: &IndexedImage,
40    graph: &SceneGraph,
41    atlas: &GlyphAtlas,
42    palette: &IndexedPalette,
43    t_ms: u64,
44    clock: Clock,
45) -> IndexedImage {
46    let art = BuiltinArt::for_graph(graph);
47    render_frame_with_art(background, graph, atlas, palette, t_ms, clock, &art)
48}
49
50/// Composites one frame with the given art (built-in, or a saved scene's).
51pub fn render_frame_with_art(
52    background: &IndexedImage,
53    graph: &SceneGraph,
54    atlas: &GlyphAtlas,
55    palette: &IndexedPalette,
56    t_ms: u64,
57    clock: Clock,
58    art: &dyn SceneArt,
59) -> IndexedImage {
60    let mut img = background.clone();
61    let mut drawn = Vec::new();
62    compose(
63        &mut img, &mut drawn, graph, atlas, palette, t_ms, clock, art,
64    );
65    img
66}
67
68/// A rectangle of pixels, inside the image it belongs to.
69#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
70pub struct PixelRect {
71    pub x: u32,
72    pub y: u32,
73    pub w: u32,
74    pub h: u32,
75}
76
77impl PixelRect {
78    /// `[x0, x1) x [y0, y1)` clipped to a `width x height` image; `None` when
79    /// nothing is left.
80    pub(crate) fn clipped(
81        x0: i32,
82        y0: i32,
83        x1: i32,
84        y1: i32,
85        width: u32,
86        height: u32,
87    ) -> Option<Self> {
88        let (x0, y0) = (x0.max(0), y0.max(0));
89        let (x1, y1) = (x1.min(width as i32), y1.min(height as i32));
90        (x1 > x0 && y1 > y0).then(|| PixelRect {
91            x: x0 as u32,
92            y: y0 as u32,
93            w: (x1 - x0) as u32,
94            h: (y1 - y0) as u32,
95        })
96    }
97
98    pub fn contains(&self, x: u32, y: u32) -> bool {
99        x >= self.x && x < self.x + self.w && y >= self.y && y < self.y + self.h
100    }
101}
102
103/// A frame kept between calls: each new time redraws only what the last
104/// frame and this one draw over the background. The image always equals
105/// [`render_frame_with_art`] at the last time given.
106#[derive(Clone, Debug)]
107pub struct IncrementalFrame {
108    image: IndexedImage,
109    /// What the frame drew over the background (restored before the next).
110    drawn: Vec<PixelRect>,
111}
112
113impl IncrementalFrame {
114    /// Starts as the bare background (nothing drawn yet).
115    pub fn new(background: &IndexedImage) -> Self {
116        IncrementalFrame {
117            image: background.clone(),
118            drawn: Vec::new(),
119        }
120    }
121
122    pub fn image(&self) -> &IndexedImage {
123        &self.image
124    }
125
126    /// Moves to `t_ms` and returns the rectangles whose pixels may have
127    /// changed (sorted, without repeats): the only ones a copy of the last
128    /// frame needs, such as a GPU texture.
129    #[allow(clippy::too_many_arguments)]
130    pub fn advance(
131        &mut self,
132        background: &IndexedImage,
133        graph: &SceneGraph,
134        atlas: &GlyphAtlas,
135        palette: &IndexedPalette,
136        t_ms: u64,
137        clock: Clock,
138        art: &dyn SceneArt,
139    ) -> Vec<PixelRect> {
140        debug_assert_eq!(
141            (background.width, background.height),
142            (self.image.width, self.image.height)
143        );
144        let w = self.image.width as usize;
145        for r in &self.drawn {
146            for y in r.y..r.y + r.h {
147                let a = y as usize * w + r.x as usize;
148                let b = a + r.w as usize;
149                self.image.pixels[a..b].copy_from_slice(&background.pixels[a..b]);
150            }
151        }
152        let mut drawn = Vec::with_capacity(self.drawn.len());
153        compose(
154            &mut self.image,
155            &mut drawn,
156            graph,
157            atlas,
158            palette,
159            t_ms,
160            clock,
161            art,
162        );
163        let mut changed: Vec<PixelRect> = self.drawn.iter().chain(&drawn).copied().collect();
164        changed.sort_unstable();
165        changed.dedup();
166        self.drawn = drawn;
167        changed
168    }
169}
170
171impl IncrementalFrame {
172    /// Like [`IncrementalFrame::advance`] for a journey, whose background
173    /// itself moves: the whole frame is redrawn (the background at `t_ms`,
174    /// then everything over it) and reported as changed.
175    #[allow(clippy::too_many_arguments)]
176    pub fn advance_journey(
177        &mut self,
178        journey: &crate::JourneyBake,
179        still: &IndexedImage,
180        graph: &SceneGraph,
181        atlas: &GlyphAtlas,
182        palette: &IndexedPalette,
183        t_ms: u64,
184        clock: Clock,
185        art: &dyn SceneArt,
186    ) -> Vec<PixelRect> {
187        journey.draw(graph, still, t_ms, &mut self.image);
188        let mut drawn = Vec::with_capacity(self.drawn.len());
189        compose(
190            &mut self.image,
191            &mut drawn,
192            graph,
193            atlas,
194            palette,
195            t_ms,
196            clock,
197            art,
198        );
199        self.drawn = drawn;
200        vec![PixelRect {
201            x: 0,
202            y: 0,
203            w: self.image.width,
204            h: self.image.height,
205        }]
206    }
207}
208
209/// Draws the animated elements at `t_ms` over `img` (the background, or a
210/// frame restored to it) and records each rectangle written in `drawn`.
211#[allow(clippy::too_many_arguments)]
212fn compose(
213    img: &mut IndexedImage,
214    drawn: &mut Vec<PixelRect>,
215    graph: &SceneGraph,
216    atlas: &GlyphAtlas,
217    palette: &IndexedPalette,
218    t_ms: u64,
219    clock: Clock,
220    art: &dyn SceneArt,
221) {
222    if clock == Clock::Smooth {
223        for p in graph.particles_in_grid(t_ms) {
224            draw_particle_at(img, drawn, graph, atlas, p.x, p.y, p.glyph, p.level, p.ink);
225        }
226    } else {
227        for (col, row, p) in graph.particles_at(t_ms) {
228            draw_particle(
229                img, drawn, graph, atlas, col, row, p.glyph, p.level, p.ink, false,
230            );
231        }
232    }
233    for sprite in graph.sprites_at(t_ms) {
234        if sprite.kind == SpriteKind::Character {
235            let sprite = match (clock, graph.character) {
236                (Clock::Loop(loop_ms), Some(track)) => {
237                    graph.on_ground(loop_track(track, loop_ms).at(t_ms), t_ms)
238                }
239                _ => sprite,
240            };
241            if let Some(pose) = character_pose(graph, art, &sprite, t_ms, clock) {
242                // Effects go on top as an overlay (only the glyph's own pixels),
243                // so steam over a mug or sparkles over the hat stay visible.
244                draw_pose(img, drawn, &pose, palette);
245                draw_effects(img, drawn, graph, art, atlas, palette, &pose, t_ms, clock);
246            }
247            continue;
248        }
249        draw_sprite(img, drawn, &sprite, art, palette);
250    }
251}
252
253/// Draws a particle glyph in ember colour, or ink colour when `ink` (faint
254/// wisps such as steam). Normally it replaces its whole cell (background band,
255/// then the glyph); with `overlay` only the glyph's pixels are written.
256#[allow(clippy::too_many_arguments)]
257pub(crate) fn draw_particle(
258    img: &mut IndexedImage,
259    drawn: &mut Vec<PixelRect>,
260    graph: &SceneGraph,
261    atlas: &GlyphAtlas,
262    col: u32,
263    row: u32,
264    glyph: u8,
265    level: u8,
266    ink: bool,
267    overlay: bool,
268) {
269    let g = &graph.grid;
270    let shade =
271        ((u32::from(level.min(LEVELS)) * 255 + u32::from(LEVELS) / 2) / u32::from(LEVELS)) as u8;
272    let mask = atlas.mask(glyph);
273    let (x0, y0) = (g.origin_x + col * g.cell_w, g.origin_y + row * g.cell_h);
274    // Cells lie inside the image (the grid is laid out within it).
275    drawn.push(PixelRect {
276        x: x0,
277        y: y0,
278        w: g.cell_w,
279        h: g.cell_h,
280    });
281    let w = img.width as usize;
282    for my in 0..g.cell_h {
283        let y = y0 + my;
284        let rt = graph.lighting.row_terms(y);
285        for mx in 0..g.cell_w {
286            let x = x0 + mx;
287            let band = graph.lighting.band_in_row(&rt, x);
288            let lvl = ink_level(mask[(my * g.cell_w + mx) as usize], shade);
289            if overlay && lvl == 0 {
290                continue; // overlay: leave whatever is under the glyph
291            }
292            img.pixels[y as usize * w + x as usize] = match (lvl > 0, ink) {
293                (false, _) => band,
294                (true, false) => IndexedPalette::ember(band, lvl),
295                (true, true) => IndexedPalette::ink(band, lvl),
296            };
297        }
298    }
299}
300
301/// Draws a particle glyph centred on pixel `(cx, cy)` (`Clock::Smooth`): only
302/// the glyph's own pixels, over whatever is there, clipped to the image.
303#[allow(clippy::too_many_arguments)]
304pub(crate) fn draw_particle_at(
305    img: &mut IndexedImage,
306    drawn: &mut Vec<PixelRect>,
307    graph: &SceneGraph,
308    atlas: &GlyphAtlas,
309    cx: f64,
310    cy: f64,
311    glyph: u8,
312    level: u8,
313    ink: bool,
314) {
315    let g = &graph.grid;
316    let shade =
317        ((u32::from(level.min(LEVELS)) * 255 + u32::from(LEVELS) / 2) / u32::from(LEVELS)) as u8;
318    let mask = atlas.mask(glyph);
319    let x0 = (cx - f64::from(g.cell_w) / 2.0).round() as i32;
320    let y0 = (cy - f64::from(g.cell_h) / 2.0).round() as i32;
321    let x1 = x0 + g.cell_w as i32;
322    let y1 = y0 + g.cell_h as i32;
323    let Some(rect) = PixelRect::clipped(x0, y0, x1, y1, img.width, img.height) else {
324        return;
325    };
326    drawn.push(rect);
327    let w = img.width as usize;
328    for y in rect.y..rect.y + rect.h {
329        let my = (y as i32 - y0) as u32;
330        let rt = graph.lighting.row_terms(y);
331        for x in rect.x..rect.x + rect.w {
332            let mx = (x as i32 - x0) as u32;
333            let lvl = ink_level(mask[(my * g.cell_w + mx) as usize], shade);
334            if lvl == 0 {
335                continue;
336            }
337            let band = graph.lighting.band_in_row(&rt, x);
338            img.pixels[y as usize * w + x as usize] = if ink {
339                IndexedPalette::ink(band, lvl)
340            } else {
341                IndexedPalette::ember(band, lvl)
342            };
343        }
344    }
345}
346
347/// The still-frame time used for stills, thumbnails and the static wallpaper:
348/// the first moment at or after the scene's ember-based hero time (50 ms steps,
349/// up to 10 s) when the character shows the first frame of its activity's start
350/// state, so a still never catches a blink or a mid-gesture. Independent of the
351/// Buddy toggle, so toggling it keeps the same moment.
352pub fn hero_time(graph: &SceneGraph) -> u64 {
353    hero_time_with_art(graph, &BuiltinArt::for_graph(graph))
354}
355
356/// [`hero_time`] with the given art.
357pub fn hero_time_with_art(graph: &SceneGraph, art: &dyn SceneArt) -> u64 {
358    let base = graph.t_hero_ms;
359    let (Some(a), Some(_)) = (art.activity(), graph.character) else {
360        return base;
361    };
362    let character = art.character();
363    for k in 0..200u64 {
364        let t = base + k * 50;
365        let st = state_at(a, graph.seed, t, &|s| character.one_shot_ms(&a.id, s));
366        if st.state != a.animation.start {
367            continue;
368        }
369        let looping = a.animation.states.get(&st.state).is_some_and(|d| d.looping);
370        if let Some(anim) = character.animation(&a.id, &st.state)
371            && anim.frame_index_at(t - st.since_ms, looping) == 0
372        {
373            return t;
374        }
375    }
376    base
377}
378
379/// The character's current frame and where it sits on screen: the art's origin
380/// (feet row, body centre) aligned inside the layout box, mirrored when it faces
381/// left. All character art comes through `Character`.
382struct Pose<'a> {
383    frame: &'a Frame,
384    state: String,
385    /// Time into the state, in ms (thrown things fly on it).
386    elapsed: u64,
387    /// Screen pixel of the origin pixel's top-left corner.
388    x0: i32,
389    y0: i32,
390    sc: i32,
391    mirror: bool,
392}
393
394impl Pose<'_> {
395    /// Screen position of the centre of frame pixel `(col, row)`.
396    fn screen(&self, col: i32, row: i32) -> (i32, i32) {
397        let (ox, oy) = self.frame.origin();
398        let dx = (col - ox) * if self.mirror { -1 } else { 1 };
399        (
400            self.x0 + dx * self.sc + self.sc / 2,
401            self.y0 + (row - oy) * self.sc + self.sc / 2,
402        )
403    }
404}
405
406/// Picks the frame for the activity state at `t` (state machine when live, the
407/// loop script when looping) and places it.
408fn character_pose<'a>(
409    graph: &SceneGraph,
410    art: &'a dyn SceneArt,
411    s: &SpriteInstance,
412    t_ms: u64,
413    clock: Clock,
414) -> Option<Pose<'a>> {
415    let character = art.character();
416    let (anim, state, elapsed, looping) = match (art.activity(), clock) {
417        (Some(a), Clock::Loop(loop_ms)) => {
418            let script = LoopScript::new(a, character, loop_ms);
419            let (state, elapsed) = script.at(t_ms);
420            (
421                character.animation(&a.id, state),
422                state.to_string(),
423                elapsed,
424                true,
425            )
426        }
427        (Some(a), Clock::Live | Clock::Smooth) => {
428            let st = state_at(a, graph.seed, t_ms, &|state| {
429                character.one_shot_ms(&a.id, state)
430            });
431            let looping = a.animation.states.get(&st.state).is_some_and(|d| d.looping);
432            (
433                character.animation(&a.id, &st.state),
434                st.state.clone(),
435                t_ms - st.since_ms,
436                looping,
437            )
438        }
439        (None, _) => (
440            character.animation("", "idle"),
441            "idle".to_string(),
442            t_ms,
443            true,
444        ),
445    };
446    let frame = anim?.frame_at(elapsed, looping);
447    let sc = s.scale as i32;
448    Some(Pose {
449        frame,
450        state,
451        elapsed,
452        x0: s.x + (CHARACTER_BOX.0 as i32 / 2) * sc,
453        y0: s.y + (CHARACTER_BOX.1 as i32 - 1) * sc,
454        sc,
455        mirror: s.mirror,
456    })
457}
458
459/// The activity's `[[effects]]` for the current state, at their anchors on the
460/// current frame. Particles snap to cells like the embers, except with
461/// `Clock::Smooth`.
462#[allow(clippy::too_many_arguments)]
463fn draw_effects(
464    img: &mut IndexedImage,
465    drawn: &mut Vec<PixelRect>,
466    graph: &SceneGraph,
467    art: &dyn SceneArt,
468    atlas: &GlyphAtlas,
469    palette: &IndexedPalette,
470    pose: &Pose,
471    t_ms: u64,
472    clock: Clock,
473) {
474    let Some(a) = art.activity() else {
475        return;
476    };
477    let g = &graph.grid;
478    let dir = if pose.mirror { -1.0 } else { 1.0 };
479    for (i, e) in a.effects.iter().enumerate() {
480        if !e.active_in(&pose.state) {
481            continue;
482        }
483        if e.kind == "projectile" {
484            // Thrown from where the anchor is on the state's first frame, so
485            // the flight doesn't jump when the pose changes mid-air.
486            let first = art
487                .character()
488                .animation(&a.id, &pose.state)
489                .and_then(|an| an.frames.first())
490                .and_then(|f| f.anchors.get(&e.at).copied());
491            if let Some((ax, ay)) = first {
492                let (x, y) = pose.screen(ax, ay);
493                let anchor = Anchor {
494                    x,
495                    y,
496                    sc: pose.sc,
497                    dir: if pose.mirror { -1 } else { 1 },
498                    state: &pose.state,
499                };
500                draw_projectile(
501                    img,
502                    drawn,
503                    graph,
504                    atlas,
505                    palette,
506                    &e.preset,
507                    &anchor,
508                    pose.elapsed,
509                    clock,
510                );
511            }
512            continue;
513        }
514        if e.kind == "tether" {
515            if let Some(&(ax, ay)) = pose.frame.anchors.get(&e.at) {
516                let (x, y) = pose.screen(ax, ay);
517                let anchor = Anchor {
518                    x,
519                    y,
520                    sc: pose.sc,
521                    dir: if pose.mirror { -1 } else { 1 },
522                    state: &pose.state,
523                };
524                draw_tether(
525                    img, drawn, graph, atlas, palette, &e.preset, &anchor, t_ms, clock,
526                );
527            }
528            continue;
529        }
530        let (Some(&(ax, ay)), Some(preset)) =
531            (pose.frame.anchors.get(&e.at), Preset::from_id(&e.preset))
532        else {
533            continue;
534        };
535        let (cx, cy) = pose.screen(ax, ay);
536        let seed = graph.seed.wrapping_add(i as u64 + 1);
537        for p in preset_particles(preset, seed, t_ms, graph.pace) {
538            let x = f64::from(cx) + p.dx * dir * f64::from(pose.sc);
539            let y = f64::from(cy) + p.dy * f64::from(pose.sc);
540            if clock == Clock::Smooth {
541                let (gx0, gy0) = (f64::from(g.origin_x), f64::from(g.origin_y));
542                let inside = x >= gx0
543                    && y >= gy0
544                    && x < gx0 + f64::from(g.cols * g.cell_w)
545                    && y < gy0 + f64::from(g.rows * g.cell_h);
546                if inside {
547                    let steam = matches!(preset, Preset::Steam | Preset::SnowSpray);
548                    draw_particle_at(img, drawn, graph, atlas, x, y, p.glyph, p.level, steam);
549                }
550                continue;
551            }
552            let col = ((x - f64::from(g.origin_x)) / f64::from(g.cell_w)).floor();
553            let row = ((y - f64::from(g.origin_y)) / f64::from(g.cell_h)).floor();
554            if col < 0.0 || row < 0.0 || col >= f64::from(g.cols) || row >= f64::from(g.rows) {
555                continue;
556            }
557            draw_particle(
558                img,
559                drawn,
560                graph,
561                atlas,
562                col as u32,
563                row as u32,
564                p.glyph,
565                p.level,
566                matches!(preset, Preset::Steam | Preset::SnowSpray),
567                true,
568            );
569        }
570    }
571}
572
573fn draw_pose(
574    img: &mut IndexedImage,
575    drawn: &mut Vec<PixelRect>,
576    pose: &Pose,
577    palette: &IndexedPalette,
578) {
579    let f = pose.frame;
580    let (ox, oy) = f.origin();
581    let (x0, y0, sc, s) = (pose.x0, pose.y0, pose.sc, pose);
582    let (w, h) = (img.width as i32, img.height as i32);
583    // The frame's box on screen (mirroring flips it around the origin).
584    let (fw, fh) = (f.width as i32, f.height as i32);
585    let (left, right) = if s.mirror {
586        (x0 - (fw - 1 - ox) * sc, x0 + (ox + 1) * sc)
587    } else {
588        (x0 - ox * sc, x0 + (fw - ox) * sc)
589    };
590    if let Some(r) = PixelRect::clipped(
591        left,
592        y0 - oy * sc,
593        right,
594        y0 + (fh - oy) * sc,
595        img.width,
596        img.height,
597    ) {
598        drawn.push(r);
599    }
600    for row in 0..f.height {
601        for col in 0..f.width {
602            let Some(rgb) = f.pixel(col, row) else {
603                continue;
604            };
605            let idx = palette
606                .sprite_index(rgb)
607                .expect("character colour in palette");
608            let dx = (col as i32 - ox) * if s.mirror { -1 } else { 1 };
609            let px = x0 + dx * sc;
610            let py = y0 + (row as i32 - oy) * sc;
611            for y in py.max(0)..(py + sc).min(h) {
612                let line = (y * w) as usize;
613                for x in px.max(0)..(px + sc).min(w) {
614                    img.pixels[line + x as usize] = idx;
615                }
616            }
617        }
618    }
619}
620
621fn draw_sprite(
622    img: &mut IndexedImage,
623    drawn: &mut Vec<PixelRect>,
624    s: &SpriteInstance,
625    art: &dyn SceneArt,
626    palette: &IndexedPalette,
627) {
628    let Some(map) = art.prop(s.kind) else {
629        return;
630    };
631    let (w, h) = (img.width as i32, img.height as i32);
632    let scale = s.scale as i32;
633    let (mw, mh) = (map.width(), map.height());
634    if let Some(r) = PixelRect::clipped(
635        s.x,
636        s.y,
637        s.x + mw as i32 * scale,
638        s.y + mh as i32 * scale,
639        img.width,
640        img.height,
641    ) {
642        drawn.push(r);
643    }
644    for my in 0..mh {
645        for mx in 0..mw {
646            let sx = if s.mirror { mw - 1 - mx } else { mx };
647            let Some(rgb) = map.pixel(sx, my) else {
648                continue;
649            };
650            let idx = palette.sprite_index(rgb).expect("sprite colour in palette");
651            let px = s.x + mx as i32 * scale;
652            let py = s.y + my as i32 * scale;
653            for y in py.max(0)..(py + scale).min(h) {
654                let line = (y * w) as usize;
655                for x in px.max(0)..(px + scale).min(w) {
656                    img.pixels[line + x as usize] = idx;
657                }
658            }
659        }
660    }
661}