Eliminate lifetime annotations in World and Text
authorBernie Innocenti <codewiz@google.com>
Tue, 21 Jan 2020 15:34:04 +0000 (00:34 +0900)
committerBernie Innocenti <codewiz@google.com>
Tue, 21 Jan 2020 15:34:04 +0000 (00:34 +0900)
Changing the lifetime of glium::DrawParameters to 'static did the trick.
Poof! No need carry around lifetime annotations everywhere.

Also removed Display from World, which paves the way for rendering World
onto multiple displays at once (think VR or video streaming...)

main.rs
text.rs

diff --git a/main.rs b/main.rs
index 86a17c2c61e0d8f288c91a0f679893db95259729..4aa56cbf57c9d5e0517803c6153d380b811a8ad2 100644 (file)
--- a/main.rs
+++ b/main.rs
@@ -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 {
@@ -87,23 +85,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.clone()),
             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.clone()),
+            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 +117,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 +173,7 @@ impl<'a> World<'a> {
         }
 
         mandelwow::draw(
-            &self.display,
+            &display,
             &mut frame,
             &self.mandelwow_program,
             model,
@@ -275,7 +272,7 @@ 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();
@@ -296,7 +293,7 @@ fn main() {
             Event::NewEvents(cause) => {
                 match cause {
                     event::StartCause::ResumeTimeReached { .. } | event::StartCause::Init => {
-                        world.draw_frame(&camera, t);
+                        world.draw_frame(&display, &camera, t);
                     },
                     _ => {}
                 }
diff --git a/text.rs b/text.rs
index 4c66a285cbdd5ab689c478834cf6692ece11f825..e020156cd08956f226d9c36c0117dcaa24069452 100644 (file)
--- a/text.rs
+++ b/text.rs
@@ -51,16 +51,16 @@ struct Vertex {
 }
 implement_vertex!(Vertex, position, tex_coords);
 
-pub struct Text<'a> {
+pub struct Text {
     tex: texture::Texture2d,
     vertex_buffer: glium::VertexBuffer<Vertex>,
     index_buffer: glium::IndexBuffer<u16>,
     program: glium::Program,
-    params: glium::DrawParameters<'a>,
+    params: glium::DrawParameters<'static>,
 }
 
-impl<'a> Text<'a> {
-    pub fn new(display: &Display) -> Text<'a> {
+impl Text {
+    pub fn new(display: &Display) -> Text {
         let (w, h, pixels) = c64_font();
         let image = glium::texture::RawImage2d {
             data: std::borrow::Cow::from(pixels),