Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.18 KB | None | 0 0
  1. use glutin::event::{Event, WindowEvent};
  2. use glutin::event_loop::{ControlFlow, EventLoop};
  3. use glutin::window::{WindowBuilder};
  4. use glutin::{Context, ContextBuilder, PossiblyCurrent, WindowedContext};
  5.  
  6. pub struct Window {
  7.     window_context: WindowedContext<PossiblyCurrent>,
  8.     event_loop: EventLoop<()>
  9. }
  10.  
  11. impl Window {
  12.     pub fn new() -> Self {
  13.         let event_loop = EventLoop::new();
  14.         let wb = WindowBuilder::new()
  15.             .with_title("OpenGL Example")
  16.             .with_resizable(true);
  17.  
  18.         let window_context = ContextBuilder::new()
  19.             .build_windowed(wb, &event_loop)
  20.             .expect("Could not create windowed context!");
  21.  
  22.         let window_context = unsafe {
  23.             window_context.make_current()
  24.                 .expect("Could not front the current context")
  25.         };
  26.  
  27.         Window {
  28.             window_context,
  29.             event_loop
  30.         }
  31.     }
  32.  
  33.     pub fn context(&self) -> &Context<PossiblyCurrent> {
  34.         self.window_context.context()
  35.     }
  36.  
  37.     pub fn run_with_draw(&self) {
  38.         self.event_loop.run(move |event, _, control_flow| {
  39.             *control_flow = ControlFlow::Wait;
  40.  
  41.             match event {
  42.                 Event::LoopDestroyed => return,
  43.                 Event::WindowEvent { ref event, .. } => match event {
  44.                     WindowEvent::Resized(logical_size) => {
  45.                         let dpi_factor = self.window_context.window()
  46.                             .hidpi_factor();
  47.  
  48.                         self.window_context
  49.                             .resize(logical_size.to_physical(dpi_factor));
  50.                     }
  51.                     WindowEvent::RedrawRequested => {
  52.                         // Draw
  53.                     }
  54.                     WindowEvent::CloseRequested => {
  55.                         *control_flow = ControlFlow::Exit
  56.                     }
  57.                     _ => (),
  58.                 },
  59.                 _ => (),
  60.             }
  61.         });
  62.     }
  63. }
  64.  
  65. $ cargo build
  66.  
  67. error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  68.   --> src/window.rs:38:29
  69.    |
  70. 38 |           self.event_loop.run(move |event, _, control_flow| {
  71.    |  _____________________________^
  72. 39 | |             *control_flow = ControlFlow::Wait;
  73. 40 | |
  74. 41 | |             match event {
  75. ...  |
  76. 60 | |             }
  77. 61 | |         });
  78.    | |_________^
  79.    |
  80. note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 37:5...
  81.   --> src/window.rs:37:5
  82.    |
  83. 37 | /     pub fn run_with_draw(&self) {
  84. 38 | |         self.event_loop.run(move |event, _, control_flow| {
  85. 39 | |             *control_flow = ControlFlow::Wait;
  86. 40 | |
  87. ...  |
  88. 61 | |         });
  89. 62 | |     }
  90.    | |_____^
  91.    = note: ...so that the types are compatible:
  92.            expected &window::Window
  93.               found &window::Window
  94.    = note: but, the lifetime must be valid for the static lifetime...
  95. note: ...so that the type `[closure@src/window.rs:38:29: 61:10 self:&window::Window]` will meet its required lifetime bounds
  96.   --> src/window.rs:38:25
  97.    |
  98. 38 |         self.event_loop.run(move |event, _, control_flow| {
  99.    |                         ^^^
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement