demo/
demo.rs

1fn main() {
2    use spottedcat::{Bounds, Context, DrawOption, Image, Pt, Spot, WindowConfig, run};
3
4    struct DemoSpot {
5        tree: Image,
6        image: Image,
7        image_sub: Image,
8        image_clone: Image,
9    }
10
11    impl Spot for DemoSpot {
12        fn initialize(_context: &mut Context) -> Self {
13            const TREE_PNG: &[u8] = include_bytes!("../assets/happy-tree.png");
14            let decoded = image::load_from_memory(TREE_PNG).expect("failed to decode happy-tree.png");
15            let rgba = decoded.to_rgba8();
16            let (w, h) = (rgba.width(), rgba.height());
17            let tree = Image::new_from_rgba8(Pt::from(w), Pt::from(h), rgba.as_raw())
18                .expect("failed to create happy-tree image");
19
20            let mut rgba = vec![0u8; 20 * 20 * 4];
21            for y in 0..20u32 {
22                for x in 0..20u32 {
23                    let i = ((y * 20 + x) * 4) as usize;
24                    let on = ((x / 5 + y / 5) % 2) == 0;
25                    rgba[i] = if on { 255 } else { 30 };
26                    rgba[i + 1] = if on { 80 } else { 200 };
27                    rgba[i + 2] = if on { 80 } else { 255 };
28                    rgba[i + 3] = 255;
29                }
30            }
31            let image =
32                Image::new_from_rgba8(Pt::from(20.0), Pt::from(20.0), &rgba).expect("failed to create test image");
33            let image_sub = Image::sub_image(image, Bounds::new(Pt::from(5.0), Pt::from(5.0), Pt::from(10.0), Pt::from(10.0)))
34                .expect("failed to create sub image");
35            let image_clone = Image::new_from_image(image).expect("failed to create image from image");
36
37            Self {
38                tree,
39                image,
40                image_sub,
41                image_clone,
42            }
43        }
44
45        fn draw(&mut self, context: &mut Context) {
46            let mut opts = DrawOption::default();
47            opts.position = [Pt::from(20.0), Pt::from(300.0)];
48            self.tree.draw(context, opts);
49
50            let mut opts = DrawOption::default();
51            opts.position = [Pt::from(50.0), Pt::from(50.0)];
52            opts.scale = [10.0, 10.0];
53            self.image.draw(context, opts);
54
55            let mut opts = DrawOption::default();
56            opts.position = [Pt::from(300.0), Pt::from(50.0)];
57            opts.scale = [20.0, 20.0];
58            self.image_sub.draw(context, opts);
59
60            let mut opts = DrawOption::default();
61            opts.position = [Pt::from(550.0), Pt::from(50.0)];
62            opts.scale = [10.0, 10.0];
63            self.image_clone.draw(context, opts);
64        }
65
66        fn update(&mut self, context: &mut Context, _dt: std::time::Duration) {
67            let (w, h) = spottedcat::window_size(context);
68            println!("window size: {}x{}", w, h);
69        }
70
71        fn remove(&self) {}
72    }
73
74    run::<DemoSpot>(WindowConfig::default());
75}