From 0bfe5f1a1210bd76279d9657fe31fc43e229df66 Mon Sep 17 00:00:00 2001 From: Juergen Stuber Date: Tue, 11 Apr 2023 09:55:56 +0200 Subject: [PATCH] Remove explicit event handling to fix resize issue --- src/main.rs | 59 +++++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/src/main.rs b/src/main.rs index d2b3f65..9967cfc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,6 @@ use std::process; use clap::Parser; -use piston::EventLoop; -use piston::EventSettings; -use piston::Events; -use piston::RenderEvent; - use piston_window as pw; use piston_window::PistonWindow; use piston_window::WindowSettings; @@ -70,43 +65,39 @@ fn main() -> Result<(), Box> { [1600, 800], ) .exit_on_esc(true) + .vsync(true) .build()?; - let mut events = Events::new(EventSettings::new().ups(cli.frame_rate)); - let x_size = cli.cols; let y_size = cli.rows; - while let Some(event) = events.next(&mut window) { - if let Some(args) = event.render_args() { - window.draw_2d(&event, |context, graphics, _device| { - // let [vsx, vsy] = context.get_view_size(); - let [vsx, vsy] = args.viewport().window_size; + while let Some(event) = window.next() { + window.draw_2d(&event, |context, graphics, _device| { + let [vsx, vsy] = context.get_view_size(); - // Compute cell size - let csx = vsx / (x_size as f64); - let csy = vsy / (y_size as f64); - let cs = csx.min(csy); + // Compute cell size + let csx = (vsx as f64) / (x_size as f64); + let csy = (vsy as f64) / (y_size as f64); + let cs = csx.min(csy); - pw::clear([0.0, 0.0, 0.0, 1.0], graphics); + pw::clear([0.5, 0.5, 0.5, 1.0], graphics); - stdout.read_exact(&mut buffer).expect("pipe read failed"); - if cli.debug >= 3 { - eprintln!("Received frame {}", frame_count); + stdout.read_exact(&mut buffer).expect("pipe read failed"); + if cli.debug >= 3 { + eprintln!("Received frame {}", frame_count); + } + + for y in 0..y_size { + for x in 0..x_size { + let i = colors * (y * x_size + x); + let r = f32::from(buffer[i + 0]) / 255.0; + let g = f32::from(buffer[i + 1]) / 255.0; + let b = f32::from(buffer[i + 2]) / 255.0; + let color = [r, g, b, 1.0]; + let rectangle = [(x as f64) * cs, (y as f64) * cs, cs, cs]; + pw::rectangle(color, rectangle, context.transform, graphics); } - - for y in 0..y_size { - for x in 0..x_size { - let i = colors * (y * x_size + x); - let r = f32::from(buffer[i + 0]) / 255.0; - let g = f32::from(buffer[i + 1]) / 255.0; - let b = f32::from(buffer[i + 2]) / 255.0; - let color = [r, g, b, 1.0]; - let rectangle = [(x as f64) * cs, (y as f64) * cs, cs, cs]; - pw::rectangle(color, rectangle, context.transform, graphics); - } - } - }); - } + } + }); frame_count += 1; } child.kill()?;