Skip to main content

sprites/
sprites.rs

1//! Drawing sprites: position, rotation, scale, tint, draw order and pivot.
2//!
3//! Run with: `cargo run --example sprites`
4//!
5//! - Arrow keys / WASD: move the big crab
6//! - The small crab follows the mouse
7//! - Escape: quit
8
9use ponko::*;
10
11fn main() {
12    Game::new("Crabby Engine: sprites", 800, 600).run(|ctx| {
13        ctx.clear(Color::hex(0x1E2A38));
14        let crab = ctx.load_texture("crab.png");
15
16        // A generated texture: a soft white circle.
17        let size = 32;
18        let mut pixels = Vec::with_capacity(size * size * 4);
19        for y in 0..size {
20            for x in 0..size {
21                let d = vec2(x as f32 + 0.5, y as f32 + 0.5).distance(Vec2::splat(size as f32 / 2.0));
22                let a = ((size as f32 / 2.0 - d).clamp(0.0, 1.0) * 255.0) as u8;
23                pixels.extend_from_slice(&[255, 255, 255, a]);
24            }
25        }
26        let dot = ctx.create_texture(size as u32, size as u32, &pixels);
27
28        let mut player = ctx.window_size() / 2.0;
29
30        move |ctx: &mut Ctx| {
31            let t = ctx.time() as f32;
32
33            let mut dir = Vec2::ZERO;
34            if ctx.key_down(Key::Left) || ctx.key_down(Key::A) {
35                dir.x -= 1.0;
36            }
37            if ctx.key_down(Key::Right) || ctx.key_down(Key::D) {
38                dir.x += 1.0;
39            }
40            if ctx.key_down(Key::Up) || ctx.key_down(Key::W) {
41                dir.y -= 1.0;
42            }
43            if ctx.key_down(Key::Down) || ctx.key_down(Key::S) {
44                dir.y += 1.0;
45            }
46            player += dir.normalize_or_zero() * 250.0 * ctx.dt();
47
48            // A ring of tinted, spinning dots around the player, drawn behind it (z = -1).
49            for i in 0..12 {
50                let angle = t + i as f32 * std::f32::consts::TAU / 12.0;
51                let hue = i as f32 / 12.0;
52                ctx.draw_ex(
53                    &dot,
54                    player + Vec2::from_angle(angle) * 120.0,
55                    DrawParams {
56                        color: Color::rgb(
57                            0.5 + 0.5 * (hue * std::f32::consts::TAU).cos(),
58                            0.6,
59                            0.5 + 0.5 * (hue * std::f32::consts::TAU).sin(),
60                        ),
61                        z: -1.0,
62                        ..Default::default()
63                    },
64                );
65            }
66
67            // The player: big and wobbling. Drawn first in code but z = 1 puts it on top.
68            ctx.draw_ex(
69                &crab,
70                player,
71                DrawParams {
72                    scale: Vec2::splat(4.0),
73                    rotation: (t * 3.0).sin() * 0.2,
74                    z: 1.0,
75                    ..Default::default()
76                },
77            );
78
79            // A row of crabs: plain, flipped, tinted, half transparent, pivot at top-left.
80            let row_y = 80.0;
81            ctx.draw_ex(
82                &crab,
83                vec2(100.0, row_y),
84                DrawParams {
85                    scale: Vec2::splat(2.0),
86                    ..Default::default()
87                },
88            );
89            ctx.draw_ex(
90                &crab,
91                vec2(200.0, row_y),
92                DrawParams {
93                    scale: vec2(-2.0, 2.0),
94                    ..Default::default()
95                },
96            );
97            ctx.draw_ex(
98                &crab,
99                vec2(300.0, row_y),
100                DrawParams {
101                    scale: Vec2::splat(2.0),
102                    color: Color::hex(0x66CCFF),
103                    ..Default::default()
104                },
105            );
106            ctx.draw_ex(
107                &crab,
108                vec2(400.0, row_y),
109                DrawParams {
110                    scale: Vec2::splat(2.0),
111                    color: Color::WHITE.with_alpha(0.4),
112                    ..Default::default()
113                },
114            );
115            ctx.draw_ex(
116                &crab,
117                vec2(500.0, row_y),
118                DrawParams {
119                    scale: Vec2::splat(2.0),
120                    pivot: Vec2::ZERO,
121                    rotation: t,
122                    ..Default::default()
123                },
124            );
125
126            // A small crab following the mouse, on top of everything.
127            ctx.draw_ex(
128                &crab,
129                ctx.mouse_pos(),
130                DrawParams {
131                    z: 2.0,
132                    ..Default::default()
133                },
134            );
135
136            if ctx.key_pressed(Key::Escape) {
137                ctx.quit();
138            }
139        }
140    });
141}