gl_utils/
init.rs

1//! Glium initialization functions
2
3use std;
4
5use glium::{self, glutin};
6use winit;
7
8/// Creates a `winit::event_loop::EventLoop`, `winit::window::Window`, and
9/// associated `glium::Display` with an OpenGL 3.3 core profile
10pub fn glium_init_gl33core (window_title : &str) -> (
11  winit::event_loop::EventLoop <()>,
12  winit::window::Window,
13  glutin::config::Config,
14  glium::Display <glutin::surface::WindowSurface>
15) {
16  let (event_loop, window, gl_config) = glutin_window (window_title);
17  let display = glium_display_gl33core (&window, &gl_config);
18  (event_loop, window, gl_config, display)
19}
20
21/// Creates a `winit::event_loop::EventLoop`, `winit::window::Window`, and a
22/// `glutin::config::Config` that can be moved or shared to another thread and
23/// used to create a `glium::Display` with `glium_display`
24pub fn glutin_window (window_title : &str) -> (
25  winit::event_loop::EventLoop <()>,
26  winit::window::Window,
27  glutin::config::Config
28) {
29  let event_loop = winit::event_loop::EventLoop::builder()
30    .build().unwrap();
31  let window_attributes = winit::window::Window::default_attributes()
32    .with_title      (window_title)
33    .with_inner_size (winit::dpi::PhysicalSize::new (320, 240));
34  let config_template_builder = glutin::config::ConfigTemplateBuilder::new()
35    .with_api (glutin::config::Api::OPENGL);
36  let (window, gl_config) = glutin_winit::DisplayBuilder::new()
37    .with_window_attributes (Some (window_attributes))
38    .build (&event_loop, config_template_builder,
39      |mut configs| configs.next().unwrap())
40    .unwrap();
41  (event_loop, window.unwrap(), gl_config)
42}
43
44/// Create glium Display from given window and OpenGL configuration
45pub fn glium_display (
46  window     : &winit::window::Window,
47  gl_config  : &glutin::config::Config,
48  gl_version : glutin::context::Version,
49  gl_profile : glutin::context::GlProfile
50) -> glium::Display <glutin::surface::WindowSurface> {
51  use glutin::context::NotCurrentGlContext;
52  use glutin::display::{GetGlDisplay, GlDisplay};
53  use glutin::surface::GlSurface;
54  let (width, height) = window.inner_size().into();
55  #[cfg(target_os = "windows")]
56  let raw_window_handle = unsafe {
57    use winit::platform::windows::WindowExtWindows;
58    window.window_handle_any_thread()
59  }.unwrap().into();
60  #[cfg(not(target_os = "windows"))]
61  let raw_window_handle = {
62    use winit::raw_window_handle::HasWindowHandle;
63    window.window_handle().unwrap().into()
64  };
65  let surface_attrs = glutin::surface::SurfaceAttributesBuilder::<
66    glutin::surface::WindowSurface
67  >::new().build (
68    raw_window_handle,
69    std::num::NonZeroU32::new (width).unwrap(),
70    std::num::NonZeroU32::new (height).unwrap(),
71  );
72  let surface = unsafe {
73    gl_config.display().create_window_surface (&gl_config, &surface_attrs)
74      .unwrap()
75  };
76  let context_attributes = glutin::context::ContextAttributesBuilder::new()
77    .with_context_api (glutin::context::ContextApi::OpenGl (Some (gl_version)))
78    .with_profile (gl_profile)
79    .build (Some (raw_window_handle));
80  let not_current_context = unsafe {
81    gl_config.display().create_context (&gl_config, &context_attributes)
82      .expect("failed to create context")
83  };
84  let possibly_current_context = not_current_context.make_current (&surface)
85    .unwrap();
86  // set vsync
87  surface.set_swap_interval (
88    &possibly_current_context,
89    glutin::surface::SwapInterval::Wait (std::num::NonZeroU32::new (1).unwrap())
90  ).unwrap();
91  glium::Display::from_context_surface (possibly_current_context, surface)
92    .unwrap()
93}
94
95/// Create glium Display from given window and OpenGL configuration
96pub fn glium_display_gl33core (
97  window     : &winit::window::Window,
98  gl_config  : &glutin::config::Config
99) -> glium::Display <glutin::surface::WindowSurface> {
100  glium_display (
101    &window, &gl_config, glutin::context::Version { major: 3, minor: 3 },
102    glutin::context::GlProfile::Core
103  )
104}