Skip to content

Commit bb1108a

Browse files
committed
Fix windows OpenGL context creation
1 parent 67b67df commit bb1108a

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/gl/win.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ffi::{c_void, CString, OsStr};
22
use std::os::windows::ffi::OsStrExt;
33

4-
use raw_window_handle::RawWindowHandle;
4+
use raw_window_handle::{RawWindowHandle, Win32WindowHandle};
55

66
use winapi::shared::minwindef::{HINSTANCE, HMODULE};
77
use winapi::shared::ntdef::WCHAR;
@@ -77,13 +77,9 @@ extern "C" {
7777
}
7878

7979
impl GlContext {
80-
pub unsafe fn create(parent: &RawWindowHandle, config: GlConfig) -> Result<GlContext, GlError> {
81-
let handle = if let RawWindowHandle::Win32(handle) = parent {
82-
handle
83-
} else {
84-
return Err(GlError::InvalidWindowHandle);
85-
};
86-
80+
pub unsafe fn create(
81+
handle: &Win32WindowHandle, config: GlConfig,
82+
) -> Result<GlContext, GlError> {
8783
if handle.hwnd.is_null() {
8884
return Err(GlError::InvalidWindowHandle);
8985
}

src/win/win32_window.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::win::util::to_wstr;
22
use crate::{PhySize, Size, WindowInfo, WindowOpenOptions, WindowScalePolicy};
3+
use raw_window_handle::{RawWindowHandle, Win32WindowHandle};
34
use std::cell::Cell;
5+
use std::ffi::c_void;
46
use std::ptr::null_mut;
57
use winapi::shared::minwindef::{DWORD, UINT};
68
use winapi::shared::windef::{HWND, RECT};
@@ -23,6 +25,9 @@ pub(crate) struct Win32Window {
2325
scale_policy: WindowScalePolicy,
2426

2527
frame_timer_started: Cell<bool>,
28+
29+
#[cfg(feature = "opengl")]
30+
pub(crate) gl_context: Option<std::rc::Rc<crate::gl::win::GlContext>>,
2631
}
2732

2833
impl Win32Window {
@@ -77,13 +82,15 @@ impl Win32Window {
7782
};
7883

7984
// TODO: GL context
80-
let window = Self {
85+
let mut window = Self {
8186
_class: class,
8287
handle,
8388
style_flags,
8489
current_size: Cell::new(initial_size),
8590
scale_policy: options.scale,
8691
frame_timer_started: Cell::new(false),
92+
#[cfg(feature = "opengl")]
93+
gl_context: None,
8794
};
8895

8996
// FIXME: this should NOT be changed if the window is part of an host
@@ -96,6 +103,9 @@ impl Win32Window {
96103

97104
// Now we can get the actual dpi of the window.
98105
window.check_for_dpi_changes();
106+
107+
#[cfg(feature = "opengl")]
108+
window.create_gl_context(options);
99109
window.start_frame_timer();
100110

101111
window
@@ -107,6 +117,23 @@ impl Win32Window {
107117
dpi as f64 / 96.0
108118
}
109119

120+
pub fn raw_window_handle(&self) -> Win32WindowHandle {
121+
let mut handle = Win32WindowHandle::empty();
122+
handle.hwnd = self.handle() as *mut c_void;
123+
handle
124+
}
125+
126+
#[cfg(feature = "opengl")]
127+
fn create_gl_context(&mut self, options: &WindowOpenOptions) {
128+
self.gl_context = options.gl_config.as_ref().map(|gl_config| {
129+
let ctx =
130+
// SAFETY: TODO
131+
unsafe { crate::gl::win::GlContext::create(&self.raw_window_handle(), gl_config.clone()) }
132+
.expect("Could not create OpenGL context");
133+
std::rc::Rc::new(ctx)
134+
});
135+
}
136+
110137
fn resize(&self, size: PhySize) {
111138
let window_size = client_size_to_window_size(size, self.style_flags);
112139

src/win/window.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ use winapi::um::ole2::OleInitialize;
33

44
use std::cell::RefCell;
55
use std::collections::VecDeque;
6-
use std::ffi::c_void;
76
use std::ptr::null_mut;
87
use std::rc::Rc;
98

109
use raw_window_handle::{
11-
HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, Win32WindowHandle, WindowsDisplayHandle,
10+
HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, WindowsDisplayHandle,
1211
};
1312

1413
#[cfg(feature = "opengl")]
15-
use crate::gl::GlContext;
14+
use crate::gl::win::GlContext;
1615
use crate::win::handle::{WindowHandle, WindowHandleTransmitter};
1716
use crate::win::proc::ProcState;
1817
use crate::win::win32_window::Win32Window;
@@ -104,15 +103,12 @@ impl Window {
104103
}
105104

106105
#[cfg(feature = "opengl")]
107-
pub fn gl_context(&self) -> Option<&GlContext> {
108-
self.state.gl_context.as_ref()
106+
pub fn gl_context(&self) -> Option<std::rc::Weak<GlContext>> {
107+
self.win32_window.gl_context.as_ref().map(Rc::downgrade)
109108
}
110109

111110
pub fn raw_window_handle(&self) -> RawWindowHandle {
112-
let mut handle = Win32WindowHandle::empty();
113-
handle.hwnd = self.win32_window.handle() as *mut c_void;
114-
115-
handle.into()
111+
self.win32_window.raw_window_handle().into()
116112
}
117113
pub fn raw_display_handle(&self) -> RawDisplayHandle {
118114
WindowsDisplayHandle::empty().into()

0 commit comments

Comments
 (0)