1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! The [glutin] windowing implementation for [luminance-windowing].
//!
//! [glutin]: https://crates.io/crates/glutin
//! [luminance-windowing]: https://crates.io/crates/luminance-windowing

#![deny(missing_docs)]

use gl;
pub use glutin::dpi::PhysicalSize;
use glutin::{
  Api, ContextBuilder, EventsLoop, GlProfile, GlRequest, NotCurrent, PossiblyCurrent,
  WindowBuilder, WindowedContext,
};
pub use glutin::{ContextError, CreationError};
use luminance::context::GraphicsContext;
use luminance::framebuffer::Framebuffer;
use luminance::state::{GraphicsState, StateQueryError};
use luminance::texture::{Dim2, Flat};
pub use luminance_windowing::{CursorMode, Surface, WindowDim, WindowOpt};
use std::cell::RefCell;
use std::fmt;
use std::os::raw::c_void;
use std::rc::Rc;

/// Error that might occur when creating a Glutin surface.
#[derive(Debug)]
pub enum GlutinError {
  /// Something went wrong when creating the Glutin surface. The carried [`CreationError`] provides
  /// more information.
  CreationError(CreationError),
  /// OpenGL context error.
  ContextError(ContextError),
  /// Graphics state error that might occur when querying the initial state.
  GraphicsStateError(StateQueryError),
}

impl fmt::Display for GlutinError {
  fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
    match *self {
      GlutinError::CreationError(ref e) => write!(f, "Glutin surface creation error: {}", e),
      GlutinError::ContextError(ref e) => write!(f, "Glutin OpenGL context creation error: {}", e),
      GlutinError::GraphicsStateError(ref e) => {
        write!(f, "OpenGL graphics state initialization error: {}", e)
      }
    }
  }
}

impl From<CreationError> for GlutinError {
  fn from(e: CreationError) -> Self {
    GlutinError::CreationError(e)
  }
}

impl From<ContextError> for GlutinError {
  fn from(e: ContextError) -> Self {
    GlutinError::ContextError(e)
  }
}

/// The Glutin surface.
///
/// You want to create such an object in order to use any [luminance] construct.
///
/// [luminance]: https://crates.io/crates/luminance
pub struct GlutinSurface {
  /// The windowed context.
  pub ctx: WindowedContext<PossiblyCurrent>,
  /// Associated events loop.
  pub event_loop: EventsLoop,
  gfx_state: Rc<RefCell<GraphicsState>>,
}

unsafe impl GraphicsContext for GlutinSurface {
  fn state(&self) -> &Rc<RefCell<GraphicsState>> {
    &self.gfx_state
  }
}

impl GlutinSurface {
  /// Create a new [`GlutinSurface`] by consuming a [`WindowBuilder`].
  ///
  /// This is an alternative method to [`new`] that is more flexible as you have access to the
  /// whole `glutin` types.
  ///
  /// `window_builder` is the default object when passed to your closure and `ctx_builder` is
  /// already initialized for the OpenGL context (you’re not supposed to change it!).
  pub fn from_builders<WB, CB>(window_builder: WB, ctx_builder: CB) -> Result<Self, GlutinError>
  where
    WB: FnOnce(WindowBuilder) -> WindowBuilder,
    CB: FnOnce(ContextBuilder<NotCurrent>) -> ContextBuilder<NotCurrent>,
  {
    let event_loop = EventsLoop::new();

    let window_builder = window_builder(WindowBuilder::new());

    let windowed_ctx = ctx_builder(
      ContextBuilder::new()
        .with_gl(GlRequest::Specific(Api::OpenGl, (3, 3)))
        .with_gl_profile(GlProfile::Core),
    )
    .build_windowed(window_builder, &event_loop)?;

    let ctx = unsafe { windowed_ctx.make_current().map_err(|(_, e)| e)? };

    // init OpenGL
    gl::load_with(|s| ctx.get_proc_address(s) as *const c_void);

    ctx.window().show();

    let gfx_state = GraphicsState::new().map_err(GlutinError::GraphicsStateError)?;
    let surface = GlutinSurface {
      ctx,
      event_loop,
      gfx_state: Rc::new(RefCell::new(gfx_state)),
    };

    Ok(surface)
  }

  /// Create a new [`GlutinSurface`] from scratch.
  pub fn new(dim: WindowDim, title: &str, win_opt: WindowOpt) -> Result<Self, GlutinError> {
    let event_loop = EventsLoop::new();

    let window_builder = WindowBuilder::new().with_title(title);
    let window_builder = match dim {
      WindowDim::Windowed(w, h) => window_builder.with_dimensions((w, h).into()),
      WindowDim::Fullscreen => {
        window_builder.with_fullscreen(Some(event_loop.get_primary_monitor()))
      }
      WindowDim::FullscreenRestricted(w, h) => window_builder
        .with_dimensions((w, h).into())
        .with_fullscreen(Some(event_loop.get_primary_monitor())),
    };

    let windowed_ctx = ContextBuilder::new()
      .with_gl(GlRequest::Specific(Api::OpenGl, (3, 3)))
      .with_gl_profile(GlProfile::Core)
      .with_multisampling(win_opt.num_samples().unwrap_or(0) as u16)
      .with_double_buffer(Some(true))
      .build_windowed(window_builder, &event_loop)?;

    let ctx = unsafe { windowed_ctx.make_current().map_err(|(_, e)| e)? };

    // init OpenGL
    gl::load_with(|s| ctx.get_proc_address(s) as *const c_void);

    ctx.window().show();

    let gfx_state = GraphicsState::new().map_err(GlutinError::GraphicsStateError)?;
    let surface = GlutinSurface {
      ctx,
      event_loop,
      gfx_state: Rc::new(RefCell::new(gfx_state)),
    };

    Ok(surface)
  }

  /// Get the underlying size (in physical pixels) of the surface.
  ///
  /// This is equivalent to getting the inner size of the windowed context and converting it to
  /// a physical size by using the HiDPI factor of the windowed context.
  pub fn size(&self) -> [u32; 2] {
    let logical = self.ctx.window().get_inner_size().unwrap();
    let (w, h) = PhysicalSize::from_logical(logical, self.ctx.window().get_hidpi_factor()).into();
    [w, h]
  }

  /// Get access to the back buffer.
  pub fn back_buffer(&mut self) -> Result<Framebuffer<Flat, Dim2, (), ()>, GlutinError> {
    Ok(Framebuffer::back_buffer(self, self.size()))
  }

  /// Swap the back and front buffers.
  pub fn swap_buffers(&mut self) {
    let _ = self.ctx.swap_buffers();
  }
}