Skip to main content

wisp_storybook/stories/
s_hello_quad.rs

1//! Story: textured quad as a single sprite (M0.6 + M0.9).
2
3use glam::Vec2;
4use wisp::application::Application;
5use wisp::{Sprite, Stage, Texture};
6
7use crate::story::Story;
8
9pub fn story() -> Story {
10    Story {
11        id: "hello-quad",
12        category: "Renderer Foundation",
13        title: "Textured quad",
14        milestone: "M0.6",
15        writeup: include_str!("writeups/hello_quad.md"),
16        build,
17        tick: Some(tick),
18    }
19}
20
21const CHECKER: u32 = 64;
22const CELL: u32 = 8;
23
24fn build(app: &Application, stage: &mut Stage) {
25    let mut bytes = Vec::with_capacity((CHECKER * CHECKER * 4) as usize);
26    for y in 0..CHECKER {
27        for x in 0..CHECKER {
28            let cell_x = (x / CELL) % 2;
29            let cell_y = (y / CELL) % 2;
30            let dark = (cell_x ^ cell_y) == 1;
31            let v = if dark { 64 } else { 224 };
32            bytes.extend_from_slice(&[v, v, v, 255]);
33        }
34    }
35    let texture = Texture::from_rgba(app, CHECKER, CHECKER, &bytes);
36
37    let mut sprite = Sprite::from_texture(texture).with_anchor(Vec2::splat(0.5));
38    sprite.container.transform.scale = Vec2::splat(0.6);
39    let _ = stage.add_child(stage.root(), sprite);
40}
41
42fn tick(stage: &mut Stage, t: f32) {
43    let child_ids: Vec<_> = stage
44        .get(stage.root())
45        .map(|n| n.container().children().collect())
46        .unwrap_or_default();
47    if let Some(id) = child_ids.first()
48        && let Some(node) = stage.get_mut(*id)
49    {
50        node.container_mut().transform.rotation = t * 0.4;
51    }
52}