aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/win32/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/win32/mod.rs')
-rw-r--r--src/api/win32/mod.rs79
1 files changed, 38 insertions, 41 deletions
diff --git a/src/api/win32/mod.rs b/src/api/win32/mod.rs
index d0caea7..af339c5 100644
--- a/src/api/win32/mod.rs
+++ b/src/api/win32/mod.rs
@@ -3,7 +3,6 @@
use std::sync::atomic::AtomicBool;
use std::mem;
use std::ptr;
-use std::ffi::CString;
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::sync::{
@@ -25,13 +24,16 @@ pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
use winapi;
use user32;
use kernel32;
-use gdi32;
+
+use api::wgl;
+use api::wgl::Context as WglContext;
+use api::egl::Context as EglContext;
+
+use self::init::RawContext;
mod callback;
mod event;
-mod gl;
mod init;
-mod make_current_guard;
mod monitor;
/// The Win32 implementation of the main `Window` object.
@@ -40,13 +42,7 @@ pub struct Window {
window: WindowWrapper,
/// OpenGL context.
- context: ContextWrapper,
-
- /// Binded to `opengl32.dll`.
- ///
- /// `wglGetProcAddress` returns null for GL 1.1 functions because they are
- /// already defined by the system. This module contains them.
- gl_library: winapi::HMODULE,
+ context: Context,
/// Receiver for the events dispatched by the window callback.
events_receiver: Receiver<Event>,
@@ -56,25 +52,14 @@ pub struct Window {
/// The current cursor state.
cursor_state: Arc<Mutex<CursorState>>,
-
- /// The pixel format that has been used to create this window.
- pixel_format: PixelFormat,
}
unsafe impl Send for Window {}
unsafe impl Sync for Window {}
-/// A simple wrapper that destroys the context when it is destroyed.
-// FIXME: remove `pub` (https://github.com/rust-lang/rust/issues/23585)
-#[doc(hidden)]
-pub struct ContextWrapper(pub winapi::HGLRC);
-
-impl Drop for ContextWrapper {
- fn drop(&mut self) {
- unsafe {
- gl::wgl::DeleteContext(self.0 as *const libc::c_void);
- }
- }
+enum Context {
+ Egl(EglContext),
+ Wgl(WglContext),
}
/// A simple wrapper that destroys the window when it is destroyed.
@@ -103,7 +88,12 @@ impl Window {
/// See the docs in the crate root file.
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
let (builder, sharing) = builder.extract_non_static();
- let sharing = sharing.map(|w| init::ContextHack(w.context.0));
+
+ let sharing = sharing.map(|w| match w.context {
+ Context::Wgl(ref c) => RawContext::Wgl(c.get_hglrc()),
+ Context::Egl(_) => unimplemented!(), // FIXME:
+ });
+
init::new_window(builder, sharing)
}
@@ -326,38 +316,45 @@ impl Window {
impl GlContext for Window {
unsafe fn make_current(&self) {
- // TODO: check return value
- gl::wgl::MakeCurrent(self.window.1 as *const libc::c_void,
- self.context.0 as *const libc::c_void);
+ match self.context {
+ Context::Wgl(ref c) => c.make_current(),
+ Context::Egl(ref c) => c.make_current(),
+ }
}
fn is_current(&self) -> bool {
- unsafe { gl::wgl::GetCurrentContext() == self.context.0 as *const libc::c_void }
+ match self.context {
+ Context::Wgl(ref c) => c.is_current(),
+ Context::Egl(ref c) => c.is_current(),
+ }
}
fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
- let addr = CString::new(addr.as_bytes()).unwrap();
- let addr = addr.as_ptr();
-
- unsafe {
- let p = gl::wgl::GetProcAddress(addr) as *const _;
- if !p.is_null() { return p; }
- kernel32::GetProcAddress(self.gl_library, addr) as *const _
+ match self.context {
+ Context::Wgl(ref c) => c.get_proc_address(addr),
+ Context::Egl(ref c) => c.get_proc_address(addr),
}
}
fn swap_buffers(&self) {
- unsafe {
- gdi32::SwapBuffers(self.window.1);
+ match self.context {
+ Context::Wgl(ref c) => c.swap_buffers(),
+ Context::Egl(ref c) => c.swap_buffers(),
}
}
fn get_api(&self) -> Api {
- Api::OpenGl
+ match self.context {
+ Context::Wgl(ref c) => c.get_api(),
+ Context::Egl(ref c) => c.get_api(),
+ }
}
fn get_pixel_format(&self) -> PixelFormat {
- self.pixel_format.clone()
+ match self.context {
+ Context::Wgl(ref c) => c.get_pixel_format(),
+ Context::Egl(ref c) => c.get_pixel_format(),
+ }
}
}