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
//! Opens a window and shows off the game loop and input.
//!
//! Run with: `cargo run --example window`
//!
//! - Arrow keys / WASD: move an (invisible, for now) player; Space prints its position
//! - Click: prints the mouse position
//! - Scroll: changes zoom
//! - Escape: quit
//!
//! Nothing is drawn yet; rendering arrives in M2. Watch the terminal.
use ponko::*;
fn main() {
Game::new("Crabby Engine: window", 800, 600).run(|ctx| {
println!("window is {} x {}", ctx.window_size().x, ctx.window_size().y);
let mut pos = ctx.window_size() / 2.0;
let mut zoom = 1.0_f32;
let mut fixed_ticks = 0_u64;
move |ctx: &mut Ctx| {
// Per-frame movement, scaled by dt so speed is the same at any frame rate.
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;
}
pos += dir.normalize_or_zero() * 200.0 * ctx.dt();
// Fixed-rate logic: runs exactly 60 times per second on average.
for _ in 0..ctx.fixed_steps() {
fixed_ticks += 1;
}
if ctx.key_pressed(Key::Space) {
println!(
"pos = ({:.0}, {:.0}) time = {:.2}s fixed ticks = {} fps = {:.0}",
pos.x,
pos.y,
ctx.time(),
fixed_ticks,
ctx.fps()
);
}
if ctx.mouse_pressed(MouseButton::Left) {
let m = ctx.mouse_pos();
println!("click at ({:.0}, {:.0})", m.x, m.y);
}
if ctx.mouse_wheel().y != 0.0 {
zoom = (zoom * 1.1_f32.powf(ctx.mouse_wheel().y)).clamp(0.1, 10.0);
println!("zoom = {zoom:.2}");
}
if ctx.key_pressed(Key::Escape) {
println!("bye!");
ctx.quit();
}
}
});
}