diff --git a/turtle-lib-macros/src/lib.rs b/turtle-lib-macros/src/lib.rs index e1086fe..c630ea7 100644 --- a/turtle-lib-macros/src/lib.rs +++ b/turtle-lib-macros/src/lib.rs @@ -123,7 +123,7 @@ pub fn turtle_main(args: TokenStream, input: TokenStream) -> TokenStream { quote! { #[macroquad::main(#window_title)] async fn main() { - // Parse command-line arguments for SVG export + // Parse command-line arguments for SVG export FIRST (before any graphics init) let args: Vec = std::env::args().collect(); let mut export_svg_path: Option = None; @@ -136,22 +136,21 @@ pub fn turtle_main(args: TokenStream, input: TokenStream) -> TokenStream { i += 1; } - let mut turtle = turtle_lib::create_turtle_plan(); - - // Call the user's function with the turtle - #fn_name(&mut turtle); - - let mut app = turtle_lib::TurtleApp::new() - .with_commands(turtle.build()); - - // Handle SVG export if requested + // Handle SVG export mode (execute instantly without rendering) if let Some(filename) = export_svg_path { #[cfg(feature = "svg")] { + let mut turtle = turtle_lib::create_turtle_plan(); + #fn_name(&mut turtle); + + // Create app and execute instantly + let mut app = turtle_lib::TurtleApp::new() + .with_commands(turtle.build()); + // Set instant speed to execute all commands immediately app.set_all_turtles_speed(turtle_lib::AnimationSpeed::Instant(1000)); - // Execute all commands instantly + // Execute all commands instantly (no rendering needed) while !app.all_animations_complete() { app.update(); } @@ -176,7 +175,15 @@ pub fn turtle_main(args: TokenStream, input: TokenStream) -> TokenStream { } } - // Normal rendering loop + // Normal rendering mode (with window) + let mut turtle = turtle_lib::create_turtle_plan(); + + // Call the user's function with the turtle + #fn_name(&mut turtle); + + let mut app = turtle_lib::TurtleApp::new() + .with_commands(turtle.build()); + loop { macroquad::prelude::clear_background(macroquad::prelude::WHITE); app.update(); @@ -206,7 +213,7 @@ pub fn turtle_main(args: TokenStream, input: TokenStream) -> TokenStream { quote! { #[macroquad::main(#window_title)] async fn main() { - // Parse command-line arguments for SVG export + // Parse command-line arguments for SVG export FIRST (before any graphics init) let args: Vec = std::env::args().collect(); let mut export_svg_path: Option = None; @@ -219,22 +226,21 @@ pub fn turtle_main(args: TokenStream, input: TokenStream) -> TokenStream { i += 1; } - let mut turtle = turtle_lib::create_turtle_plan(); - - // Inline the user's code - #fn_block - - let mut app = turtle_lib::TurtleApp::new() - .with_commands(turtle.build()); - - // Handle SVG export if requested + // Handle SVG export mode (execute instantly without rendering) if let Some(filename) = export_svg_path { #[cfg(feature = "svg")] { + let mut turtle = turtle_lib::create_turtle_plan(); + #fn_block + + // Create app and execute instantly + let mut app = turtle_lib::TurtleApp::new() + .with_commands(turtle.build()); + // Set instant speed to execute all commands immediately app.set_all_turtles_speed(turtle_lib::AnimationSpeed::Instant(1000)); - // Execute all commands instantly + // Execute all commands instantly (no rendering needed) while !app.all_animations_complete() { app.update(); } @@ -259,7 +265,13 @@ pub fn turtle_main(args: TokenStream, input: TokenStream) -> TokenStream { } } - // Normal rendering loop + // Normal rendering mode (with window) + let mut turtle = turtle_lib::create_turtle_plan(); + #fn_block + + let mut app = turtle_lib::TurtleApp::new() + .with_commands(turtle.build()); + loop { macroquad::prelude::clear_background(macroquad::prelude::WHITE); app.update(); diff --git a/turtle-lib/examples/test_svg_export.rs b/turtle-lib/examples/test_svg_export.rs new file mode 100644 index 0000000..dbeba35 --- /dev/null +++ b/turtle-lib/examples/test_svg_export.rs @@ -0,0 +1,36 @@ +//! Test example for CLI SVG export feature +//! +//! Run this with: cargo run --package turtle-lib --example test_svg_export --features svg -- --export-svg test_output.svg + +use turtle_lib::*; + +#[turtle_main("SVG Export Test")] +fn draw_test(turtle: &mut TurtlePlan) { + turtle.set_pen_color(RED); + turtle.set_pen_width(3.0); + + // Draw a square + for _ in 0..4 { + turtle.forward(100.0); + turtle.right(90.0); + } + + // Draw a circle + turtle.set_pen_color(BLUE); + turtle.pen_up(); + turtle.forward(150.0); + turtle.pen_down(); + turtle.circle_left(50.0, 360.0, 36); + + // Draw a filled triangle + turtle.set_fill_color(GREEN); + turtle.pen_up(); + turtle.go_to(vec2(-50.0, 100.0)); + turtle.pen_down(); + turtle.begin_fill(); + for _ in 0..3 { + turtle.forward(80.0); + turtle.right(120.0); + } + turtle.end_fill(); +}