Skip to content

Commit

Permalink
Fix windows issue
Browse files Browse the repository at this point in the history
  • Loading branch information
FreyMo committed Apr 9, 2023
1 parent 90dc9fe commit 8819616
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/game/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ impl GameLoop {
}

#[cfg(target_os = "windows")]
fn idle(&self, difference: Duration) {
fn idle(&self, _: Duration) {
// Do nothing because Windows timers are very inaccurate.
// This may increase CPU load but is much more feasible.
// This may increase CPU load but does stabilize frame times.
}
}
53 changes: 25 additions & 28 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::mpsc::Sender;

use crossterm::event::{Event, KeyCode, KeyModifiers};
use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers};

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Input {
Expand Down Expand Up @@ -28,25 +28,30 @@ impl TryFrom<Event> for Input {

fn try_from(value: Event) -> Result<Self, Self::Error> {
match value {
Event::Key(e) => match e.code {
KeyCode::Up => Ok(Input::Rotate),
KeyCode::Right => Ok(Input::Right),
KeyCode::Left => Ok(Input::Left),
KeyCode::Down => Ok(Input::Down),
KeyCode::Char('d') => Ok(Input::Drop),
KeyCode::Char(' ') => Ok(Input::Drop),
KeyCode::Char('q') => Ok(Input::Quit),
KeyCode::Char('r') => Ok(Input::Restart),
KeyCode::Char('c') => match e.modifiers {
KeyModifiers::CONTROL => Ok(Input::Quit),
Event::Key(e) => {
if e.kind == KeyEventKind::Release {
return Err(());
}
match e.code {
KeyCode::Up => Ok(Input::Rotate),
KeyCode::Right => Ok(Input::Right),
KeyCode::Left => Ok(Input::Left),
KeyCode::Down => Ok(Input::Down),
KeyCode::Char('d') => Ok(Input::Drop),
KeyCode::Char(' ') => Ok(Input::Drop),
KeyCode::Char('q') => Ok(Input::Quit),
KeyCode::Char('r') => Ok(Input::Restart),
KeyCode::Char('c') => match e.modifiers {
KeyModifiers::CONTROL => Ok(Input::Quit),
_ => Err(()),
},
KeyCode::Char(a) => match a {
'0'..='9' => Ok(Input::Number(a.to_digit(10).expect("Should not fail"))),
_ => Err(()),
},
_ => Err(()),
},
KeyCode::Char(a) => match a {
'0'..='9' => Ok(Input::Number(a.to_digit(10).expect("Should not fail"))),
_ => Err(()),
},
_ => Err(()),
},
}
}
_ => Err(()),
}
}
Expand All @@ -58,15 +63,7 @@ impl InputLoop {
}

pub fn run(&self) {
while let Loop::Continue = self.read_inputs() {
#[cfg(target_os = "windows")]
{
// Debounce for Windows since there are multiple Input events if read in a tight loop
use std::{thread, time::Duration};

thread::sleep(Duration::from_millis(20));
}
}
while let Loop::Continue = self.read_inputs() {}
}

fn read_inputs(&self) -> Loop {
Expand Down

0 comments on commit 8819616

Please sign in to comment.