From 0f1b8353d03a47648b162f6dc0edea74bac64a46 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Mon, 7 Feb 2022 17:31:19 +0100 Subject: [PATCH] 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. --- Cargo.toml | 5 +++++ src/gl/macos.rs | 7 +++---- src/gl/{lib.rs => mod.rs} | 11 ++++------- src/gl/win.rs | 14 ++++---------- src/gl/x11.rs | 35 +++++++++-------------------------- src/lib.rs | 3 +++ 6 files changed, 28 insertions(+), 47 deletions(-) rename src/gl/{lib.rs => mod.rs} (88%) diff --git a/Cargo.toml b/Cargo.toml index f45bdc3..cee5ab5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,10 @@ authors = [ edition = "2018" license = "MIT OR Apache-2.0" +[features] +default = ["opengl"] +opengl = ["uuid"] + [dependencies] keyboard-types = { version = "0.6.1", default-features = false } raw-window-handle = "0.4.2" @@ -26,6 +30,7 @@ nix = "0.22.0" [target.'cfg(target_os="windows")'.dependencies] winapi = { version = "0.3.8", features = ["libloaderapi", "winuser", "windef", "minwindef", "guiddef", "combaseapi", "wingdi", "errhandlingapi"] } +uuid = { version = "0.8", features = ["v4"], optional = true } [target.'cfg(target_os="macos")'.dependencies] cocoa = "0.24.0" diff --git a/src/gl/macos.rs b/src/gl/macos.rs index 28f56c1..0610879 100644 --- a/src/gl/macos.rs +++ b/src/gl/macos.rs @@ -18,7 +18,7 @@ use core_foundation::string::CFString; use objc::{msg_send, sel, sel_impl}; -use crate::{GlConfig, GlError, Profile}; +use super::{GlConfig, GlError, Profile}; pub type CreationFailedError = (); pub struct GlContext { @@ -28,10 +28,9 @@ pub struct GlContext { impl GlContext { pub unsafe fn create( - parent: &impl HasRawWindowHandle, - config: GlConfig, + parent: &impl HasRawWindowHandle, config: GlConfig, ) -> Result { - let handle = if let RawWindowHandle::MacOS(handle) = parent.raw_window_handle() { + let handle = if let RawWindowHandle::AppKit(handle) = parent.raw_window_handle() { handle } else { return Err(GlError::InvalidWindowHandle); diff --git a/src/gl/lib.rs b/src/gl/mod.rs similarity index 88% rename from src/gl/lib.rs rename to src/gl/mod.rs index 9c43878..5af8634 100644 --- a/src/gl/lib.rs +++ b/src/gl/mod.rs @@ -11,7 +11,7 @@ use win as platform; #[cfg(target_os = "linux")] mod x11; #[cfg(target_os = "linux")] -use crate::x11 as platform; +use self::x11 as platform; #[cfg(target_os = "macos")] mod macos; @@ -73,13 +73,10 @@ pub struct GlContext { impl GlContext { pub unsafe fn create( - parent: &impl HasRawWindowHandle, - config: GlConfig, + parent: &impl HasRawWindowHandle, config: GlConfig, ) -> Result { - platform::GlContext::create(parent, config).map(|context| GlContext { - context, - phantom: PhantomData, - }) + platform::GlContext::create(parent, config) + .map(|context| GlContext { context, phantom: PhantomData }) } pub unsafe fn make_current(&self) { diff --git a/src/gl/win.rs b/src/gl/win.rs index 5594ae9..57982c9 100644 --- a/src/gl/win.rs +++ b/src/gl/win.rs @@ -18,7 +18,7 @@ use winapi::um::winuser::{ UnregisterClassW, CS_OWNDC, CW_USEDEFAULT, WNDCLASSW, }; -use crate::{GlConfig, GlError, Profile}; +use super::{GlConfig, GlError, Profile}; // See https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_create_context.txt @@ -78,10 +78,9 @@ extern "C" { impl GlContext { pub unsafe fn create( - parent: &impl HasRawWindowHandle, - config: GlConfig, + parent: &impl HasRawWindowHandle, config: GlConfig, ) -> Result { - let handle = if let RawWindowHandle::Windows(handle) = parent.raw_window_handle() { + let handle = if let RawWindowHandle::Win32(handle) = parent.raw_window_handle() { handle } else { return Err(GlError::InvalidWindowHandle); @@ -267,12 +266,7 @@ impl GlContext { wglSwapIntervalEXT.unwrap()(config.vsync as i32); wglMakeCurrent(hdc, std::ptr::null_mut()); - Ok(GlContext { - hwnd, - hdc, - hglrc, - gl_library, - }) + Ok(GlContext { hwnd, hdc, hglrc, gl_library }) } pub unsafe fn make_current(&self) { diff --git a/src/gl/x11.rs b/src/gl/x11.rs index ced977d..b31bffc 100644 --- a/src/gl/x11.rs +++ b/src/gl/x11.rs @@ -6,7 +6,7 @@ use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; use x11::glx; use x11::xlib; -use crate::{GlConfig, GlError, Profile}; +use super::{GlConfig, GlError, Profile}; mod errors; @@ -57,8 +57,7 @@ pub struct GlContext { impl GlContext { pub unsafe fn create( - parent: &impl HasRawWindowHandle, - config: GlConfig, + parent: &impl HasRawWindowHandle, config: GlConfig, ) -> Result { let handle = if let RawWindowHandle::Xlib(handle) = parent.raw_window_handle() { handle @@ -102,18 +101,14 @@ impl GlContext { error_handler.check()?; if n_configs <= 0 { - return Err(GlError::CreationFailed( - CreationFailedError::InvalidFBConfig, - )); + return Err(GlError::CreationFailed(CreationFailedError::InvalidFBConfig)); } #[allow(non_snake_case)] let glXCreateContextAttribsARB: GlXCreateContextAttribsARB = unsafe { let addr = get_proc_address("glXCreateContextAttribsARB"); if addr.is_null() { - return Err(GlError::CreationFailed( - CreationFailedError::GetProcAddressFailed, - )); + return Err(GlError::CreationFailed(CreationFailedError::GetProcAddressFailed)); } else { std::mem::transmute(addr) } @@ -123,9 +118,7 @@ impl GlContext { let glXSwapIntervalEXT: GlXSwapIntervalEXT = unsafe { let addr = get_proc_address("glXSwapIntervalEXT"); if addr.is_null() { - return Err(GlError::CreationFailed( - CreationFailedError::GetProcAddressFailed, - )); + return Err(GlError::CreationFailed(CreationFailedError::GetProcAddressFailed)); } else { std::mem::transmute(addr) } @@ -159,18 +152,14 @@ impl GlContext { error_handler.check()?; if context.is_null() { - return Err(GlError::CreationFailed( - CreationFailedError::ContextCreationFailed, - )); + return Err(GlError::CreationFailed(CreationFailedError::ContextCreationFailed)); } unsafe { let res = glx::glXMakeCurrent(display, handle.window, context); error_handler.check()?; if res == 0 { - return Err(GlError::CreationFailed( - CreationFailedError::MakeCurrentFailed, - )); + return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed)); } glXSwapIntervalEXT(display, handle.window, config.vsync as i32); @@ -178,17 +167,11 @@ impl GlContext { if glx::glXMakeCurrent(display, 0, std::ptr::null_mut()) == 0 { error_handler.check()?; - return Err(GlError::CreationFailed( - CreationFailedError::MakeCurrentFailed, - )); + return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed)); } } - Ok(GlContext { - window: handle.window, - display, - context, - }) + Ok(GlContext { window: handle.window, display, context }) }) } diff --git a/src/lib.rs b/src/lib.rs index 5a61586..46a3695 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,9 @@ mod window; mod window_info; mod window_open_options; +#[cfg(feature = "opengl")] +pub mod gl; + pub use event::*; pub use mouse_cursor::MouseCursor; pub use window::*;