Fix benchmarks for latest glium/glutin API
[mandelwow.git] / main.rs
diff --git a/main.rs b/main.rs
index 86a17c2c61e0d8f288c91a0f679893db95259729..0cf1c18fce7db7f1a671c833373152252e9af75a 100644 (file)
--- a/main.rs
+++ b/main.rs
@@ -1,12 +1,12 @@
 use cgmath::conv::array4x4;
 use cgmath::{Euler, Matrix4, Rad, SquareMatrix, Vector3, Vector4, Zero};
 use glium::glutin::event::{ self, Event, VirtualKeyCode, WindowEvent };
-use glium::glutin::event_loop::{ ControlFlow };
+use glium::glutin::event_loop::ControlFlow;
 use glium::{Display, Program, Surface, uniform};
+use instant::Duration;
 use mandelwow_lib::*;
 use std::f32::consts::PI;
 use std::rc::Rc;
-use std::time::{Duration, Instant};
 
 #[cfg(target_os = "emscripten")]
 use std::os::raw::{c_int, c_void};
@@ -29,16 +29,14 @@ fn gl_info(display: &glium::Display) {
 const SEA_XSIZE: usize = 40;
 const SEA_ZSIZE: usize = 25;
 
-struct World<'a> {
-    display: Display,
-
+struct World {
     mandelwow_program: Rc<Program>,
     mandelwow_bounds: Cube,
     mandelwow_bbox: BoundingBox,
     bounding_box_enabled: bool,
 
     shaded_cube: ShadedCube,
-    text: Text<'a>,
+    text: Text,
 
     sea: [[Vector3<f32>; SEA_ZSIZE]; SEA_XSIZE],
 
@@ -47,11 +45,11 @@ struct World<'a> {
     last_hit: f32,
 }
 
-impl<'a> World<'a> {
-    pub fn new(display: glium::Display) -> World<'a> {
-        let mandelwow_program = Rc::new(mandelwow::program(&display));
-        let bounding_box_program = Rc::new(bounding_box::solid_fill_program(&display));
-        let shaded_program = Rc::new(shaded_cube::shaded_program(&display));
+impl World {
+    pub fn new(display: &glium::Display) -> World {
+        let mandelwow_program = Rc::new(mandelwow::program(display));
+        let bounding_box_program = Rc::new(bounding_box::solid_fill_program(display));
+        let shaded_program = Rc::new(shaded_cube::shaded_program(display));
 
         // These are the bounds for the 3D slice of the 4D Mandelwow
         let mandelwow_bounds = Cube {
@@ -74,6 +72,7 @@ impl<'a> World<'a> {
         println!("xstep={} ystep={:?}", sea_xstep, sea_zstep);
 
         let mut sea = [[Vector3::zero(); SEA_ZSIZE]; SEA_XSIZE];
+        #[allow(clippy::needless_range_loop)]
         for x in 0..SEA_XSIZE {
             for z in 0..SEA_ZSIZE {
                 sea[x][z] = Vector3 {
@@ -87,23 +86,22 @@ impl<'a> World<'a> {
         World {
             mandelwow_program,
             mandelwow_bbox: BoundingBox::new(
-                &display, &mandelwow_bounds, bounding_box_program.clone()),
+                display, &mandelwow_bounds, bounding_box_program),
             mandelwow_bounds,
             bounding_box_enabled: true,
 
-            shaded_cube: ShadedCube::new(&display, shaded_program.clone()),
-            text: text::Text::new(&display),
-            sea: sea,
+            shaded_cube: ShadedCube::new(display, shaded_program),
+            text: text::Text::new(display),
+            sea,
 
             hit_time: 0.0,
             last_hit: 0.0,
-
-            display,
         }
     }
 
     fn draw_frame(
         &self,
+        display: &Display,
         camera: &support::camera::CameraState,
         t: f32,
     ) {
@@ -120,7 +118,7 @@ impl<'a> World<'a> {
 
         //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
 
-        let mut frame = self.display.draw();
+        let mut frame = display.draw();
         frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
 
         let rotation = Matrix4::from(Euler {
@@ -176,7 +174,7 @@ impl<'a> World<'a> {
         }
 
         mandelwow::draw(
-            &self.display,
+            &display,
             &mut frame,
             &self.mandelwow_program,
             model,
@@ -256,12 +254,9 @@ fn main() {
     let mut soundplayer = sound::start();
 
     let event_loop = glutin::event_loop::EventLoop::new();
-    //let fullscreen = Some(glutin::window::Fullscreen::Borderless(event_loop.primary_monitor()));
     let window = glutin::window::WindowBuilder::new()
         //.with_dimensions(1280, 720)
-        //.with_fullscreen(fullscreen);
-        ;
-    //.with_title("MandelWow");
+        .with_title("MandelWow");
     let context = glutin::ContextBuilder::new()
         //.with_gl_profile(glutin::GlProfile::Core)
         //.with_gl(glutin::GlRequest::Specific(glutin::Api::WebGl, (2, 0)))
@@ -275,11 +270,11 @@ fn main() {
 
     let display = glium::Display::new(window, context, &event_loop).unwrap();
     gl_info(&display);
-    let mut world = World::new(display);
+    let mut world = World::new(&display);
 
     let mut timer = Timer::new();
     let mut camera = support::camera::CameraState::new();
-    let _fullscreen = true;
+    let mut fullscreen = false;
 
     event_loop.run(move |event, _, control_flow| {
         let t = timer.t;
@@ -291,71 +286,56 @@ fn main() {
 
         camera.update();
 
-        *control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::from_nanos(16666667));
+        *control_flow = ControlFlow::WaitUntil(timer.now + Duration::from_nanos(16_666_667));
         match event {
+            Event::MainEventsCleared => {
+                timer.update();
+                world.draw_frame(&display, &camera, t);
+            }
             Event::NewEvents(cause) => {
                 match cause {
                     event::StartCause::ResumeTimeReached { .. } | event::StartCause::Init => {
-                        world.draw_frame(&camera, t);
+                        // FIXME
                     },
                     _ => {}
                 }
-            } _ => (),
-        }
-        if let Event::WindowEvent { event, .. } = event {
-            camera.process_input(&event);
-            match event {
-                WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
-                WindowEvent::KeyboardInput { input, .. } => {
-                    if input.state == event::ElementState::Pressed {
-                        if let Some(key) = input.virtual_keycode {
-                            match key {
-                                VirtualKeyCode::Escape | VirtualKeyCode::Q => {
-                                    *control_flow = ControlFlow::Exit;
+            }
+            Event::WindowEvent { event, .. } => {
+                camera.process_input(&event);
+                match event {
+                    WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
+                    WindowEvent::KeyboardInput { input, .. } => {
+                        if input.state == event::ElementState::Pressed {
+                            if let Some(key) = input.virtual_keycode {
+                                match key {
+                                    VirtualKeyCode::Escape | VirtualKeyCode::Q => {
+                                        *control_flow = ControlFlow::Exit;
+                                    }
+                                    VirtualKeyCode::B => world.bounding_box_enabled ^= true,
+                                    VirtualKeyCode::P => timer.pause ^= true,
+                                    VirtualKeyCode::PageUp => timer.t += 0.1,
+                                    VirtualKeyCode::PageDown => timer.t -= 0.2,
+                                    VirtualKeyCode::F10 => screenshot::take_screenshot(&display),
+                                    VirtualKeyCode::F11 | VirtualKeyCode::Return => {
+                                        fullscreen ^= true;
+                                        let fs = if fullscreen {
+                                            // let monitor_handle = display.gl_window().window()
+                                            //    .available_monitors().next().unwrap();
+                                            Some(glium::glutin::window::Fullscreen::Borderless(None))
+                                        } else {
+                                            None
+                                        };
+                                        display.gl_window().window().set_fullscreen(fs);
+                                    }
+                                    _ => (),
                                 }
-                                _ => (),
                             }
                         }
                     }
+                    _ => (),
                 }
-                /*
-                KeyboardInput { input: glutin::KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::Escape), .. } } |
-                KeyboardInput { input: glutin::KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::Q), .. } } => {
-                    *control_flow = ControlFlow::Exit;
-                },
-                KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::B) } => {
-                    bounding_box_enabled ^= true;
-                },
-                KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::P) } => {
-                    timer.pause ^= true;
-                },
-                KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::PageUp) } => {
-                    timer.t += 0.01;
-                },
-                KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::PageDown) } => {
-                    timer.t -= 0.01;
-                },
-                KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::F10) } => {
-                    screenshot(&display);
-                },
-                KeyboardInput { state: Pressed, virtual_keycode: Some(VirtualKeyCode::F11) } => {
-                    fullscreen ^= true;
-                    if fullscreen {
-                        // Not implemented on Linux
-                        glutin::WindowBuilder::new()
-                            .with_fullscreen(glutin::get_primary_monitor())
-                            .with_depth_buffer(24)
-                            .rebuild_glium(&display).unwrap();
-                    } else {
-                        glutin::WindowBuilder::new()
-                            .rebuild_glium(&display).unwrap();
-                    }
-                },
-                */
-                _ => (),
-            }
+            },
+            _ => (),
         }
-
-        timer.update();
     });
 }