Skip to content

Commit

Permalink
Update dependencies (#272)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
parasyte authored Apr 25, 2022
1 parent 2e7c822 commit 5c16009
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 44 deletions.
2 changes: 1 addition & 1 deletion examples/conway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions examples/conway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion examples/imgui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions examples/invaders/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ 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"
pixels = { path = "../.." }
randomize = "3.0"
simple-invaders = { path = "simple-invaders" }
winit = "0.26"
winit_input_helper = "0.11"
winit_input_helper = "0.12"
56 changes: 23 additions & 33 deletions examples/invaders/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,8 +28,6 @@ struct Game {
gamepad: Option<GamepadId>,
/// Game pause state.
paused: bool,
/// State for key edge detection.
held: [bool; 2],
}

impl Game {
Expand All @@ -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);
Expand All @@ -67,25 +58,21 @@ 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 {
let gamepad = self.gilrs.gamepad(id);

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()
});
Expand Down Expand Up @@ -166,18 +153,21 @@ fn main() -> Result<(), Error> {
}
},
|g, event| {
// Update controls
g.game.update_controls(&event);

// 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);
// 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;
}

// Resize the window
if let Some(size) = g.game.input.window_resized() {
g.game.pixels.resize_surface(size.width, size.height);
}
}
},
);
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal-egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion examples/minimal-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion examples/raqote-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5c16009

Please sign in to comment.