Skip to main content

clairlune_render/
art.rs

1//! Everything a frame draws besides the background: the activity's state
2//! machine, the character's frames and the props' pixel art. Live scenes take
3//! them from the built-in catalog and art ([`BuiltinArt`]); saved scenes carry
4//! their own copy (`crate::package`), so they play the same after the
5//! built-in art or activities change.
6
7use clairlune_scene::activity::{Activity, builtin_catalog};
8use clairlune_scene::placeholder::PixelMap;
9use clairlune_scene::{Rgb, SceneGraph, SpriteKind};
10use clairlune_sprites::{Character, builtin_character};
11
12/// A small pixel image with transparency (prop art).
13pub trait Pixmap {
14    fn width(&self) -> u32;
15    fn height(&self) -> u32;
16    /// Colour of pixel `(x, y)`, or `None` when transparent.
17    fn pixel(&self, x: u32, y: u32) -> Option<Rgb>;
18}
19
20impl Pixmap for PixelMap {
21    fn width(&self) -> u32 {
22        self.width
23    }
24    fn height(&self) -> u32 {
25        self.height
26    }
27    fn pixel(&self, x: u32, y: u32) -> Option<Rgb> {
28        PixelMap::pixel(self, x, y)
29    }
30}
31
32/// An owned pixel image (props copied into a saved scene).
33#[derive(Clone, Debug, PartialEq, Eq)]
34pub struct OwnedPixmap {
35    pub width: u32,
36    pub height: u32,
37    pub pixels: Vec<Option<Rgb>>,
38}
39
40impl OwnedPixmap {
41    pub fn copy_of(p: &dyn Pixmap) -> Self {
42        let pixels = (0..p.height())
43            .flat_map(|y| (0..p.width()).map(move |x| (x, y)))
44            .map(|(x, y)| p.pixel(x, y))
45            .collect();
46        OwnedPixmap {
47            width: p.width(),
48            height: p.height(),
49            pixels,
50        }
51    }
52}
53
54impl Pixmap for OwnedPixmap {
55    fn width(&self) -> u32 {
56        self.width
57    }
58    fn height(&self) -> u32 {
59        self.height
60    }
61    fn pixel(&self, x: u32, y: u32) -> Option<Rgb> {
62        self.pixels[(y * self.width + x) as usize]
63    }
64}
65
66/// The art a scene is drawn with.
67pub trait SceneArt {
68    /// The scene's activity (state machine, effects), if it has one.
69    fn activity(&self) -> Option<&Activity>;
70    fn character(&self) -> &dyn Character;
71    /// Pixel art for a prop.
72    fn prop(&self, kind: SpriteKind) -> Option<&dyn Pixmap>;
73}
74
75/// The built-in catalog and art, for scenes made by this generator.
76pub struct BuiltinArt {
77    activity: Option<&'static Activity>,
78}
79
80impl BuiltinArt {
81    pub fn for_graph(graph: &SceneGraph) -> Self {
82        BuiltinArt {
83            activity: graph
84                .activity
85                .as_deref()
86                .and_then(|id| builtin_catalog().get(id)),
87        }
88    }
89}
90
91impl SceneArt for BuiltinArt {
92    fn activity(&self) -> Option<&Activity> {
93        self.activity
94    }
95    fn character(&self) -> &dyn Character {
96        builtin_character()
97    }
98    fn prop(&self, kind: SpriteKind) -> Option<&dyn Pixmap> {
99        Some(kind.map())
100    }
101}