From 58fb346b2584a4cd4d01d80d3403f7629b6c7ede Mon Sep 17 00:00:00 2001 From: numzero Date: Sat, 6 Sep 2025 03:36:19 +0300 Subject: [PATCH] now with the main loop --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 74 +++++++++++++++++++++++++++++++---------------------- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 92492f8..e7dadce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -885,6 +885,7 @@ name = "gears" version = "0.1.0" dependencies = [ "flo_draw", + "futures", "glam", ] diff --git a/Cargo.toml b/Cargo.toml index d4e3839..a870244 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ edition = "2024" [dependencies] flo_draw = "0.3.1" +futures = "0.3.31" glam = "0.30.5" diff --git a/src/main.rs b/src/main.rs index b50b009..9c2adba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,14 @@ -use std::f32::consts::PI; +use std::{ + f32::consts::PI, + pin::pin, + task::{Context, Poll, Waker}, +}; use flo_draw::{ canvas::{Color::Rgba, GraphicsContext, GraphicsPrimitives}, - create_drawing_window, with_2d_graphics, + create_drawing_window, create_drawing_window_with_events, with_2d_graphics, }; +use futures::{StreamExt, executor, pin_mut}; use glam::{FloatExt, Mat2, Vec2, vec2}; const fn rad(deg: f32) -> f32 { @@ -92,42 +97,49 @@ fn main() { let sat = gear_shape(N_TEETH_SAT); let crown = gear_shape_outer(N_TEETH_SKY); - let canvas = create_drawing_window("Canvas window"); - canvas.draw(|gc| { - gc.clear_canvas(Rgba(0.1, 0.1, 0.1, 1.0)); - - gc.canvas_height(100.0); - gc.line_width(0.25); - - gc.new_path(); - for pt in sun { - let pt = R_SUN * pt; - gc.line_to(pt.x, pt.y); + let (canvas, mut events) = create_drawing_window_with_events("Canvas window"); + 'main: loop { + while let Poll::Ready(event) = + events.poll_next_unpin(&mut Context::from_waker(Waker::noop())) + { + let Some(_event) = event else { break 'main }; } - gc.close_path(); - gc.stroke_color(Rgba(0.8, 0.7, 0.1, 1.0)); - gc.stroke(); + canvas.draw(|gc| { + gc.clear_canvas(Rgba(0.1, 0.1, 0.1, 1.0)); + + gc.canvas_height(100.0); + gc.line_width(0.25); - for k in 0..N_SAT { - let m = Mat2::from_angle(k as f32 / N_SAT as f32 * 2. * PI); gc.new_path(); - for pt in &sat { - let pt = m * vec2(OFF_SAT, 0.) + R_SAT * pt; + for pt in &sun { + let pt = R_SUN * pt; gc.line_to(pt.x, pt.y); } gc.close_path(); - gc.stroke_color(Rgba(0.1, 0.8, 0.1, 1.0)); + gc.stroke_color(Rgba(0.8, 0.7, 0.1, 1.0)); gc.stroke(); - } - gc.new_path(); - for pt in crown { - let pt = R_SKY * pt; - gc.line_to(pt.x, pt.y); - } - gc.close_path(); - gc.stroke_color(Rgba(0.1, 0.4, 0.8, 1.0)); - gc.stroke(); - }); + for k in 0..N_SAT { + let m = Mat2::from_angle(k as f32 / N_SAT as f32 * 2. * PI); + gc.new_path(); + for pt in &sat { + let pt = m * vec2(OFF_SAT, 0.) + R_SAT * pt; + gc.line_to(pt.x, pt.y); + } + gc.close_path(); + gc.stroke_color(Rgba(0.1, 0.8, 0.1, 1.0)); + gc.stroke(); + } + + gc.new_path(); + for pt in &crown { + let pt = R_SKY * pt; + gc.line_to(pt.x, pt.y); + } + gc.close_path(); + gc.stroke_color(Rgba(0.1, 0.4, 0.8, 1.0)); + gc.stroke(); + }); + } }); }