better drawable
This commit is contained in:
parent
6d270344cf
commit
481ed64a85
3 changed files with 28 additions and 10 deletions
|
@ -12,3 +12,19 @@ where
|
|||
self.iter().for_each(|s| s.draw(draw))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait DrawShape<T>
|
||||
where
|
||||
T: Drawable,
|
||||
{
|
||||
fn draw(&self, drawable: &T);
|
||||
}
|
||||
|
||||
impl<T> DrawShape<T> for Draw
|
||||
where
|
||||
T: Drawable,
|
||||
{
|
||||
fn draw(&self, drawable: &T) {
|
||||
drawable.draw(self);
|
||||
}
|
||||
}
|
||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -1,10 +1,13 @@
|
|||
mod drawable;
|
||||
mod particle;
|
||||
|
||||
use drawable::Drawable;
|
||||
use drawable::{DrawShape, Drawable};
|
||||
use nannou::prelude::*;
|
||||
use particle::Particle;
|
||||
|
||||
const PARTICLE_COUNT: u32 = 200;
|
||||
const PARTICLE_SIZE: f32 = 10.0;
|
||||
|
||||
struct Model {
|
||||
// Store the window ID so we can refer to this specific window later if needed.
|
||||
_window: WindowId,
|
||||
|
@ -14,7 +17,7 @@ struct Model {
|
|||
fn fill_particles(app: &App, particles: &mut Vec<Particle>) {
|
||||
let boundary = app.window_rect();
|
||||
|
||||
for _ in 0..200 {
|
||||
for _ in 0..PARTICLE_COUNT {
|
||||
let x = random_range(boundary.left(), boundary.right());
|
||||
let y = random_range(boundary.top(), boundary.bottom());
|
||||
|
||||
|
@ -59,13 +62,13 @@ fn event(app: &App, model: &mut Model, event: WindowEvent) {
|
|||
|
||||
// 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);
|
||||
let canvas = app.draw();
|
||||
canvas.background().color(CORNFLOWERBLUE);
|
||||
|
||||
model.particles.draw(&draw);
|
||||
canvas.draw(&model.particles);
|
||||
|
||||
// I don't think there is even a fail condition in this function, but it returns a result?
|
||||
draw.to_frame(app, &frame).unwrap();
|
||||
canvas.to_frame(app, &frame).unwrap();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use drawable::Drawable;
|
||||
|
||||
use crate::drawable;
|
||||
use crate::drawable::Drawable;
|
||||
use crate::PARTICLE_SIZE;
|
||||
|
||||
use nannou::prelude::*;
|
||||
|
||||
|
@ -23,7 +22,7 @@ impl Default for Particle {
|
|||
fn default() -> Self {
|
||||
Self {
|
||||
pos: Default::default(),
|
||||
radius: 10.0,
|
||||
radius: PARTICLE_SIZE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue