Skip to content

Commit

Permalink
examples: add mini animation recorder example
Browse files Browse the repository at this point in the history
  • Loading branch information
decahedron1 committed Dec 21, 2023
1 parent be2ba23 commit fe04e7b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ tokio-stream = "0.1"
thiserror = "1.0"

[dev-dependencies]
tokio = { version = "1.30", features = [ "net", "macros", "rt-multi-thread" ] }
tokio = { version = "1.30", features = [ "net", "macros", "signal", "rt-multi-thread" ] }
tokio-test = "0.4"
approx = "0.5"
rmp-serde = "1.1"
console = "0.15"
70 changes: 70 additions & 0 deletions examples/recorder.rs
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

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, stable)

unresolved import `serde`

Check failure on line 7 in examples/recorder.rs

View workflow job for this annotation

GitHub Actions / Build and test (macOS-latest, stable)

unresolved import `serde`
use tokio_stream::StreamExt;
use vmc::{VMCMessage, VMCResult};

#[derive(Default, Serialize)]

Check failure on line 11 in examples/recorder.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, stable)

cannot determine resolution for the derive macro `Serialize`

Check failure on line 11 in examples/recorder.rs

View workflow job for this annotation

GitHub Actions / Build and test (macOS-latest, stable)

cannot determine resolution for the derive macro `Serialize`
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

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, stable)

the trait bound `MessageBundle: serde::ser::Serialize` is not satisfied

Check failure on line 39 in examples/recorder.rs

View workflow job for this annotation

GitHub Actions / Build and test (macOS-latest, stable)

the trait bound `MessageBundle: serde::ser::Serialize` is not satisfied
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(())
}

0 comments on commit fe04e7b

Please sign in to comment.