Skip to content

Commit 0f1b835

Browse files
committed
Patch up the raw-gl-context modules for a feature
This only needed a couple changes for the raw-window-handle migration, and for the different module paths.
1 parent 45e29b4 commit 0f1b835

6 files changed

Lines changed: 28 additions & 47 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ authors = [
1414
edition = "2018"
1515
license = "MIT OR Apache-2.0"
1616

17+
[features]
18+
default = ["opengl"]
19+
opengl = ["uuid"]
20+
1721
[dependencies]
1822
keyboard-types = { version = "0.6.1", default-features = false }
1923
raw-window-handle = "0.4.2"
@@ -26,6 +30,7 @@ nix = "0.22.0"
2630

2731
[target.'cfg(target_os="windows")'.dependencies]
2832
winapi = { version = "0.3.8", features = ["libloaderapi", "winuser", "windef", "minwindef", "guiddef", "combaseapi", "wingdi", "errhandlingapi"] }
33+
uuid = { version = "0.8", features = ["v4"], optional = true }
2934

3035
[target.'cfg(target_os="macos")'.dependencies]
3136
cocoa = "0.24.0"

src/gl/macos.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use core_foundation::string::CFString;
1818

1919
use objc::{msg_send, sel, sel_impl};
2020

21-
use crate::{GlConfig, GlError, Profile};
21+
use super::{GlConfig, GlError, Profile};
2222

2323
pub type CreationFailedError = ();
2424
pub struct GlContext {
@@ -28,10 +28,9 @@ pub struct GlContext {
2828

2929
impl GlContext {
3030
pub unsafe fn create(
31-
parent: &impl HasRawWindowHandle,
32-
config: GlConfig,
31+
parent: &impl HasRawWindowHandle, config: GlConfig,
3332
) -> Result<GlContext, GlError> {
34-
let handle = if let RawWindowHandle::MacOS(handle) = parent.raw_window_handle() {
33+
let handle = if let RawWindowHandle::AppKit(handle) = parent.raw_window_handle() {
3534
handle
3635
} else {
3736
return Err(GlError::InvalidWindowHandle);

src/gl/lib.rs renamed to src/gl/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use win as platform;
1111
#[cfg(target_os = "linux")]
1212
mod x11;
1313
#[cfg(target_os = "linux")]
14-
use crate::x11 as platform;
14+
use self::x11 as platform;
1515

1616
#[cfg(target_os = "macos")]
1717
mod macos;
@@ -73,13 +73,10 @@ pub struct GlContext {
7373

7474
impl GlContext {
7575
pub unsafe fn create(
76-
parent: &impl HasRawWindowHandle,
77-
config: GlConfig,
76+
parent: &impl HasRawWindowHandle, config: GlConfig,
7877
) -> Result<GlContext, GlError> {
79-
platform::GlContext::create(parent, config).map(|context| GlContext {
80-
context,
81-
phantom: PhantomData,
82-
})
78+
platform::GlContext::create(parent, config)
79+
.map(|context| GlContext { context, phantom: PhantomData })
8380
}
8481

8582
pub unsafe fn make_current(&self) {

src/gl/win.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use winapi::um::winuser::{
1818
UnregisterClassW, CS_OWNDC, CW_USEDEFAULT, WNDCLASSW,
1919
};
2020

21-
use crate::{GlConfig, GlError, Profile};
21+
use super::{GlConfig, GlError, Profile};
2222

2323
// See https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_create_context.txt
2424

@@ -78,10 +78,9 @@ extern "C" {
7878

7979
impl GlContext {
8080
pub unsafe fn create(
81-
parent: &impl HasRawWindowHandle,
82-
config: GlConfig,
81+
parent: &impl HasRawWindowHandle, config: GlConfig,
8382
) -> Result<GlContext, GlError> {
84-
let handle = if let RawWindowHandle::Windows(handle) = parent.raw_window_handle() {
83+
let handle = if let RawWindowHandle::Win32(handle) = parent.raw_window_handle() {
8584
handle
8685
} else {
8786
return Err(GlError::InvalidWindowHandle);
@@ -267,12 +266,7 @@ impl GlContext {
267266
wglSwapIntervalEXT.unwrap()(config.vsync as i32);
268267
wglMakeCurrent(hdc, std::ptr::null_mut());
269268

270-
Ok(GlContext {
271-
hwnd,
272-
hdc,
273-
hglrc,
274-
gl_library,
275-
})
269+
Ok(GlContext { hwnd, hdc, hglrc, gl_library })
276270
}
277271

278272
pub unsafe fn make_current(&self) {

src/gl/x11.rs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
66
use x11::glx;
77
use x11::xlib;
88

9-
use crate::{GlConfig, GlError, Profile};
9+
use super::{GlConfig, GlError, Profile};
1010

1111
mod errors;
1212

@@ -57,8 +57,7 @@ pub struct GlContext {
5757

5858
impl GlContext {
5959
pub unsafe fn create(
60-
parent: &impl HasRawWindowHandle,
61-
config: GlConfig,
60+
parent: &impl HasRawWindowHandle, config: GlConfig,
6261
) -> Result<GlContext, GlError> {
6362
let handle = if let RawWindowHandle::Xlib(handle) = parent.raw_window_handle() {
6463
handle
@@ -102,18 +101,14 @@ impl GlContext {
102101
error_handler.check()?;
103102

104103
if n_configs <= 0 {
105-
return Err(GlError::CreationFailed(
106-
CreationFailedError::InvalidFBConfig,
107-
));
104+
return Err(GlError::CreationFailed(CreationFailedError::InvalidFBConfig));
108105
}
109106

110107
#[allow(non_snake_case)]
111108
let glXCreateContextAttribsARB: GlXCreateContextAttribsARB = unsafe {
112109
let addr = get_proc_address("glXCreateContextAttribsARB");
113110
if addr.is_null() {
114-
return Err(GlError::CreationFailed(
115-
CreationFailedError::GetProcAddressFailed,
116-
));
111+
return Err(GlError::CreationFailed(CreationFailedError::GetProcAddressFailed));
117112
} else {
118113
std::mem::transmute(addr)
119114
}
@@ -123,9 +118,7 @@ impl GlContext {
123118
let glXSwapIntervalEXT: GlXSwapIntervalEXT = unsafe {
124119
let addr = get_proc_address("glXSwapIntervalEXT");
125120
if addr.is_null() {
126-
return Err(GlError::CreationFailed(
127-
CreationFailedError::GetProcAddressFailed,
128-
));
121+
return Err(GlError::CreationFailed(CreationFailedError::GetProcAddressFailed));
129122
} else {
130123
std::mem::transmute(addr)
131124
}
@@ -159,36 +152,26 @@ impl GlContext {
159152
error_handler.check()?;
160153

161154
if context.is_null() {
162-
return Err(GlError::CreationFailed(
163-
CreationFailedError::ContextCreationFailed,
164-
));
155+
return Err(GlError::CreationFailed(CreationFailedError::ContextCreationFailed));
165156
}
166157

167158
unsafe {
168159
let res = glx::glXMakeCurrent(display, handle.window, context);
169160
error_handler.check()?;
170161
if res == 0 {
171-
return Err(GlError::CreationFailed(
172-
CreationFailedError::MakeCurrentFailed,
173-
));
162+
return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed));
174163
}
175164

176165
glXSwapIntervalEXT(display, handle.window, config.vsync as i32);
177166
error_handler.check()?;
178167

179168
if glx::glXMakeCurrent(display, 0, std::ptr::null_mut()) == 0 {
180169
error_handler.check()?;
181-
return Err(GlError::CreationFailed(
182-
CreationFailedError::MakeCurrentFailed,
183-
));
170+
return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed));
184171
}
185172
}
186173

187-
Ok(GlContext {
188-
window: handle.window,
189-
display,
190-
context,
191-
})
174+
Ok(GlContext { window: handle.window, display, context })
192175
})
193176
}
194177

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ mod window;
1212
mod window_info;
1313
mod window_open_options;
1414

15+
#[cfg(feature = "opengl")]
16+
pub mod gl;
17+
1518
pub use event::*;
1619
pub use mouse_cursor::MouseCursor;
1720
pub use window::*;

0 commit comments

Comments
 (0)