From fc696620605cf4dc65f6a286c8fbc5c9545d61f9 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Sun, 9 Jan 2022 18:02:07 +0000 Subject: [PATCH] add depth streaming feature --- Cargo.lock | 9 +++---- Cargo.toml | 2 +- src/bin/platformer/main.rs | 50 +++++++++++++++++++++++++++++++++++--- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe3614c..0215084 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1327,11 +1327,10 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.5.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83f0c8e7c0addab50b663055baf787d0af7f413a46e6e7fb9559a4e4db7137a5" +checksum = "fbbf1c778ec206785635ce8ad57fe52b3009ae9e0c9f574a728f3049d3e55838" dependencies = [ - "autocfg 1.0.1", "bytes", "libc", "memchr", @@ -1347,9 +1346,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "1.1.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "caf7b11a536f46a809a8a9f0bb4237020f70ecbf115b842360afb127ea2fda57" +checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index aacd39f..97fb4a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ rmp-serde = "0.15.4" serde = { version = "1.0.125", features = ["derive"] } serde_json = "1.0.73" sdl2 = "0.32.2" -tokio = { version = "1.5.0", features = ["full"] } +tokio = { version = "1.15.0", features = ["full"] } tracing = "0.1.22" tracing-subscriber = "0.2.15" diff --git a/src/bin/platformer/main.rs b/src/bin/platformer/main.rs index 7953857..6664829 100644 --- a/src/bin/platformer/main.rs +++ b/src/bin/platformer/main.rs @@ -1,8 +1,15 @@ use std::{ + sync::Arc, time::Instant, }; use anyhow::Result; +use tokio::{ + io::AsyncWriteExt, + net::TcpListener, + sync::watch, + task::JoinHandle, +}; use opengl_rust::{ prelude::*, @@ -72,7 +79,32 @@ async fn main () -> Result <()> { let mut next_upf_print = 60; let mut last_upf_instant = Instant::now (); - let mut depth_buffer = vec! [0u32; 320 * 2 * 240 * 2]; + let depth_buffer = vec! [0u8; 320 * 2 * 240 * 2 * 4]; + let depth_buffer = Arc::new (depth_buffer); + let (depth_tx, depth_rx) = watch::channel (depth_buffer); + let depth_rx_2 = depth_rx.clone (); + + let _: JoinHandle > = tokio::spawn (async move { + let tcp_listener = TcpListener::bind ("127.0.0.1:0").await?; + tracing::info! ("Listening for TCP on {:?}", tcp_listener.local_addr ()); + + loop { + let (socket, _) = tcp_listener.accept ().await?; + + let mut depth_rx_3 = depth_rx_2.clone (); + + let _: JoinHandle > = tokio::spawn (async move { + let (_rx, mut tx) = socket.into_split (); + + loop { + depth_rx_3.changed ().await?; + let depth_buffer = Arc::clone (&depth_rx_3.borrow_and_update ()); + + tx.write_all (&depth_buffer [..]).await?; + } + }); + } + }); 'running: loop { let frames_to_do = time_step.step (); @@ -91,7 +123,9 @@ async fn main () -> Result <()> { game_state.logic.reset_level (&level); }, Event::KeyDown { keycode: Some (Keycode::F12), .. } => { - let depth_bytes: Vec = depth_buffer.iter () + let depth_bytes: Vec = depth_rx.borrow () + .chunks_exact (4) + .map (|x| u32::from_le_bytes ([x [0], x [1], x [2], x [3]])) .map (|x| (x >> 16) as u16) .map (|x| x.to_le_bytes ()) .flatten () @@ -174,8 +208,16 @@ async fn main () -> Result <()> { window.gl_swap_window (); - unsafe { - gl::ReadPixels (0, 0, 320 * 2, 240 * 2, gl::DEPTH_COMPONENT, gl::UNSIGNED_INT, &mut depth_buffer [0] as *mut u32 as *mut std::ffi::c_void); + { + let mut depth_buffer = vec! [0u8; 320 * 2 * 240 * 2 * 4]; + + unsafe { + gl::ReadPixels (0, 0, 320 * 2, 240 * 2, gl::DEPTH_COMPONENT, gl::UNSIGNED_INT, &mut depth_buffer [0] as *mut u8 as *mut std::ffi::c_void); + } + + let depth_buffer = Arc::new (depth_buffer); + // Shouldn't fail, because we always keep one receiver open ourselves + depth_tx.send (depth_buffer)?; } tokio::time::sleep (Duration::from_millis (10)).await;