11use crate :: win:: util:: to_wstr;
22use crate :: { PhySize , Size , WindowInfo , WindowOpenOptions , WindowScalePolicy } ;
3+ use raw_window_handle:: { RawWindowHandle , Win32WindowHandle } ;
34use std:: cell:: Cell ;
5+ use std:: ffi:: c_void;
46use std:: ptr:: null_mut;
57use winapi:: shared:: minwindef:: { DWORD , UINT } ;
68use 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
2833impl 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
0 commit comments