Skip to content

Commit

Permalink
controls for increasing simulation speed and show framerate
Browse files Browse the repository at this point in the history
  • Loading branch information
surt91 committed Aug 8, 2017
1 parent fd81c20 commit 6f54e29
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ pistoncore-glutin_window = "*"
pistoncore-sdl2_window = "*"
piston2d-graphics = "*"
piston2d-opengl_graphics = "*"

fps_counter = "*"
24 changes: 21 additions & 3 deletions src/animate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ extern crate graphics;
extern crate glutin_window;
extern crate sdl2_window;
extern crate opengl_graphics;
extern crate fps_counter;
extern crate rand;

use std::cmp::min;

use self::graphics::*;
use self::sdl2_window::Sdl2Window as Window;
use self::piston::window::WindowSettings;
use self::piston::input::keyboard::Key::*;

use self::opengl_graphics::{GlGraphics, OpenGL};
use self::piston::event_loop::{Events, EventSettings};
use self::piston::input::{Button, Input};
use self::fps_counter::FPSCounter;

use super::vicsek_model::Vicsek;
use super::bird::Bird;
Expand Down Expand Up @@ -53,23 +56,38 @@ pub fn show(size: (u32, u32), vicsek: &mut Vicsek) {
.unwrap();

let mut gfx = GlGraphics::new(OpenGL::V3_2);
let mut fps = FPSCounter::new();
let mut sweeps_per_second = 100.;
let mut rate = 0;

let mut events = Events::new(EventSettings::new());
while let Some(e) = events.next(&mut window) {
match e {
Input::Render(args) => {
rate = fps.tick();

gfx.draw(args.viewport(), |c, gfx| {
vicsek.render(&c, gfx, &size);
});
}

Input::Press(Button::Keyboard(key)) => {
// TODO
match key {
F => println!("{} FPS", rate),
Up => {
sweeps_per_second *= 1.2;
println!("{} sweeps per second", sweeps_per_second);
}
Down => {
sweeps_per_second /= 1.2;
println!("{} sweeps per second", sweeps_per_second);
}
_ => ()
};
}

Input::Update(args) => {
// 100 sweeps per second
vicsek.sweep((args.dt * 100.).ceil() as u64);
vicsek.sweep((args.dt * sweeps_per_second).ceil() as u64);
}

_ => {}
Expand Down

0 comments on commit 6f54e29

Please sign in to comment.