now with the main loop

This commit is contained in:
numzero 2025-09-06 03:36:19 +03:00
parent 9d4c61d7ec
commit 58fb346b25
3 changed files with 45 additions and 31 deletions

1
Cargo.lock generated
View File

@ -885,6 +885,7 @@ name = "gears"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"flo_draw", "flo_draw",
"futures",
"glam", "glam",
] ]

View File

@ -5,4 +5,5 @@ edition = "2024"
[dependencies] [dependencies]
flo_draw = "0.3.1" flo_draw = "0.3.1"
futures = "0.3.31"
glam = "0.30.5" glam = "0.30.5"

View File

@ -1,9 +1,14 @@
use std::f32::consts::PI; use std::{
f32::consts::PI,
pin::pin,
task::{Context, Poll, Waker},
};
use flo_draw::{ use flo_draw::{
canvas::{Color::Rgba, GraphicsContext, GraphicsPrimitives}, 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}; use glam::{FloatExt, Mat2, Vec2, vec2};
const fn rad(deg: f32) -> f32 { const fn rad(deg: f32) -> f32 {
@ -92,42 +97,49 @@ fn main() {
let sat = gear_shape(N_TEETH_SAT); let sat = gear_shape(N_TEETH_SAT);
let crown = gear_shape_outer(N_TEETH_SKY); let crown = gear_shape_outer(N_TEETH_SKY);
let canvas = create_drawing_window("Canvas window"); let (canvas, mut events) = create_drawing_window_with_events("Canvas window");
canvas.draw(|gc| { 'main: loop {
gc.clear_canvas(Rgba(0.1, 0.1, 0.1, 1.0)); while let Poll::Ready(event) =
events.poll_next_unpin(&mut Context::from_waker(Waker::noop()))
gc.canvas_height(100.0); {
gc.line_width(0.25); let Some(_event) = event else { break 'main };
gc.new_path();
for pt in sun {
let pt = R_SUN * pt;
gc.line_to(pt.x, pt.y);
} }
gc.close_path(); canvas.draw(|gc| {
gc.stroke_color(Rgba(0.8, 0.7, 0.1, 1.0)); gc.clear_canvas(Rgba(0.1, 0.1, 0.1, 1.0));
gc.stroke();
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(); gc.new_path();
for pt in &sat { for pt in &sun {
let pt = m * vec2(OFF_SAT, 0.) + R_SAT * pt; let pt = R_SUN * pt;
gc.line_to(pt.x, pt.y); gc.line_to(pt.x, pt.y);
} }
gc.close_path(); 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.stroke();
}
gc.new_path(); for k in 0..N_SAT {
for pt in crown { let m = Mat2::from_angle(k as f32 / N_SAT as f32 * 2. * PI);
let pt = R_SKY * pt; gc.new_path();
gc.line_to(pt.x, pt.y); for pt in &sat {
} let pt = m * vec2(OFF_SAT, 0.) + R_SAT * pt;
gc.close_path(); gc.line_to(pt.x, pt.y);
gc.stroke_color(Rgba(0.1, 0.4, 0.8, 1.0)); }
gc.stroke(); 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();
});
}
}); });
} }