extract handle_package function, make window resizable as content gets scaled

This commit is contained in:
Vinzenz Schroeter 2024-05-09 13:10:08 +02:00
parent 64e85bcd4f
commit 1584a2685e

View file

@ -102,7 +102,7 @@ fn main() {
let mut buf = [0; 8985];
while rx.try_recv().is_err() {
let (amount, source) = match socket.recv_from(&mut buf) {
let (amount, _) = match socket.recv_from(&mut buf) {
Err(err) if err.kind() == ErrorKind::WouldBlock => {
thread::sleep(Duration::from_millis(1));
continue;
@ -110,20 +110,35 @@ fn main() {
Ok(result) => result,
other => other.unwrap(),
};
let received = &mut buf[..amount];
handle_package(&mut buf[..amount]);
}
});
let event_loop = EventLoop::new().expect("could not create event loop");
event_loop.set_control_flow(ControlFlow::Poll);
let mut app = App::default();
event_loop
.run_app(&mut app)
.expect("could not run event loop");
tx.send(()).expect("could not cancel thread");
thread.join().expect("could not join threads");
}
fn handle_package(received: &mut [u8]) {
let header = read_hdr_window(&received[..10]);
if let Err(err) = header {
warn!("could not read header: {}", err);
continue;
return;
}
let header = header.unwrap();
let payload = &received[10..];
info!(
"received from {:?}: {:?} (and {} bytes of payload)",
source,
"received from {:?} (and {} bytes of payload)",
header,
payload.len()
);
@ -143,25 +158,9 @@ fn main() {
print_cp437_data(&header, payload);
}
_ => {
error!(
"command {:?} sent by {:?} not implemented yet",
header.command, source
);
error!("command {:?} not implemented yet", header.command);
}
}
}
});
let event_loop = EventLoop::new().expect("could not create event loop");
event_loop.set_control_flow(ControlFlow::Poll);
let mut app = App::default();
event_loop
.run_app(&mut app)
.expect("could not run event loop");
tx.send(()).expect("could not cancel thread");
thread.join().expect("could not join threads");
}
#[derive(Default)]
@ -175,9 +174,7 @@ impl ApplicationHandler for App {
let size = Size::from(LogicalSize::new(PIXEL_WIDTH as f64, PIXEL_HEIGHT as f64));
let attributes = Window::default_attributes()
.with_title("pixel-receiver-rs")
.with_inner_size(size)
.with_resizable(false)
.with_visible(true);
.with_inner_size(size);
let window = event_loop.create_window(attributes).unwrap();
self.window = Some(window);