Clean up event handling with winit_input_helper (#21)
* Clean up event handling with winit_input_helper - Closes #20 * QoL improvements
This commit is contained in:
parent
a7802453ec
commit
b18690f19f
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -709,6 +709,7 @@ dependencies = [
|
||||||
"simple-invaders 0.1.0",
|
"simple-invaders 0.1.0",
|
||||||
"wgpu 0.3.0 (git+https://github.com/gfx-rs/wgpu-rs?rev=012d08d64de924da93289c2b51fb06b22d7348cf)",
|
"wgpu 0.3.0 (git+https://github.com/gfx-rs/wgpu-rs?rev=012d08d64de924da93289c2b51fb06b22d7348cf)",
|
||||||
"winit 0.20.0-alpha4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"winit 0.20.0-alpha4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"winit_input_helper 0.4.0-alpha4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1273,6 +1274,14 @@ dependencies = [
|
||||||
"x11-dl 2.18.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"x11-dl 2.18.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winit_input_helper"
|
||||||
|
version = "0.4.0-alpha4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"winit 0.20.0-alpha4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wio"
|
name = "wio"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
|
@ -1484,6 +1493,7 @@ dependencies = [
|
||||||
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
"checksum wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96f5016b18804d24db43cebf3c77269e7569b8954a8464501c216cc5e070eaa9"
|
"checksum wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96f5016b18804d24db43cebf3c77269e7569b8954a8464501c216cc5e070eaa9"
|
||||||
"checksum winit 0.20.0-alpha4 (registry+https://github.com/rust-lang/crates.io-index)" = "56c565622ccb05351d92445415952ca09dade6a53e75dd9e75d9bd35d4e99333"
|
"checksum winit 0.20.0-alpha4 (registry+https://github.com/rust-lang/crates.io-index)" = "56c565622ccb05351d92445415952ca09dade6a53e75dd9e75d9bd35d4e99333"
|
||||||
|
"checksum winit_input_helper 0.4.0-alpha4 (registry+https://github.com/rust-lang/crates.io-index)" = "48df19ac64492fa604aeb2269afb41f21c19a2bdc552134bec1c3751d6c0919d"
|
||||||
"checksum wio 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5d129932f4644ac2396cb456385cbf9e63b5b30c6e8dc4820bdca4eb082037a5"
|
"checksum wio 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5d129932f4644ac2396cb456385cbf9e63b5b30c6e8dc4820bdca4eb082037a5"
|
||||||
"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
|
"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
|
||||||
"checksum x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39697e3123f715483d311b5826e254b6f3cfebdd83cf7ef3358f579c3d68e235"
|
"checksum x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39697e3123f715483d311b5826e254b6f3cfebdd83cf7ef3358f579c3d68e235"
|
||||||
|
|
|
@ -20,6 +20,7 @@ log = { version = "0.4", features = ["release_max_level_warn"] }
|
||||||
pixels-mocks = { path = "pixels-mocks" }
|
pixels-mocks = { path = "pixels-mocks" }
|
||||||
simple-invaders = { path = "simple-invaders" }
|
simple-invaders = { path = "simple-invaders" }
|
||||||
winit = "0.20.0-alpha4"
|
winit = "0.20.0-alpha4"
|
||||||
|
winit_input_helper = "0.4.0-alpha4"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
|
|
|
@ -3,12 +3,14 @@ use std::time::Instant;
|
||||||
|
|
||||||
use pixels::{Error, Pixels, SurfaceTexture};
|
use pixels::{Error, Pixels, SurfaceTexture};
|
||||||
use simple_invaders::{Controls, Direction, World, SCREEN_HEIGHT, SCREEN_WIDTH};
|
use simple_invaders::{Controls, Direction, World, SCREEN_HEIGHT, SCREEN_WIDTH};
|
||||||
use winit::event;
|
use winit::event::{Event, VirtualKeyCode, WindowEvent};
|
||||||
use winit::event_loop::{ControlFlow, EventLoop};
|
use winit::event_loop::{ControlFlow, EventLoop};
|
||||||
|
use winit_input_helper::WinitInputHelper;
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new();
|
||||||
|
let mut input = WinitInputHelper::new();
|
||||||
|
|
||||||
// Enable debug mode with `DEBUG=true` environment variable
|
// Enable debug mode with `DEBUG=true` environment variable
|
||||||
let debug = env::var("DEBUG")
|
let debug = env::var("DEBUG")
|
||||||
|
@ -40,94 +42,64 @@ fn main() -> Result<(), Error> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let surface_texture = SurfaceTexture::new(width, height, surface);
|
let surface_texture = SurfaceTexture::new(width, height, surface);
|
||||||
let mut fb = Pixels::new(224, 256, surface_texture)?;
|
let mut pixels = Pixels::new(224, 256, surface_texture)?;
|
||||||
let mut invaders = World::new(debug);
|
let mut invaders = World::new(debug);
|
||||||
let mut last = Instant::now();
|
let mut time = Instant::now();
|
||||||
|
|
||||||
let mut controls = Controls::default();
|
let mut controls = Controls::default();
|
||||||
let mut last_state = false;
|
|
||||||
let mut button_state = false;
|
|
||||||
let mut rising_edge = false;
|
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| match event {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
event::Event::WindowEvent { event, .. } => match event {
|
// The one and only event that winit_input_helper doesn't have for us...
|
||||||
// Close events
|
match event {
|
||||||
event::WindowEvent::KeyboardInput {
|
Event::WindowEvent {
|
||||||
input:
|
event: WindowEvent::RedrawRequested,
|
||||||
event::KeyboardInput {
|
|
||||||
virtual_keycode: Some(event::VirtualKeyCode::Escape),
|
|
||||||
state: event::ElementState::Pressed,
|
|
||||||
..
|
|
||||||
},
|
|
||||||
..
|
..
|
||||||
|
} => {
|
||||||
|
pixels.render(invaders.draw());
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
// For everything else, for let winit_input_helper collect events to build its state.
|
||||||
|
// It returns `true` when it is time to update our game state and request a redraw.
|
||||||
|
if input.update(event) {
|
||||||
|
// Close events
|
||||||
|
if input.key_pressed(VirtualKeyCode::Escape) || input.quit() {
|
||||||
|
*control_flow = ControlFlow::Exit;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
| event::WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
|
||||||
|
|
||||||
// Keyboard controls
|
// Keyboard controls
|
||||||
event::WindowEvent::KeyboardInput {
|
controls.direction = if input.key_held(VirtualKeyCode::Left) {
|
||||||
input:
|
Direction::Left
|
||||||
event::KeyboardInput {
|
} else if input.key_held(VirtualKeyCode::Right) {
|
||||||
virtual_keycode: Some(virtual_code),
|
Direction::Right
|
||||||
state: event::ElementState::Pressed,
|
} else {
|
||||||
..
|
Direction::Still
|
||||||
},
|
};
|
||||||
..
|
controls.fire = input.key_pressed(VirtualKeyCode::Space);
|
||||||
} => match virtual_code {
|
|
||||||
event::VirtualKeyCode::Left => controls.direction = Direction::Left,
|
|
||||||
event::VirtualKeyCode::Right => controls.direction = Direction::Right,
|
|
||||||
event::VirtualKeyCode::Space => button_state = true,
|
|
||||||
_ => (),
|
|
||||||
},
|
|
||||||
|
|
||||||
event::WindowEvent::KeyboardInput {
|
|
||||||
input:
|
|
||||||
event::KeyboardInput {
|
|
||||||
virtual_keycode: Some(virtual_code),
|
|
||||||
state: event::ElementState::Released,
|
|
||||||
..
|
|
||||||
},
|
|
||||||
..
|
|
||||||
} => match virtual_code {
|
|
||||||
event::VirtualKeyCode::Left => controls.direction = Direction::Still,
|
|
||||||
event::VirtualKeyCode::Right => controls.direction = Direction::Still,
|
|
||||||
event::VirtualKeyCode::Space => button_state = false,
|
|
||||||
_ => (),
|
|
||||||
},
|
|
||||||
|
|
||||||
// Adjust high DPI factor
|
// Adjust high DPI factor
|
||||||
event::WindowEvent::HiDpiFactorChanged(factor) => hidpi_factor = factor,
|
if let Some(factor) = input.hidpi_changed() {
|
||||||
|
hidpi_factor = factor;
|
||||||
|
}
|
||||||
|
|
||||||
// Resize the window
|
// Resize the window
|
||||||
event::WindowEvent::Resized(size) => {
|
if let Some(size) = input.window_resized() {
|
||||||
let size = size.to_physical(hidpi_factor);
|
let size = size.to_physical(hidpi_factor);
|
||||||
let width = size.width.round() as u32;
|
let width = size.width.round() as u32;
|
||||||
let height = size.height.round() as u32;
|
let height = size.height.round() as u32;
|
||||||
|
|
||||||
fb.resize(width, height);
|
pixels.resize(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redraw the screen
|
|
||||||
event::WindowEvent::RedrawRequested => fb.render(invaders.draw()),
|
|
||||||
|
|
||||||
_ => (),
|
|
||||||
},
|
|
||||||
event::Event::EventsCleared => {
|
|
||||||
// Get a new delta time.
|
// Get a new delta time.
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let dt = now.duration_since(last);
|
let dt = now.duration_since(time);
|
||||||
last = now;
|
time = now;
|
||||||
|
|
||||||
// Compute rising edge based on current and last button states
|
|
||||||
rising_edge = button_state && !last_state;
|
|
||||||
last_state = button_state;
|
|
||||||
|
|
||||||
// Fire button only uses rising edge
|
|
||||||
controls.fire = rising_edge;
|
|
||||||
|
|
||||||
// Update the game logic and request redraw
|
// Update the game logic and request redraw
|
||||||
invaders.update(&dt, &controls);
|
invaders.update(&dt, &controls);
|
||||||
window.request_redraw();
|
window.request_redraw();
|
||||||
}
|
}
|
||||||
_ => (),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue