1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use glium::{
    self, glutin::{self, WindowEvent},
};
use gridsim::{GetNeighbors, Sim, SquareGrid, TakeMoveNeighbors};
use Renderer;

type Coloration<C> = Box<Fn(&C) -> [f32; 4] + Sync>;
type Filter<C> = Box<Fn(&C) -> bool + Sync>;

pub struct Loop<'a, S>
where
    S: Sim<'a>,
{
    scale: f32,
    coloration: Coloration<S::Cell>,
    filter: Filter<S::Cell>,
}

impl<'a, S> Loop<'a, S>
where
    S: Sim<'a>,
{
    pub fn new<C>(coloration: C) -> Self
    where
        C: Fn(&S::Cell) -> [f32; 4] + Sync + 'static,
    {
        Loop {
            scale: 10.0,
            coloration: Box::new(coloration),
            filter: Box::new(|_| true),
        }
    }

    pub fn new_bool() -> Self
    where
        S: Sim<'a, Cell = bool>,
    {
        Loop {
            scale: 10.0,
            coloration: Box::new(|_| [1.0, 1.0, 1.0, 1.0]),
            filter: Box::new(|&c| c),
        }
    }

    pub fn scale(&mut self, scale: f32) -> &mut Self {
        self.scale = scale;
        self
    }

    pub fn filter<F>(&mut self, filter: F) -> &mut Self
    where
        F: Fn(&S::Cell) -> bool + Sync + 'static,
    {
        self.filter = Box::new(filter);
        self
    }

    pub fn run(&self, mut grid: SquareGrid<'a, S>)
    where
        S: 'a,
        S::Cell: Sync + Send,
        S::Move: Sync + Send,
        S::Diff: Sync + Send,
        S::Neighbors: Sync + Send,
        S::MoveNeighbors: Sync + Send,
        SquareGrid<'a, S>: TakeMoveNeighbors<usize, S::MoveNeighbors>,
        SquareGrid<'a, S>: GetNeighbors<'a, usize, S::Neighbors>,
    {
        let mut events_loop = glutin::EventsLoop::new();
        let window = glutin::WindowBuilder::new().with_dimensions(
            (self.scale * grid.get_width() as f32) as u32,
            (self.scale * grid.get_height() as f32) as u32,
        );
        let context = glutin::ContextBuilder::new().with_vsync(true);
        let display = glium::Display::new(window, context, &events_loop).unwrap();
        let renderer = Renderer::new(&display);

        loop {
            use glium::Surface;
            grid.cycle();

            let mut target = display.draw();
            target.clear_color(0.0, 0.0, 0.0, 1.0);
            renderer
                .render(
                    &display,
                    &mut target,
                    &grid,
                    Default::default(),
                    &*self.coloration,
                    &*self.filter,
                )
                .unwrap();
            target.finish().unwrap();

            let mut finish = false;

            // the main loop
            events_loop.poll_events(|event| {
                match event {
                    glutin::Event::WindowEvent { event, .. } => {
                        match event {
                            // Break from the main loop when the window is closed.
                            WindowEvent::Closed => {
                                finish = true;
                            }
                            _ => (),
                        }
                    }
                    _ => (),
                }
            });

            if finish {
                return;
            }
        }
    }
}