Update dependencies (#272)

* Update dependencies

- Closes #270

* Unify controls in Invaders example

The fire button on gamepads was allowing trapid fire when holding the
button. Keyboard controls required the fire key to be released between
each shot fired. This commit fixes the difference by making the gamepad
fire button act like the keyboard fire key.
This commit is contained in:
Jay Oster 2022-04-25 05:04:51 -07:00 committed by GitHub
parent 2e7c822091
commit 5c1600926e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 32 additions and 42 deletions

View file

@ -18,4 +18,4 @@ log = "0.4"
pixels = { path = "../.." }
randomize = "3.0"
winit = "0.26"
winit_input_helper = "0.11"
winit_input_helper = "0.12"

View file

@ -66,7 +66,7 @@ fn main() -> Result<(), Error> {
if input.key_pressed(VirtualKeyCode::P) {
paused = !paused;
}
if input.key_pressed(VirtualKeyCode::Space) {
if input.key_pressed_os(VirtualKeyCode::Space) {
// Space is frame-step, so ensure we're paused
paused = true;
}
@ -127,7 +127,7 @@ fn main() -> Result<(), Error> {
if let Some(size) = input.window_resized() {
pixels.resize_surface(size.width, size.height);
}
if !paused || input.key_pressed(VirtualKeyCode::Space) {
if !paused || input.key_pressed_os(VirtualKeyCode::Space) {
life.update();
}
window.request_redraw();

View file

@ -15,4 +15,4 @@ env_logger = "0.9"
log = "0.4"
pixels = { path = "../.." }
winit = "0.26"
winit_input_helper = "0.11"
winit_input_helper = "0.12"

View file

@ -17,4 +17,4 @@ imgui-winit-support = { version = "0.8", default-features = false, features = ["
log = "0.4"
pixels = { path = "../.." }
winit = "0.26"
winit_input_helper = "0.11"
winit_input_helper = "0.12"

View file

@ -12,7 +12,7 @@ default = ["optimize"]
[dependencies]
byteorder = "1.3"
env_logger = "0.9"
game-loop = { version = "0.8", features = ["window"] }
game-loop = { version = "0.9", features = ["window"] }
getrandom = "0.2"
gilrs = "0.8"
log = "0.4"
@ -20,4 +20,4 @@ pixels = { path = "../.." }
randomize = "3.0"
simple-invaders = { path = "simple-invaders" }
winit = "0.26"
winit_input_helper = "0.11"
winit_input_helper = "0.12"

View file

@ -8,10 +8,7 @@ use pixels::{Error, Pixels, SurfaceTexture};
use simple_invaders::{Controls, Direction, World, FPS, HEIGHT, TIME_STEP, WIDTH};
use std::{env, time::Duration};
use winit::{
dpi::LogicalSize,
event::{Event, VirtualKeyCode},
event_loop::EventLoop,
window::WindowBuilder,
dpi::LogicalSize, event::VirtualKeyCode, event_loop::EventLoop, window::WindowBuilder,
};
use winit_input_helper::WinitInputHelper;
@ -31,8 +28,6 @@ struct Game {
gamepad: Option<GamepadId>,
/// Game pause state.
paused: bool,
/// State for key edge detection.
held: [bool; 2],
}
impl Game {
@ -45,14 +40,10 @@ impl Game {
gilrs: Gilrs::new().unwrap(), // XXX: Don't unwrap.
gamepad: None,
paused: false,
held: [false; 2],
}
}
fn update_controls(&mut self, event: &Event<()>) {
// Let winit_input_helper collect events to build its state.
self.input.update(event);
fn update_controls(&mut self) {
// Pump the gilrs event loop and find an active gamepad
while let Some(gilrs::Event { id, event, .. }) = self.gilrs.next_event() {
let pad = self.gilrs.gamepad(id);
@ -67,17 +58,11 @@ impl Game {
self.controls = {
// Keyboard controls
let held = [
self.input.key_held(VirtualKeyCode::Pause),
self.input.key_held(VirtualKeyCode::P),
];
let mut left = self.input.key_held(VirtualKeyCode::Left);
let mut right = self.input.key_held(VirtualKeyCode::Right);
let mut fire = self.input.key_held(VirtualKeyCode::Space);
let mut pause = (held[0] ^ self.held[0] & held[0]) | (held[1] ^ self.held[1] & held[1]);
self.held = held;
let mut fire = self.input.key_pressed(VirtualKeyCode::Space);
let mut pause = self.input.key_pressed(VirtualKeyCode::Pause)
| self.input.key_pressed(VirtualKeyCode::P);
// GamePad controls
if let Some(id) = self.gamepad {
@ -85,7 +70,9 @@ impl Game {
left |= gamepad.is_pressed(Button::DPadLeft);
right |= gamepad.is_pressed(Button::DPadRight);
fire |= gamepad.is_pressed(Button::South);
fire |= gamepad.button_data(Button::South).map_or(false, |button| {
button.is_pressed() && button.counter() == self.gilrs.counter()
});
pause |= gamepad.button_data(Button::Start).map_or(false, |button| {
button.is_pressed() && button.counter() == self.gilrs.counter()
});
@ -166,18 +153,21 @@ fn main() -> Result<(), Error> {
}
},
|g, event| {
// Update controls
g.game.update_controls(&event);
// Let winit_input_helper collect events to build its state.
if g.game.input.update(event) {
// Update controls
g.game.update_controls();
// Close events
if g.game.input.key_pressed(VirtualKeyCode::Escape) || g.game.input.quit() {
g.exit();
return;
}
// Close events
if g.game.input.key_pressed(VirtualKeyCode::Escape) || g.game.input.quit() {
g.exit();
return;
}
// Resize the window
if let Some(size) = g.game.input.window_resized() {
g.game.pixels.resize_surface(size.width, size.height);
// Resize the window
if let Some(size) = g.game.input.window_resized() {
g.game.pixels.resize_surface(size.width, size.height);
}
}
},
);

View file

@ -17,4 +17,4 @@ env_logger = "0.9"
log = "0.4"
pixels = { path = "../.." }
winit = "0.26"
winit_input_helper = "0.11"
winit_input_helper = "0.12"

View file

@ -15,7 +15,7 @@ log = "0.4"
pixels = { path = "../.." }
wgpu = "0.12"
winit = "0.26"
winit_input_helper = "0.11"
winit_input_helper = "0.12"
[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1"

View file

@ -14,4 +14,4 @@ env_logger = "0.9"
log = "0.4"
pixels = { path = "../.." }
winit = "0.26"
winit_input_helper = "0.11"
winit_input_helper = "0.12"

View file

@ -15,7 +15,7 @@ euclid = "0.22"
log = "0.4"
pixels = { path = "../.." }
winit = "0.26"
winit_input_helper = "0.11"
winit_input_helper = "0.12"
[dependencies.raqote]
git = "https://github.com/jrmuizel/raqote.git"