lava-lamp/src/main.rs

80 lines
2.1 KiB
Rust

mod drawable;
mod particle;
use drawable::DrawShape;
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,
particles: Vec<Particle>,
}
fn fill_particles(app: &App, particles: &mut Vec<Particle>) {
let boundary = app.window_rect();
for _ in 0..PARTICLE_COUNT {
let x = random_range(boundary.left(), boundary.right());
let y = random_range(boundary.top(), boundary.bottom());
let particle = Particle::new(Vec2::new(x, y));
particles.push(particle);
}
}
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();
let mut particles = Vec::new();
fill_particles(app, &mut particles);
Model { _window, particles }
}
// 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()
}
if let Resized(_) = event {
model.particles.clear();
fill_particles(app, &mut model.particles);
}
println!("{:?}", event);
}
// Update the state of your application here. By default, this gets called right before `view`.
fn update(_app: &App, _model: &mut Model, _update: Update) {}
// Draw the state of your `Model` into the given `Frame` here.
fn view(app: &App, model: &Model, frame: Frame) {
let canvas = app.draw();
canvas.background().color(CORNFLOWERBLUE);
canvas.draw(&model.particles);
// I don't think there is even a fail condition in this function, but it returns a result?
canvas.to_frame(app, &frame).unwrap();
}
fn main() {
nannou::app(model).update(update).run();
}