Skip to content

Commit 99a3615

Browse files
Add GlutinSurface::new_gl33_with_builders
This is a constructor of `GlutinSurface` that allows the `EventLoop`, `WindowBuilder`, and `ContextBuilder` to be passed in and thus configured by the user (In particular, the existing constructors do not allow passing in an `EventLoop`). To ensure the context matches what luminance needs, the context is forced to OpenGL version 3.3 core profile.
1 parent 22b07d4 commit 99a3615

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

luminance-glutin/src/lib.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,9 @@ impl GlutinSurface {
116116
ContextBuilder::new()
117117
.with_gl(GlRequest::Specific(Api::OpenGl, (3, 3)))
118118
.with_gl_profile(GlProfile::Core),
119-
)
120-
.build_windowed(window_builder, &event_loop)?;
119+
);
121120

122-
let ctx = unsafe { windowed_ctx.make_current().map_err(|(_, e)| e)? };
123-
124-
// init OpenGL
125-
gl::load_with(|s| ctx.get_proc_address(s) as *const c_void);
126-
127-
ctx.window().set_visible(true);
128-
129-
let gl = GL33::new().map_err(GlutinError::GraphicsStateError)?;
130-
let surface = GlutinSurface { ctx, gl };
121+
let surface = Self::new_gl33_with_builders(&event_loop, window_builder, windowed_ctx)?;
131122

132123
Ok((surface, event_loop))
133124
}
@@ -138,12 +129,27 @@ impl GlutinSurface {
138129
samples: u16,
139130
) -> Result<(Self, EventLoop<()>), GlutinError> {
140131
let event_loop = EventLoop::new();
141-
142132
let windowed_ctx = ContextBuilder::new()
133+
.with_multisampling(samples)
134+
.with_double_buffer(Some(true));
135+
let surface = Self::new_gl33_with_builders(&event_loop, window_builder, windowed_ctx)?;
136+
137+
Ok((surface, event_loop))
138+
}
139+
140+
/// Create a new [`GlutinSurface`] by passing in the builders.
141+
///
142+
/// This is the most flexible but least hand-holding way of creating a [`GlutinSurface`].
143+
///
144+
/// The OpenGL context should be version 3.3, core profile, and double-buffered.
145+
pub fn new_gl33_with_builders<T>(
146+
event_loop: &EventLoop<T>,
147+
window_builder: WindowBuilder,
148+
context_builder: ContextBuilder<NotCurrent>,
149+
) -> Result<Self, GlutinError> {
150+
let windowed_ctx = context_builder
143151
.with_gl(GlRequest::Specific(Api::OpenGl, (3, 3)))
144152
.with_gl_profile(GlProfile::Core)
145-
.with_multisampling(samples)
146-
.with_double_buffer(Some(true))
147153
.build_windowed(window_builder, &event_loop)?;
148154

149155
let ctx = unsafe { windowed_ctx.make_current().map_err(|(_, e)| e)? };
@@ -155,8 +161,7 @@ impl GlutinSurface {
155161

156162
let gl = GL33::new().map_err(GlutinError::GraphicsStateError)?;
157163
let surface = GlutinSurface { ctx, gl };
158-
159-
Ok((surface, event_loop))
164+
Ok(surface)
160165
}
161166

162167
/// Get the underlying size (in physical pixels) of the surface.

0 commit comments

Comments
 (0)