fix: add sleep to game loop, panic hook restores terminal
- 16ms sleep per frame prevents 100% CPU spin - Panic hook restores terminal (disable raw mode, leave alternate screen) before printing panic message, so errors are visible - Remove debug eprintlns
This commit is contained in:
+5
-1
@@ -196,6 +196,8 @@ impl Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.handle_input(vw, vh);
|
self.handle_input(vw, vh);
|
||||||
|
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(16));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = renderer.shutdown() {
|
if let Err(e) = renderer.shutdown() {
|
||||||
@@ -206,7 +208,9 @@ impl Game {
|
|||||||
pub fn handle_input(&mut self, vw: usize, vh: usize) {
|
pub fn handle_input(&mut self, vw: usize, vh: usize) {
|
||||||
let action = self.input.poll();
|
let action = self.input.poll();
|
||||||
match action {
|
match action {
|
||||||
Action::Quit => self.running = false,
|
Action::Quit => {
|
||||||
|
self.running = false;
|
||||||
|
}
|
||||||
Action::MoveLeft => self.player.move_left(&mut self.entities),
|
Action::MoveLeft => self.player.move_left(&mut self.entities),
|
||||||
Action::MoveRight => self.player.move_right(&mut self.entities),
|
Action::MoveRight => self.player.move_right(&mut self.entities),
|
||||||
Action::Jump => {
|
Action::Jump => {
|
||||||
|
|||||||
+11
@@ -28,6 +28,17 @@ fn main() {
|
|||||||
|
|
||||||
match cli.mode.as_str() {
|
match cli.mode.as_str() {
|
||||||
"terminal" => {
|
"terminal" => {
|
||||||
|
std::panic::set_hook(Box::new(|info| {
|
||||||
|
let _ = crossterm::terminal::disable_raw_mode();
|
||||||
|
let _ = crossterm::execute!(
|
||||||
|
std::io::stdout(),
|
||||||
|
crossterm::style::ResetColor,
|
||||||
|
crossterm::cursor::Show,
|
||||||
|
crossterm::terminal::LeaveAlternateScreen,
|
||||||
|
crossterm::event::DisableMouseCapture,
|
||||||
|
);
|
||||||
|
eprintln!("PANIC: {}", info);
|
||||||
|
}));
|
||||||
let mut renderer = TerminalRenderer::new();
|
let mut renderer = TerminalRenderer::new();
|
||||||
let mut game = Game::new();
|
let mut game = Game::new();
|
||||||
game.run(&mut renderer);
|
game.run(&mut renderer);
|
||||||
|
|||||||
Reference in New Issue
Block a user