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