From 52cccb8ddadbfff338b627d4f8c654840be1840b Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Sat, 2 Aug 2014 20:49:48 +0200 Subject: [PATCH 01/11] Add doc to crate root --- src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index e238b15e..f38a4b8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,21 @@ #![feature(globs)] #![unstable] +//! The purpose of this library is to provide an OpenGL context on as many +//! platforms as possible. +//! +//! # Building a window +//! +//! There are two ways to create a window: +//! +//! - Calling `Window::new()`. +//! - Calling `let builder = WindowBuilder::new()` then `builder.build()`. +//! +//! The first way is the simpliest way and will give you default values. +//! +//! The second way allows you to customize the way your window and GL context +//! will look and behave. + extern crate libc; pub use events::*; From f4fb699b5242d13482a3c1931e71a52c79a7e535 Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Sun, 3 Aug 2014 09:25:30 +0200 Subject: [PATCH 02/11] Add dummy implementation for OS/X --- .travis.yml | 4 +++ src/lib.rs | 6 ++-- src/osx/mod.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/osx/mod.rs diff --git a/.travis.yml b/.travis.yml index 22761ba7..eab930b1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1 +1,5 @@ language: rust + +os: + - linux + - osx diff --git a/src/lib.rs b/src/lib.rs index f38a4b8a..914dedc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,10 +26,12 @@ use winimpl = win32; #[cfg(unix)] use winimpl = x11; -#[cfg(windows)] +#[cfg(target_os = "win32")] mod win32; -#[cfg(unix)] +#[cfg(target_os = "linux")] mod x11; +#[cfg(target_os = "macos")] +mod osx; #[allow(dead_code)] //mod egl; diff --git a/src/osx/mod.rs b/src/osx/mod.rs new file mode 100644 index 00000000..dcf714b4 --- /dev/null +++ b/src/osx/mod.rs @@ -0,0 +1,79 @@ +//! Dummy implementation for OS/X to make gl-init-rs compile on this platform + +use WindowBuilder; + +pub struct Window; + +pub struct MonitorID; + +pub fn get_available_monitors() -> Vec { + unimplemented!() +} + +pub fn get_primary_monitor() -> MonitorID { + unimplemented!() +} + +impl MonitorID { + pub fn get_name(&self) -> Option { + unimplemented!() + } + + pub fn get_dimensions(&self) -> (uint, uint) { + unimplemented!() + } +} + +impl Window { + pub fn new(_builder: WindowBuilder) -> Result { + unimplemented!() + } + + pub fn is_closed(&self) -> bool { + unimplemented!() + } + + pub fn set_title(&self, _title: &str) { + unimplemented!() + } + + pub fn get_position(&self) -> Option<(int, int)> { + unimplemented!() + } + + pub fn set_position(&self, _x: uint, _y: uint) { + unimplemented!() + } + + pub fn get_inner_size(&self) -> Option<(uint, uint)> { + unimplemented!() + } + + pub fn get_outer_size(&self) -> Option<(uint, uint)> { + unimplemented!() + } + + pub fn set_inner_size(&self, _x: uint, _y: uint) { + unimplemented!() + } + + pub fn poll_events(&self) -> Vec { + unimplemented!() + } + + pub fn wait_events(&self) -> Vec { + unimplemented!() + } + + pub unsafe fn make_current(&self) { + unimplemented!() + } + + pub fn get_proc_address(&self, _addr: &str) -> *const () { + unimplemented!() + } + + pub fn swap_buffers(&self) { + unimplemented!() + } +} From 48632619c9e6e8d7a36382285fc505cfc591dd20 Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Sun, 3 Aug 2014 10:30:04 +0200 Subject: [PATCH 03/11] Add static assert for platform not supported --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 914dedc0..ee79fd95 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,10 @@ mod osx; mod events; +#[cfg(not(target_os = "win32"), not(target_os = "linux"), not(target_os = "macos"))] +#[static_assert] +static this_platform_is_not_supposed: bool = false; + /// Identifier for a monitor. pub struct MonitorID(winimpl::MonitorID); From 3ea19e0fa2c0e2b80bb556c587d4e8b7a7cf7870 Mon Sep 17 00:00:00 2001 From: tomaka Date: Sun, 3 Aug 2014 11:43:57 +0200 Subject: [PATCH 04/11] Update README.md --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 9ea0ec04..5972f998 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,6 @@ extern crate libc; extern crate gl; fn main() { - use std::default::Default; - let window = init::Window::new().unwrap(); unsafe { window.make_current() }; @@ -29,7 +27,7 @@ fn main() { gl::ClearColor(0.0, 1.0, 0.0, 1.0); - while !window.should_close() { + while !window.is_closed() { window.wait_events(); gl::Clear(gl::COLOR_BUFFER_BIT); From c0912c9ec4b3080fbff6dc1debd216dbe9fe93e8 Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Sun, 3 Aug 2014 17:23:08 +0200 Subject: [PATCH 05/11] Add example for multiple simultaneous examples --- examples/multiwindow.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 examples/multiwindow.rs diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs new file mode 100644 index 00000000..eeaa7d60 --- /dev/null +++ b/examples/multiwindow.rs @@ -0,0 +1,32 @@ +extern crate init = "gl-init-rs"; +extern crate libc; +extern crate gl; + +fn main() { + let window1 = init::Window::new().unwrap(); + let window2 = init::Window::new().unwrap(); + + spawn(proc() { + run(window1, (0.0, 1.0, 0.0, 1.0)); + }); + + spawn(proc() { + run(window2, (0.0, 0.0, 1.0, 1.0)); + }); +} + +fn run(window: init::Window, color: (f32, f32, f32, f32)) { + unsafe { window.make_current() }; + + gl::load_with(|symbol| window.get_proc_address(symbol) as *const libc::c_void); + + gl::ClearColor(color.val0(), color.val1(), color.val2(), color.val3()); + + while !window.is_closed() { + window.wait_events().collect::>(); + + gl::Clear(gl::COLOR_BUFFER_BIT); + + window.swap_buffers(); + } +} From 3c9565ad25d1bd86e307964811ceaef5de9c1324 Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Sun, 3 Aug 2014 17:27:48 +0200 Subject: [PATCH 06/11] Fix closed flag not being immediatly set on win32 --- src/win32/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/win32/mod.rs b/src/win32/mod.rs index 891c7835..dbcbd8a0 100644 --- a/src/win32/mod.rs +++ b/src/win32/mod.rs @@ -133,6 +133,7 @@ impl Window { } } + // if one of the received events is `Closed`, setting `is_closed` to true if events.iter().find(|e| match e { &&::Closed => true, _ => false }).is_some() { use std::sync::atomics::Relaxed; self.is_closed.store(true, Relaxed); @@ -146,10 +147,21 @@ impl Window { pub fn wait_events(&self) -> Vec { match self.events_receiver.recv_opt() { Ok(ev) => { + // if the received event is `Closed`, setting `is_closed` to true + match ev { + ::Closed => { + use std::sync::atomics::Relaxed; + self.is_closed.store(true, Relaxed); + }, + _ => () + }; + + // looing for other possible events in the queue let mut result = self.poll_events(); result.insert(0, ev); result }, + Err(_) => { use std::sync::atomics::Relaxed; self.is_closed.store(true, Relaxed); From d574f6f1bbcdcc93649fe9daa18106a3cae61ef6 Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Sun, 3 Aug 2014 17:33:48 +0200 Subject: [PATCH 07/11] Fix GL context not being destroyed on win32 --- src/win32/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/win32/mod.rs b/src/win32/mod.rs index dbcbd8a0..f29bf89b 100644 --- a/src/win32/mod.rs +++ b/src/win32/mod.rs @@ -199,6 +199,9 @@ impl Window { #[unsafe_destructor] impl Drop for Window { fn drop(&mut self) { + use std::ptr; + unsafe { ffi::wglMakeCurrent(ptr::mut_null(), ptr::mut_null()); } + unsafe { ffi::wglDeleteContext(self.context); } unsafe { ffi::DestroyWindow(self.window); } } } From f1993be9eae49ad9a1d5e8e8d2b3749ad4379a70 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Sun, 3 Aug 2014 18:30:31 +0200 Subject: [PATCH 08/11] Fix bugs from f4fb699b524 --- src/lib.rs | 4 +++- src/osx/mod.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ee79fd95..60ce0aa2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,8 +23,10 @@ pub use events::*; #[cfg(windows)] use winimpl = win32; -#[cfg(unix)] +#[cfg(target_os = "linux")] use winimpl = x11; +#[cfg(target_os = "macos")] +use winimpl = osx; #[cfg(target_os = "win32")] mod win32; diff --git a/src/osx/mod.rs b/src/osx/mod.rs index dcf714b4..f846cf4b 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -1,6 +1,6 @@ //! Dummy implementation for OS/X to make gl-init-rs compile on this platform -use WindowBuilder; +use {Event, WindowBuilder}; pub struct Window; From 92f90220a485caf71947c6bc49e134f555f10235 Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Thu, 7 Aug 2014 08:53:21 +0200 Subject: [PATCH 09/11] Fix events handling in examples --- examples/fullscreen.rs | 5 ++--- examples/multiwindow.rs | 5 ++--- examples/window.rs | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs index e5f8fd4f..f05bf9dd 100644 --- a/examples/fullscreen.rs +++ b/examples/fullscreen.rs @@ -41,10 +41,9 @@ fn main() { gl::ClearColor(0.0, 1.0, 0.0, 1.0); while !window.is_closed() { - println!("{}", window.wait_events().collect::>()); - gl::Clear(gl::COLOR_BUFFER_BIT); - window.swap_buffers(); + + println!("{}", window.wait_events().collect::>()); } } diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs index eeaa7d60..2ae9c318 100644 --- a/examples/multiwindow.rs +++ b/examples/multiwindow.rs @@ -23,10 +23,9 @@ fn run(window: init::Window, color: (f32, f32, f32, f32)) { gl::ClearColor(color.val0(), color.val1(), color.val2(), color.val3()); while !window.is_closed() { - window.wait_events().collect::>(); - gl::Clear(gl::COLOR_BUFFER_BIT); - window.swap_buffers(); + + window.wait_events().collect::>(); } } diff --git a/examples/window.rs b/examples/window.rs index c366ee0a..07f5a84a 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -19,10 +19,9 @@ fn main() { gl::ClearColor(0.0, 1.0, 0.0, 1.0); while !window.is_closed() { - println!("{}", window.wait_events().collect::>()); - gl::Clear(gl::COLOR_BUFFER_BIT); - window.swap_buffers(); + + println!("{}", window.wait_events().collect::>()); } } From 2fdcc1840ba7036bae3f69bc96e2791d6c566291 Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Thu, 7 Aug 2014 09:32:13 +0200 Subject: [PATCH 10/11] Examples now call glViewport --- examples/fullscreen.rs | 7 ++++++- examples/multiwindow.rs | 7 ++++++- examples/window.rs | 5 +++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs index f05bf9dd..5cb71f74 100644 --- a/examples/fullscreen.rs +++ b/examples/fullscreen.rs @@ -37,13 +37,18 @@ fn main() { }; println!("OpenGL version {}", version.as_str().unwrap()); + + { + let win_size = window.get_inner_size().unwrap(); + gl::Viewport(0, 0, win_size.val0() as libc::c_int, win_size.val1() as libc::c_int); + } gl::ClearColor(0.0, 1.0, 0.0, 1.0); while !window.is_closed() { gl::Clear(gl::COLOR_BUFFER_BIT); window.swap_buffers(); - + println!("{}", window.wait_events().collect::>()); } } diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs index 2ae9c318..5bc1b075 100644 --- a/examples/multiwindow.rs +++ b/examples/multiwindow.rs @@ -19,13 +19,18 @@ fn run(window: init::Window, color: (f32, f32, f32, f32)) { unsafe { window.make_current() }; gl::load_with(|symbol| window.get_proc_address(symbol) as *const libc::c_void); + + { + let win_size = window.get_inner_size().unwrap(); + gl::Viewport(0, 0, win_size.val0() as libc::c_int, win_size.val1() as libc::c_int); + } gl::ClearColor(color.val0(), color.val1(), color.val2(), color.val3()); while !window.is_closed() { gl::Clear(gl::COLOR_BUFFER_BIT); window.swap_buffers(); - + window.wait_events().collect::>(); } } diff --git a/examples/window.rs b/examples/window.rs index 07f5a84a..db80e393 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -16,6 +16,11 @@ fn main() { println!("OpenGL version {}", version.as_str().unwrap()); + { + let win_size = window.get_inner_size().unwrap(); + gl::Viewport(0, 0, win_size.val0() as libc::c_int, win_size.val1() as libc::c_int); + } + gl::ClearColor(0.0, 1.0, 0.0, 1.0); while !window.is_closed() { From d2bd19bc21889e78c18bf64a570677e6330bbad0 Mon Sep 17 00:00:00 2001 From: Tomaka17 Date: Thu, 7 Aug 2014 09:33:28 +0200 Subject: [PATCH 11/11] Doc now ignored for doctest --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 60ce0aa2..3333ebc8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,7 +122,7 @@ impl WindowBuilder { /// /// # Example /// -/// ``` +/// ```ignore /// let window = Window::new().unwrap(); /// /// unsafe { window.make_current() };