diff --git a/.gitignore b/.gitignore index 0592392..3c6d3d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target .DS_Store +recordings/* diff --git a/Cargo.lock b/Cargo.lock index 89b5ea6..c017b14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2300,6 +2300,7 @@ name = "spircles" version = "0.1.0" dependencies = [ "nannou", + "once_cell", ] [[package]] diff --git a/crates/spircles/Cargo.toml b/crates/spircles/Cargo.toml index cdc62de..65b6005 100644 --- a/crates/spircles/Cargo.toml +++ b/crates/spircles/Cargo.toml @@ -5,3 +5,4 @@ edition = "2018" [dependencies] nannou = "0.17" +once_cell = "1.8.0" diff --git a/crates/spircles/src/main.rs b/crates/spircles/src/main.rs index 2e94572..927ebcb 100644 --- a/crates/spircles/src/main.rs +++ b/crates/spircles/src/main.rs @@ -53,20 +53,22 @@ const fn bg_color(i: i32, j: i32) -> Rgb { fn view(app: &App, _model: &Model, frame: Frame) { let draw = app.draw(); + let t = frame.nth() as f32 / 60.0; + draw.background().color(BG); for i in -4..=4 { for j in -4..=4 { let speed = map_range((i + j).abs() as f32, 0.0, 8.0, 0.1, 2.0); - let dis = (app.time * speed).sin() * 10.0; + let dis = (t * speed).sin() * 10.0; let circ_offset = vec2( - (app.time + (i * i) as f32).sin() * dis, - (app.time + (j * i) as f32).cos() * dis, + (t + (i * i) as f32).sin() * dis, + (t + (j * i) as f32).cos() * dis, ); - let x = i as f32 * 100.0 * (app.time * 0.7).sin(); - let y = j as f32 * 100.0 * (app.time * 0.7).cos(); + let x = i as f32 * 100.0 * (t * 0.7).sin(); + let y = j as f32 * 100.0 * (t * 0.7).cos(); draw.ellipse() .w_h(40.0, 40.0) @@ -80,4 +82,33 @@ fn view(app: &App, _model: &Model, frame: Frame) { } draw.to_frame(app, &frame).unwrap(); + + record(app, &frame); +} + +use once_cell::sync::Lazy; +static RECORDING: Lazy = Lazy::new(|| { + let args: Vec = std::env::args().collect(); + args.len() > 1 && args[1] == "-record" +}); + +fn record(app: &App, frame: &Frame) { + if !*RECORDING { + return; + } + + // save frame + let path = app + .project_path() + .expect("failed to locate `project_path`") + // Capture all frames to a directory called `//nannou/simple_capture`. + .join("recordings") + .join(app.exe_name().unwrap()) + // Name each file after the number of the frame. + .join(format!("{:03}", frame.nth())) + // The extension will be PNG. We also support tiff, bmp, gif, jpeg, webp and some others. + .with_extension("png"); + + println!("frame: {} {:.3}", frame.nth(), frame.nth() as f32 / 60.0); + app.main_window().capture_frame(path); } diff --git a/record.sh b/record.sh new file mode 100755 index 0000000..43d6dda --- /dev/null +++ b/record.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -e + +if [[ -z $1 ]]; then + echo "plays the thingy and records it" + echo "example:" + echo -e "\t$0 packagename" +else + rm -rf "recordings/$1" + cargo run --release --package $1 -- -record + ffmpeg -framerate 60 -i "recordings/$1/%03d.png" -pix_fmt yuv420p "recordings/$1.mp4" + echo "done" +fi