Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ impl Window {
}

#[cfg(feature = "opengl")]
pub fn gl_context(&self) -> Option<&GlContext> {
self.gl_context.as_ref()
pub fn gl_context(&mut self) -> Option<GlContext> {
self.gl_context.take()
}

#[cfg(feature = "opengl")]
Expand Down
26 changes: 15 additions & 11 deletions src/win/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ use crate::{
use super::keyboard::KeyboardState;

#[cfg(feature = "opengl")]
use crate::{gl::GlContext, window::RawWindowHandleWrapper};
use {
crate::{gl::GlContext, window::RawWindowHandleWrapper},
std::sync::Mutex,
};

unsafe fn generate_guid() -> String {
let mut guid: GUID = std::mem::zeroed();
Expand Down Expand Up @@ -373,7 +376,7 @@ struct WindowState {
dw_style: u32,

#[cfg(feature = "opengl")]
gl_context: Arc<Option<GlContext>>,
gl_context: Arc<Mutex<Option<GlContext>>>,
}

impl WindowState {
Expand All @@ -392,7 +395,7 @@ pub struct Window {
hwnd: HWND,

#[cfg(feature = "opengl")]
gl_context: Arc<Option<GlContext>>,
gl_context: Arc<Mutex<Option<GlContext>>>,
}

impl Window {
Expand Down Expand Up @@ -511,13 +514,14 @@ impl Window {
// todo: manage error ^

#[cfg(feature = "opengl")]
let gl_context: Arc<Option<GlContext>> = Arc::new(options.gl_config.map(|gl_config| {
let mut handle = Win32Handle::empty();
handle.hwnd = hwnd as *mut c_void;
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Win32(handle) };
let gl_context: Arc<Mutex<Option<GlContext>>> =
Arc::new(Mutex::new(options.gl_config.map(|gl_config| {
let mut handle = Win32Handle::empty();
handle.hwnd = hwnd as *mut c_void;
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Win32(handle) };

GlContext::create(&handle, gl_config).expect("Could not create OpenGL context")
}));
GlContext::create(&handle, gl_config).expect("Could not create OpenGL context")
})));

#[cfg(not(feature = "opengl"))]
let handler = Box::new(build(&mut crate::Window::new(&mut Window { hwnd })));
Expand Down Expand Up @@ -608,8 +612,8 @@ impl Window {
}

#[cfg(feature = "opengl")]
pub fn gl_context(&self) -> Option<&GlContext> {
self.gl_context.as_ref().as_ref()
pub fn gl_context(&self) -> Option<GlContext> {
self.gl_context.lock().unwrap().take()
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ impl<'a> Window<'a> {
self.window.close();
}

/// If provided, then an OpenGL context will be created for this window. You'll be able to
/// access this context through [crate::Window::gl_context].
/// If the window has been created with [`WindowOpenOptions::gl_config`] set, then this will
/// contain the OpenGL context. Calling this function will take ownership of that OpenGL
/// context, so you can do this once after which you will need to store the context yourself.
#[cfg(feature = "opengl")]
pub fn gl_context(&self) -> Option<&crate::gl::GlContext> {
pub fn gl_context(&mut self) -> Option<crate::gl::GlContext> {
self.window.gl_context()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ impl Window {
}

#[cfg(feature = "opengl")]
pub fn gl_context(&self) -> Option<&crate::gl::GlContext> {
self.gl_context.as_ref()
pub fn gl_context(&mut self) -> Option<GlContext> {
self.gl_context.take()
}

fn find_visual_for_depth(screen: &StructPtr<xcb_screen_t>, depth: u8) -> Option<u32> {
Expand Down