From df6dceab7080d60006fd722d10bbeb21d57a413f Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Wed, 14 Feb 2024 21:40:29 -0800 Subject: [PATCH] getting some drawing done --- src/drawable.rs | 12 ++++++++ src/main.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/drawable.rs diff --git a/src/drawable.rs b/src/drawable.rs new file mode 100644 index 0000000..4cf9271 --- /dev/null +++ b/src/drawable.rs @@ -0,0 +1,12 @@ +pub(crate) trait Drawable { + fn draw(&self, draw: &Draw); +} + +impl Drawable for Vec +where + T: Drawable, +{ + fn draw(&self, draw: &Draw) { + self.iter().for_each(|s| s.draw(draw)) + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..a3b9c2c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,77 @@ -fn main() { - println!("Hello, world!"); +mod drawable; + +use drawable::Drawable; +use nannou::prelude::*; + +#[derive(Debug)] +struct Particle { + pos: Vec2, + radius: f32, +} + +impl Particle { + fn new(pos: Vec2) -> Self { + Self { + pos, + ..Default::default() + } + } +} + +impl Default for Particle { + fn default() -> Self { + Self { + pos: Default::default(), + radius: 10.0, + } + } +} + +impl Drawable for Particle { + fn draw(&self, draw: &Draw) { + draw.ellipse().color(RED).radius(self.radius).xy(self.pos); + } +} + +struct Model { + // Store the window ID so we can refer to this specific window later if needed. + _window: WindowId, +} + +fn model(app: &App) -> Model { + // Create a new window! Store the ID so we can refer to it later. + let _window = app + .new_window() + .size(512, 512) + .title("nannou") + .view(view) // The function that will be called for presenting graphics to a frame. + .event(event) // The function that will be called when the window receives events. + .build() + .unwrap(); + + Model { _window } +} + +// Handle events related to the window and update the model if necessary +fn event(_app: &App, _model: &mut Model, event: WindowEvent) { + if let KeyReleased(Key::Escape) = event { + _app.quit() + } + + println!("{:?}", event); +} + +// Draw the state of your `Model` into the given `Frame` here. +fn view(app: &App, _model: &Model, frame: Frame) { + let draw = app.draw(); + draw.background().color(CORNFLOWERBLUE); + + draw.ellipse().color(STEELBLUE).x_y(200.0, -100.0); + + // I don't think there is even a fail condition in this function, but it returns a result? + draw.to_frame(app, &frame).unwrap(); +} + +fn main() { + nannou::app(model).run(); }