use crate::IndexedImage;
use crate::art::{BuiltinArt, SceneArt};
use crate::atlas::GlyphAtlas;
use crate::clock::{Clock, LoopScript, loop_track};
use crate::palette::{IndexedPalette, LEVELS, ink_level};
use clairlune_scene::anim::state_at;
use clairlune_scene::graph::CHARACTER_BOX;
use clairlune_scene::particles::{Preset, preset_particles};
use clairlune_scene::{SceneGraph, SpriteInstance, SpriteKind};
use clairlune_sprites::Frame;
pub fn render_frame(
background: &IndexedImage,
graph: &SceneGraph,
atlas: &GlyphAtlas,
palette: &IndexedPalette,
t_ms: u64,
) -> IndexedImage {
render_frame_clocked(background, graph, atlas, palette, t_ms, Clock::Live)
}
pub fn render_frame_clocked(
background: &IndexedImage,
graph: &SceneGraph,
atlas: &GlyphAtlas,
palette: &IndexedPalette,
t_ms: u64,
clock: Clock,
) -> IndexedImage {
let art = BuiltinArt::for_graph(graph);
render_frame_with_art(background, graph, atlas, palette, t_ms, clock, &art)
}
pub fn render_frame_with_art(
background: &IndexedImage,
graph: &SceneGraph,
atlas: &GlyphAtlas,
palette: &IndexedPalette,
t_ms: u64,
clock: Clock,
art: &dyn SceneArt,
) -> IndexedImage {
let mut img = background.clone();
let mut drawn = Vec::new();
compose(
&mut img, &mut drawn, graph, atlas, palette, t_ms, clock, art,
);
img
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PixelRect {
pub x: u32,
pub y: u32,
pub w: u32,
pub h: u32,
}
impl PixelRect {
fn clipped(x0: i32, y0: i32, x1: i32, y1: i32, width: u32, height: u32) -> Option<Self> {
let (x0, y0) = (x0.max(0), y0.max(0));
let (x1, y1) = (x1.min(width as i32), y1.min(height as i32));
(x1 > x0 && y1 > y0).then(|| PixelRect {
x: x0 as u32,
y: y0 as u32,
w: (x1 - x0) as u32,
h: (y1 - y0) as u32,
})
}
pub fn contains(&self, x: u32, y: u32) -> bool {
x >= self.x && x < self.x + self.w && y >= self.y && y < self.y + self.h
}
}
#[derive(Clone, Debug)]
pub struct IncrementalFrame {
image: IndexedImage,
drawn: Vec<PixelRect>,
}
impl IncrementalFrame {
pub fn new(background: &IndexedImage) -> Self {
IncrementalFrame {
image: background.clone(),
drawn: Vec::new(),
}
}
pub fn image(&self) -> &IndexedImage {
&self.image
}
#[allow(clippy::too_many_arguments)]
pub fn advance(
&mut self,
background: &IndexedImage,
graph: &SceneGraph,
atlas: &GlyphAtlas,
palette: &IndexedPalette,
t_ms: u64,
clock: Clock,
art: &dyn SceneArt,
) -> Vec<PixelRect> {
debug_assert_eq!(
(background.width, background.height),
(self.image.width, self.image.height)
);
let w = self.image.width as usize;
for r in &self.drawn {
for y in r.y..r.y + r.h {
let a = y as usize * w + r.x as usize;
let b = a + r.w as usize;
self.image.pixels[a..b].copy_from_slice(&background.pixels[a..b]);
}
}
let mut drawn = Vec::with_capacity(self.drawn.len());
compose(
&mut self.image,
&mut drawn,
graph,
atlas,
palette,
t_ms,
clock,
art,
);
let mut changed: Vec<PixelRect> = self.drawn.iter().chain(&drawn).copied().collect();
changed.sort_unstable();
changed.dedup();
self.drawn = drawn;
changed
}
}
#[allow(clippy::too_many_arguments)]
fn compose(
img: &mut IndexedImage,
drawn: &mut Vec<PixelRect>,
graph: &SceneGraph,
atlas: &GlyphAtlas,
palette: &IndexedPalette,
t_ms: u64,
clock: Clock,
art: &dyn SceneArt,
) {
for (col, row, p) in graph.particles_at(t_ms) {
draw_particle(
img, drawn, graph, atlas, col, row, p.glyph, p.level, p.ink, false,
);
}
for sprite in graph.sprites_at(t_ms) {
if sprite.kind == SpriteKind::Character {
let sprite = match (clock, graph.character) {
(Clock::Loop(loop_ms), Some(track)) => loop_track(track, loop_ms).at(t_ms),
_ => sprite,
};
if let Some(pose) = character_pose(graph, art, &sprite, t_ms, clock) {
draw_pose(img, drawn, &pose, palette);
draw_effects(img, drawn, graph, art, atlas, &pose, t_ms);
}
continue;
}
draw_sprite(img, drawn, &sprite, art, palette);
}
}
#[allow(clippy::too_many_arguments)]
fn draw_particle(
img: &mut IndexedImage,
drawn: &mut Vec<PixelRect>,
graph: &SceneGraph,
atlas: &GlyphAtlas,
col: u32,
row: u32,
glyph: u8,
level: u8,
ink: bool,
overlay: bool,
) {
let g = &graph.grid;
let shade =
((u32::from(level.min(LEVELS)) * 255 + u32::from(LEVELS) / 2) / u32::from(LEVELS)) as u8;
let mask = atlas.mask(glyph);
let (x0, y0) = (g.origin_x + col * g.cell_w, g.origin_y + row * g.cell_h);
drawn.push(PixelRect {
x: x0,
y: y0,
w: g.cell_w,
h: g.cell_h,
});
let w = img.width as usize;
for my in 0..g.cell_h {
let y = y0 + my;
let rt = graph.lighting.row_terms(y);
for mx in 0..g.cell_w {
let x = x0 + mx;
let band = graph.lighting.band_in_row(&rt, x);
let lvl = ink_level(mask[(my * g.cell_w + mx) as usize], shade);
if overlay && lvl == 0 {
continue; }
img.pixels[y as usize * w + x as usize] = match (lvl > 0, ink) {
(false, _) => band,
(true, false) => IndexedPalette::ember(band, lvl),
(true, true) => IndexedPalette::ink(band, lvl),
};
}
}
}
pub fn hero_time(graph: &SceneGraph) -> u64 {
hero_time_with_art(graph, &BuiltinArt::for_graph(graph))
}
pub fn hero_time_with_art(graph: &SceneGraph, art: &dyn SceneArt) -> u64 {
let base = graph.t_hero_ms;
let (Some(a), Some(_)) = (art.activity(), graph.character) else {
return base;
};
let character = art.character();
for k in 0..200u64 {
let t = base + k * 50;
let st = state_at(a, graph.seed, t, &|s| character.one_shot_ms(&a.id, s));
if st.state != a.animation.start {
continue;
}
let looping = a.animation.states.get(&st.state).is_some_and(|d| d.looping);
if let Some(anim) = character.animation(&a.id, &st.state)
&& anim.frame_index_at(t - st.since_ms, looping) == 0
{
return t;
}
}
base
}
struct Pose<'a> {
frame: &'a Frame,
state: String,
x0: i32,
y0: i32,
sc: i32,
mirror: bool,
}
impl Pose<'_> {
fn screen(&self, col: i32, row: i32) -> (i32, i32) {
let (ox, oy) = self.frame.origin();
let dx = (col - ox) * if self.mirror { -1 } else { 1 };
(
self.x0 + dx * self.sc + self.sc / 2,
self.y0 + (row - oy) * self.sc + self.sc / 2,
)
}
}
fn character_pose<'a>(
graph: &SceneGraph,
art: &'a dyn SceneArt,
s: &SpriteInstance,
t_ms: u64,
clock: Clock,
) -> Option<Pose<'a>> {
let character = art.character();
let (anim, state, elapsed, looping) = match (art.activity(), clock) {
(Some(a), Clock::Loop(loop_ms)) => {
let script = LoopScript::new(a, character, loop_ms);
let (state, elapsed) = script.at(t_ms);
(
character.animation(&a.id, state),
state.to_string(),
elapsed,
true,
)
}
(Some(a), Clock::Live) => {
let st = state_at(a, graph.seed, t_ms, &|state| {
character.one_shot_ms(&a.id, state)
});
let looping = a.animation.states.get(&st.state).is_some_and(|d| d.looping);
(
character.animation(&a.id, &st.state),
st.state.clone(),
t_ms - st.since_ms,
looping,
)
}
(None, _) => (
character.animation("", "idle"),
"idle".to_string(),
t_ms,
true,
),
};
let frame = anim?.frame_at(elapsed, looping);
let sc = s.scale as i32;
Some(Pose {
frame,
state,
x0: s.x + (CHARACTER_BOX.0 as i32 / 2) * sc,
y0: s.y + (CHARACTER_BOX.1 as i32 - 1) * sc,
sc,
mirror: s.mirror,
})
}
fn draw_effects(
img: &mut IndexedImage,
drawn: &mut Vec<PixelRect>,
graph: &SceneGraph,
art: &dyn SceneArt,
atlas: &GlyphAtlas,
pose: &Pose,
t_ms: u64,
) {
let Some(a) = art.activity() else {
return;
};
let g = &graph.grid;
let dir = if pose.mirror { -1.0 } else { 1.0 };
for (i, e) in a.effects.iter().enumerate() {
if !e.active_in(&pose.state) {
continue;
}
let (Some(&(ax, ay)), Some(preset)) =
(pose.frame.anchors.get(&e.at), Preset::from_id(&e.preset))
else {
continue;
};
let (cx, cy) = pose.screen(ax, ay);
let seed = graph.seed.wrapping_add(i as u64 + 1);
for p in preset_particles(preset, seed, t_ms) {
let x = f64::from(cx) + p.dx * dir * f64::from(pose.sc);
let y = f64::from(cy) + p.dy * f64::from(pose.sc);
let col = ((x - f64::from(g.origin_x)) / f64::from(g.cell_w)).floor();
let row = ((y - f64::from(g.origin_y)) / f64::from(g.cell_h)).floor();
if col < 0.0 || row < 0.0 || col >= f64::from(g.cols) || row >= f64::from(g.rows) {
continue;
}
draw_particle(
img,
drawn,
graph,
atlas,
col as u32,
row as u32,
p.glyph,
p.level,
preset == Preset::Steam,
true,
);
}
}
}
fn draw_pose(
img: &mut IndexedImage,
drawn: &mut Vec<PixelRect>,
pose: &Pose,
palette: &IndexedPalette,
) {
let f = pose.frame;
let (ox, oy) = f.origin();
let (x0, y0, sc, s) = (pose.x0, pose.y0, pose.sc, pose);
let (w, h) = (img.width as i32, img.height as i32);
let (fw, fh) = (f.width as i32, f.height as i32);
let (left, right) = if s.mirror {
(x0 - (fw - 1 - ox) * sc, x0 + (ox + 1) * sc)
} else {
(x0 - ox * sc, x0 + (fw - ox) * sc)
};
if let Some(r) = PixelRect::clipped(
left,
y0 - oy * sc,
right,
y0 + (fh - oy) * sc,
img.width,
img.height,
) {
drawn.push(r);
}
for row in 0..f.height {
for col in 0..f.width {
let Some(rgb) = f.pixel(col, row) else {
continue;
};
let idx = palette
.sprite_index(rgb)
.expect("character colour in palette");
let dx = (col as i32 - ox) * if s.mirror { -1 } else { 1 };
let px = x0 + dx * sc;
let py = y0 + (row as i32 - oy) * sc;
for y in py.max(0)..(py + sc).min(h) {
let line = (y * w) as usize;
for x in px.max(0)..(px + sc).min(w) {
img.pixels[line + x as usize] = idx;
}
}
}
}
}
fn draw_sprite(
img: &mut IndexedImage,
drawn: &mut Vec<PixelRect>,
s: &SpriteInstance,
art: &dyn SceneArt,
palette: &IndexedPalette,
) {
let Some(map) = art.prop(s.kind) else {
return;
};
let (w, h) = (img.width as i32, img.height as i32);
let scale = s.scale as i32;
let (mw, mh) = (map.width(), map.height());
if let Some(r) = PixelRect::clipped(
s.x,
s.y,
s.x + mw as i32 * scale,
s.y + mh as i32 * scale,
img.width,
img.height,
) {
drawn.push(r);
}
for my in 0..mh {
for mx in 0..mw {
let sx = if s.mirror { mw - 1 - mx } else { mx };
let Some(rgb) = map.pixel(sx, my) else {
continue;
};
let idx = palette.sprite_index(rgb).expect("sprite colour in palette");
let px = s.x + mx as i32 * scale;
let py = s.y + my as i32 * scale;
for y in py.max(0)..(py + scale).min(h) {
let line = (y * w) as usize;
for x in px.max(0)..(px + scale).min(w) {
img.pixels[line + x as usize] = idx;
}
}
}
}
}