add depth streaming feature

main
_ 2022-01-09 18:02:07 +00:00
parent 77ed8db317
commit fc69662060
3 changed files with 51 additions and 10 deletions

9
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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 <anyhow::Result <()>> = 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 <anyhow::Result <()>> = 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 <u8> = depth_buffer.iter ()
let depth_bytes: Vec <u8> = 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;