From f4a2fc616b2fcced1370d95d3a984c464177b944 Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Fri, 28 Apr 2023 18:19:02 -0700 Subject: [PATCH] Mmark example This commit ports the mmark example from the mmark branch, and also makes the complexity adjustable through up/down arrow keys. --- examples/scenes/Cargo.toml | 1 + examples/scenes/src/lib.rs | 14 +++++++++++++- examples/scenes/src/test_scenes.rs | 8 ++++++++ examples/with_winit/src/lib.rs | 6 +++++- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/examples/scenes/Cargo.toml b/examples/scenes/Cargo.toml index c68ffa3..fd5e641 100644 --- a/examples/scenes/Cargo.toml +++ b/examples/scenes/Cargo.toml @@ -16,6 +16,7 @@ vello_svg = { path = "../../integrations/vello_svg" } anyhow = { workspace = true } clap = { workspace = true, features = ["derive"] } image = "0.24.5" +rand = "0.8.5" instant = { workspace = true } # Used for the `download` command diff --git a/examples/scenes/src/lib.rs b/examples/scenes/src/lib.rs index c85573c..7fee5cd 100644 --- a/examples/scenes/src/lib.rs +++ b/examples/scenes/src/lib.rs @@ -1,5 +1,6 @@ pub mod download; mod images; +mod mmark; mod simple_text; mod svg; mod test_scenes; @@ -25,6 +26,7 @@ pub struct SceneParams<'a> { pub images: &'a mut ImageCache, pub resolution: Option, pub base_color: Option, + pub complexity: usize, } pub struct SceneConfig { @@ -34,10 +36,20 @@ pub struct SceneConfig { } pub struct ExampleScene { - pub function: Box, + pub function: Box, pub config: SceneConfig, } +pub trait TestScene { + fn render(&mut self, sb: &mut SceneBuilder, params: &mut SceneParams); +} + +impl TestScene for F { + fn render(&mut self, sb: &mut SceneBuilder, params: &mut SceneParams) { + self(sb, params); + } +} + pub struct SceneSet { pub scenes: Vec, } diff --git a/examples/scenes/src/test_scenes.rs b/examples/scenes/src/test_scenes.rs index 53f99ba..4fbabba 100644 --- a/examples/scenes/src/test_scenes.rs +++ b/examples/scenes/src/test_scenes.rs @@ -31,8 +31,16 @@ pub fn test_scenes() -> SceneSet { }, function: Box::new(splash_with_tiger()), }; + let mmark_scene = ExampleScene { + config: SceneConfig { + animated: false, + name: "mmark".to_owned(), + }, + function: Box::new(crate::mmark::MMark::new(80_000)), + }; let scenes = vec![ splash_scene, + mmark_scene, scene!(funky_paths), scene!(cardioid_and_friends), scene!(animated_text: animated), diff --git a/examples/with_winit/src/lib.rs b/examples/with_winit/src/lib.rs index ce95e82..5b897b5 100644 --- a/examples/with_winit/src/lib.rs +++ b/examples/with_winit/src/lib.rs @@ -113,6 +113,7 @@ fn run( let mut prior_position: Option = None; // We allow looping left and right through the scenes, so use a signed index let mut scene_ix: i32 = 0; + let mut complexity: usize = 0; if let Some(set_scene) = args.scene { scene_ix = set_scene; } @@ -134,6 +135,8 @@ fn run( match input.virtual_keycode { Some(VirtualKeyCode::Left) => scene_ix = scene_ix.saturating_sub(1), Some(VirtualKeyCode::Right) => scene_ix = scene_ix.saturating_add(1), + Some(VirtualKeyCode::Up) => complexity += 1, + Some(VirtualKeyCode::Down) => complexity = complexity.saturating_sub(1), Some(key @ VirtualKeyCode::Q) | Some(key @ VirtualKeyCode::E) => { if let Some(prior_position) = prior_position { let is_clockwise = key == VirtualKeyCode::E; @@ -295,8 +298,9 @@ fn run( resolution: None, base_color: None, interactive: true, + complexity, }; - (example_scene.function)(&mut builder, &mut scene_params); + example_scene.function.render(&mut builder, &mut scene_params); // If the user specifies a base color in the CLI we use that. Otherwise we use any // color specified by the scene. The default is black.