Skip to main content

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, |mut configs| configs.next().unwrap())
39    .unwrap();
40  (event_loop, window.unwrap(), gl_config)
41}
42
43/// Create glium Display from given window and OpenGL configuration
44pub fn glium_display (
45  window     : &winit::window::Window,
46  gl_config  : &glutin::config::Config,
47  gl_version : glutin::context::Version,
48  gl_profile : glutin::context::GlProfile
49) -> glium::Display <glutin::surface::WindowSurface> {
50  use glutin::context::NotCurrentGlContext;
51  use glutin::display::{GetGlDisplay, GlDisplay};
52  use glutin::surface::GlSurface;
53  let (width, height) = window.inner_size().into();
54  #[cfg(target_os = "windows")]
55  let raw_window_handle = unsafe {
56    use winit::platform::windows::WindowExtWindows;
57    window.window_handle_any_thread()
58  }.unwrap().into();
59  #[cfg(not(target_os = "windows"))]
60  let raw_window_handle = {
61    use winit::raw_window_handle::HasWindowHandle;
62    window.window_handle().unwrap().into()
63  };
64  let surface_attrs = glutin::surface::SurfaceAttributesBuilder::<
65    glutin::surface::WindowSurface
66  >::new().build (
67    raw_window_handle,
68    std::num::NonZeroU32::new (width).unwrap(),
69    std::num::NonZeroU32::new (height).unwrap(),
70  );
71  let surface = unsafe {
72    gl_config.display().create_window_surface (gl_config, &surface_attrs)
73      .unwrap()
74  };
75  let context_attributes = glutin::context::ContextAttributesBuilder::new()
76    .with_context_api (glutin::context::ContextApi::OpenGl (Some (gl_version)))
77    .with_profile (gl_profile)
78    .build (Some (raw_window_handle));
79  let not_current_context = unsafe {
80    gl_config.display().create_context (gl_config, &context_attributes)
81      .expect("failed to create context")
82  };
83  let possibly_current_context = not_current_context.make_current (&surface)
84    .unwrap();
85  // set vsync
86  surface.set_swap_interval (
87    &possibly_current_context,
88    glutin::surface::SwapInterval::Wait (std::num::NonZeroU32::new (1).unwrap())
89  ).unwrap();
90  glium::Display::from_context_surface (possibly_current_context, surface)
91    .unwrap()
92}
93
94/// Create glium Display from given window and OpenGL configuration
95pub fn glium_display_gl33core (
96  window     : &winit::window::Window,
97  gl_config  : &glutin::config::Config
98) -> glium::Display <glutin::surface::WindowSurface> {
99  glium_display (
100    window, gl_config, glutin::context::Version { major: 3, minor: 3 },
101    glutin::context::GlProfile::Core
102  )
103}