Skip to content

Commit d835175

Browse files
committed
Implement the OpenGL context on macOS
1 parent fe107ab commit d835175

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/macos/window.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::ffi::c_void;
22
use std::marker::PhantomData;
3+
use std::ptr;
34
use std::sync::atomic::{AtomicBool, Ordering};
45
use std::sync::Arc;
56

@@ -27,7 +28,10 @@ use super::keyboard::KeyboardState;
2728
use super::view::{create_view, BASEVIEW_STATE_IVAR};
2829

2930
#[cfg(feature = "opengl")]
30-
use crate::gl::{GlConfig, GlContext};
31+
use crate::{
32+
gl::{GlConfig, GlContext},
33+
window::RawWindowHandleWrapper,
34+
};
3135

3236
pub struct WindowHandle {
3337
raw_window_handle: Option<RawWindowHandle>,
@@ -135,7 +139,9 @@ impl Window {
135139
close_requested: false,
136140

137141
#[cfg(feature = "opengl")]
138-
gl_context: options.gl_config.map(Self::create_gl_context),
142+
gl_context: options
143+
.gl_config
144+
.map(|gl_config| Self::create_gl_context(None, ns_view, gl_config)),
139145
};
140146

141147
let window_handle = Self::init(true, window, build);
@@ -167,7 +173,9 @@ impl Window {
167173
close_requested: false,
168174

169175
#[cfg(feature = "opengl")]
170-
gl_context: options.gl_config.map(Self::create_gl_context),
176+
gl_context: options
177+
.gl_config
178+
.map(|gl_config| Self::create_gl_context(None, ns_view, gl_config)),
171179
};
172180

173181
let window_handle = Self::init(true, window, build);
@@ -241,7 +249,9 @@ impl Window {
241249
close_requested: false,
242250

243251
#[cfg(feature = "opengl")]
244-
gl_context: options.gl_config.map(Self::create_gl_context),
252+
gl_context: options
253+
.gl_config
254+
.map(|gl_config| Self::create_gl_context(Some(ns_window), ns_view, gl_config)),
245255
};
246256

247257
let _ = Self::init(false, window, build);
@@ -298,8 +308,13 @@ impl Window {
298308
}
299309

300310
#[cfg(feature = "opengl")]
301-
fn create_gl_context(config: GlConfig) -> GlContext {
302-
todo!("Create the macOS OpenGL context");
311+
fn create_gl_context(ns_window: Option<id>, ns_view: id, config: GlConfig) -> GlContext {
312+
let mut handle = AppKitHandle::empty();
313+
handle.ns_window = ns_window.unwrap_or(ptr::null_mut()) as *mut c_void;
314+
handle.ns_view = ns_view as *mut c_void;
315+
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::AppKit(handle) };
316+
317+
unsafe { GlContext::create(&handle, config).expect("Could not create OpenGL context") }
303318
}
304319
}
305320

@@ -408,7 +423,7 @@ impl WindowState {
408423
// Clear ivar before triggering WindowEvent::WillClose. Otherwise, if the
409424
// handler of the event causes another call to release, this function could be
410425
// called again, leading to a double free.
411-
ns_view_obj.set_ivar(BASEVIEW_STATE_IVAR, ::std::ptr::null() as *const c_void);
426+
ns_view_obj.set_ivar(BASEVIEW_STATE_IVAR, ptr::null() as *const c_void);
412427

413428
window_state.trigger_event(Event::Window(WindowEvent::WillClose));
414429

@@ -421,7 +436,7 @@ impl WindowState {
421436

422437
unsafe impl HasRawWindowHandle for Window {
423438
fn raw_window_handle(&self) -> RawWindowHandle {
424-
let ns_window = self.ns_window.unwrap_or(::std::ptr::null_mut()) as *mut c_void;
439+
let ns_window = self.ns_window.unwrap_or(ptr::null_mut()) as *mut c_void;
425440

426441
let mut handle = AppKitHandle::empty();
427442
handle.ns_window = ns_window;

0 commit comments

Comments
 (0)