-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add mini animation recorder example
- Loading branch information
1 parent
be2ba23
commit fe04e7b
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Arc, RwLock | ||
}; | ||
|
||
use console::Term; | ||
use serde::Serialize; | ||
Check failure on line 7 in examples/recorder.rs GitHub Actions / Build and test (ubuntu-latest, stable)
|
||
use tokio_stream::StreamExt; | ||
use vmc::{VMCMessage, VMCResult}; | ||
|
||
#[derive(Default, Serialize)] | ||
Check failure on line 11 in examples/recorder.rs GitHub Actions / Build and test (ubuntu-latest, stable)
|
||
struct MessageBundle { | ||
time_delta: f32, | ||
messages: Vec<VMCMessage> | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> VMCResult<()> { | ||
let mut socket = vmc::marionette!("127.0.0.1:39539").await?; | ||
|
||
tokio::spawn(async move { | ||
tokio::signal::ctrl_c().await.unwrap(); | ||
std::process::exit(0); | ||
}); | ||
|
||
let packet_buffer = Arc::new(RwLock::new(Vec::new())); | ||
let mut current_packet = MessageBundle::default(); | ||
let active = Arc::new(AtomicBool::new(false)); | ||
|
||
let _packet_buffer = Arc::clone(&packet_buffer); | ||
let _active = Arc::clone(&active); | ||
std::thread::spawn(move || { | ||
let term = Term::stdout(); | ||
while term.read_char().is_ok() { | ||
let active = _active.load(Ordering::Relaxed); | ||
if active { | ||
let mut packet_buffer = _packet_buffer.write().unwrap(); | ||
let buf = &packet_buffer[1..]; | ||
std::fs::write("out.vmc", rmp_serde::to_vec(buf).unwrap()).unwrap(); | ||
Check failure on line 39 in examples/recorder.rs GitHub Actions / Build and test (ubuntu-latest, stable)
|
||
packet_buffer.clear(); | ||
println!("Stopped"); | ||
} else { | ||
println!("Started"); | ||
} | ||
_active.store(!active, Ordering::Relaxed); | ||
} | ||
}); | ||
|
||
while let Some(packet) = socket.next().await { | ||
let (packet, _) = packet?; | ||
for message in vmc::parse(packet)? { | ||
if active.load(Ordering::Relaxed) { | ||
match message { | ||
VMCMessage::Time(t) => { | ||
{ | ||
let mut packet_buffer = packet_buffer.write().unwrap(); | ||
packet_buffer.push(current_packet); | ||
} | ||
current_packet = MessageBundle::default(); | ||
|
||
current_packet.time_delta = t.0; | ||
} | ||
message => current_packet.messages.push(message) | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |