2014-07-27 18:55:37 +10:00
|
|
|
#![feature(unsafe_destructor)]
|
2014-07-30 21:11:49 +10:00
|
|
|
#![feature(globs)]
|
2014-08-12 17:16:08 +10:00
|
|
|
#![feature(phase)]
|
2014-07-30 22:13:42 +10:00
|
|
|
#![unstable]
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-08-03 04:49:48 +10:00
|
|
|
//! 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.
|
|
|
|
|
2014-08-12 17:16:08 +10:00
|
|
|
#[phase(plugin)] extern crate compile_msg;
|
2014-07-27 18:55:37 +10:00
|
|
|
extern crate libc;
|
|
|
|
|
2014-07-30 21:11:49 +10:00
|
|
|
pub use events::*;
|
2014-07-27 20:59:45 +10:00
|
|
|
|
2014-08-13 22:52:12 +10:00
|
|
|
use std::default::Default;
|
|
|
|
|
2014-08-16 00:12:40 +10:00
|
|
|
#[cfg(target_os = "windows")]
|
2014-07-27 20:59:45 +10:00
|
|
|
use winimpl = win32;
|
2014-08-04 02:30:31 +10:00
|
|
|
#[cfg(target_os = "linux")]
|
2014-07-27 23:10:58 +10:00
|
|
|
use winimpl = x11;
|
2014-08-04 02:30:31 +10:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
use winimpl = osx;
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-08-16 00:12:40 +10:00
|
|
|
#[cfg(target_os = "windows")]
|
2014-07-27 18:55:37 +10:00
|
|
|
mod win32;
|
2014-08-03 17:25:30 +10:00
|
|
|
#[cfg(target_os = "linux")]
|
2014-07-27 23:10:58 +10:00
|
|
|
mod x11;
|
2014-08-03 17:25:30 +10:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
mod osx;
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-07-31 01:13:24 +10:00
|
|
|
#[allow(dead_code)]
|
|
|
|
//mod egl;
|
|
|
|
|
2014-07-27 20:59:45 +10:00
|
|
|
mod events;
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-08-16 00:12:40 +10:00
|
|
|
#[cfg(not(target_os = "windows"), not(target_os = "linux"), not(target_os = "macos"))]
|
|
|
|
compile_error!("Only the `windows`, `linux` and `macos` platforms are supported")
|
2014-08-03 18:30:04 +10:00
|
|
|
|
2014-07-31 17:42:50 +10:00
|
|
|
/// Identifier for a monitor.
|
|
|
|
pub struct MonitorID(winimpl::MonitorID);
|
2014-07-28 04:38:27 +10:00
|
|
|
|
2014-08-02 16:24:30 +10:00
|
|
|
/// Object that allows you to build windows.
|
|
|
|
pub struct WindowBuilder {
|
2014-08-02 19:23:32 +10:00
|
|
|
dimensions: Option<(uint, uint)>,
|
2014-08-02 16:24:30 +10:00
|
|
|
title: String,
|
|
|
|
monitor: Option<winimpl::MonitorID>,
|
2014-08-02 19:03:09 +10:00
|
|
|
gl_version: Option<(uint, uint)>,
|
2014-08-02 16:24:30 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WindowBuilder {
|
|
|
|
/// Initializes a new `WindowBuilder` with default values.
|
|
|
|
pub fn new() -> WindowBuilder {
|
|
|
|
WindowBuilder {
|
2014-08-02 19:23:32 +10:00
|
|
|
dimensions: None,
|
2014-08-08 02:49:47 +10:00
|
|
|
title: "gl-init-rs window".to_string(),
|
2014-08-02 16:24:30 +10:00
|
|
|
monitor: None,
|
2014-08-02 19:03:09 +10:00
|
|
|
gl_version: None,
|
2014-08-02 16:24:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-02 19:04:48 +10:00
|
|
|
/// Requests the window to be of specific dimensions.
|
|
|
|
///
|
|
|
|
/// Width and height are in pixels.
|
2014-08-02 16:24:30 +10:00
|
|
|
pub fn with_dimensions(mut self, width: uint, height: uint) -> WindowBuilder {
|
2014-08-02 19:23:32 +10:00
|
|
|
self.dimensions = Some((width, height));
|
2014-08-02 16:24:30 +10:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2014-08-02 19:04:48 +10:00
|
|
|
/// Requests a specific title for the window.
|
2014-08-02 16:24:30 +10:00
|
|
|
pub fn with_title(mut self, title: String) -> WindowBuilder {
|
|
|
|
self.title = title;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2014-08-02 19:04:48 +10:00
|
|
|
/// Requests fullscreen mode.
|
2014-08-02 19:23:32 +10:00
|
|
|
///
|
|
|
|
/// If you don't specify dimensions for the window, it will match the monitor's.
|
2014-08-02 19:04:48 +10:00
|
|
|
pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder {
|
2014-08-02 16:24:30 +10:00
|
|
|
let MonitorID(monitor) = monitor;
|
|
|
|
self.monitor = Some(monitor);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2014-08-02 19:03:09 +10:00
|
|
|
/// Requests to use a specific OpenGL version.
|
|
|
|
///
|
|
|
|
/// Version is a (major, minor) pair. For example to request OpenGL 3.3
|
|
|
|
/// you would pass `(3, 3)`.
|
|
|
|
pub fn with_gl_version(mut self, version: (uint, uint)) -> WindowBuilder {
|
|
|
|
self.gl_version = Some(version);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2014-08-02 16:24:30 +10:00
|
|
|
/// Builds the window.
|
|
|
|
///
|
|
|
|
/// Error should be very rare and only occur in case of permission denied, incompatible system,
|
|
|
|
/// out of memory, etc.
|
2014-08-02 19:23:32 +10:00
|
|
|
pub fn build(mut self) -> Result<Window, String> {
|
|
|
|
// resizing the window to the dimensions of the monitor when fullscreen
|
|
|
|
if self.dimensions.is_none() && self.monitor.is_some() {
|
|
|
|
self.dimensions = Some(self.monitor.as_ref().unwrap().get_dimensions())
|
|
|
|
}
|
|
|
|
|
|
|
|
// default dimensions
|
|
|
|
if self.dimensions.is_none() {
|
|
|
|
self.dimensions = Some((1024, 768));
|
|
|
|
}
|
|
|
|
|
|
|
|
// building
|
2014-08-02 18:42:17 +10:00
|
|
|
winimpl::Window::new(self).map(|w| Window { window: w })
|
2014-08-02 16:24:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Represents an OpenGL context and the Window or environment around it.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-08-07 17:33:28 +10:00
|
|
|
/// ```ignore
|
2014-08-02 16:24:30 +10:00
|
|
|
/// let window = Window::new().unwrap();
|
2014-07-30 22:13:42 +10:00
|
|
|
///
|
2014-07-31 02:12:39 +10:00
|
|
|
/// unsafe { window.make_current() };
|
2014-07-30 22:13:42 +10:00
|
|
|
///
|
|
|
|
/// loop {
|
2014-08-02 19:07:29 +10:00
|
|
|
/// for event in window.poll_events() {
|
2014-07-30 22:13:42 +10:00
|
|
|
/// // process events here
|
|
|
|
/// _ => ()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // draw everything here
|
|
|
|
///
|
|
|
|
/// window.swap_buffers();
|
|
|
|
/// std::io::timer::sleep(17);
|
|
|
|
/// }
|
2014-07-30 22:18:31 +10:00
|
|
|
/// ```
|
|
|
|
pub struct Window {
|
|
|
|
window: winimpl::Window,
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:52:12 +10:00
|
|
|
impl Default for Window {
|
|
|
|
fn default() -> Window {
|
|
|
|
Window::new().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-27 20:59:45 +10:00
|
|
|
impl Window {
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Creates a new OpenGL context, and a Window for platforms where this is appropriate.
|
|
|
|
///
|
2014-08-02 16:24:30 +10:00
|
|
|
/// This function is equivalent to `WindowBuilder::new().build()`.
|
|
|
|
///
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Error should be very rare and only occur in case of permission denied, incompatible system,
|
|
|
|
/// out of memory, etc.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-08-02 16:24:30 +10:00
|
|
|
pub fn new() -> Result<Window, String> {
|
|
|
|
let builder = WindowBuilder::new();
|
|
|
|
builder.build()
|
2014-07-27 20:59:45 +10:00
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Returns true if the window has previously been closed by the user.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-07-30 21:29:28 +10:00
|
|
|
pub fn is_closed(&self) -> bool {
|
|
|
|
self.window.is_closed()
|
|
|
|
}
|
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Returns true if the window has previously been closed by the user.
|
2014-07-30 21:29:28 +10:00
|
|
|
#[inline]
|
|
|
|
#[deprecated = "Use is_closed instead"]
|
2014-07-27 20:59:45 +10:00
|
|
|
pub fn should_close(&self) -> bool {
|
2014-07-30 21:29:28 +10:00
|
|
|
self.is_closed()
|
2014-07-27 20:59:45 +10:00
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-07-27 20:59:45 +10:00
|
|
|
/// Modifies the title of the window.
|
2014-07-30 22:13:42 +10:00
|
|
|
///
|
|
|
|
/// This is a no-op if the window has already been closed.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn set_title(&self, title: &str) {
|
|
|
|
self.window.set_title(title)
|
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Returns the position of the top-left hand corner of the window relative to the
|
|
|
|
/// top-left hand corner of the desktop.
|
|
|
|
///
|
|
|
|
/// Note that the top-left hand corner of the desktop is not necessarly the same as
|
|
|
|
/// the screen. If the user uses a desktop with multiple monitors, the top-left hand corner
|
|
|
|
/// of the desktop is the top-left hand corner of the monitor at the top-left of the desktop.
|
|
|
|
///
|
|
|
|
/// The coordinates can be negative if the top-left hand corner of the window is outside
|
|
|
|
/// of the visible screen region.
|
2014-07-30 21:10:17 +10:00
|
|
|
///
|
|
|
|
/// Returns `None` if the window no longer exists.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-07-30 21:10:17 +10:00
|
|
|
pub fn get_position(&self) -> Option<(int, int)> {
|
2014-07-27 20:59:45 +10:00
|
|
|
self.window.get_position()
|
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Modifies the position of the window.
|
|
|
|
///
|
|
|
|
/// See `get_position` for more informations about the coordinates.
|
|
|
|
///
|
|
|
|
/// This is a no-op if the window has already been closed.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-08-08 02:07:48 +10:00
|
|
|
pub fn set_position(&self, x: int, y: int) {
|
2014-07-27 20:59:45 +10:00
|
|
|
self.window.set_position(x, y)
|
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Returns the size in pixels of the client area of the window.
|
|
|
|
///
|
|
|
|
/// The client area is the content of the window, excluding the title bar and borders.
|
|
|
|
/// These are the dimensions of the frame buffer, and the dimensions that you should use
|
|
|
|
/// when you call `glViewport`.
|
|
|
|
///
|
|
|
|
/// Returns `None` if the window no longer exists.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-07-30 21:10:17 +10:00
|
|
|
pub fn get_inner_size(&self) -> Option<(uint, uint)> {
|
2014-07-28 06:31:00 +10:00
|
|
|
self.window.get_inner_size()
|
2014-07-27 20:59:45 +10:00
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Returns the size in pixels of the window.
|
|
|
|
///
|
|
|
|
/// These dimensions include title bar and borders. If you don't want these, you should use
|
|
|
|
/// use `get_inner_size` instead.
|
|
|
|
///
|
|
|
|
/// Returns `None` if the window no longer exists.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-07-30 21:10:17 +10:00
|
|
|
pub fn get_outer_size(&self) -> Option<(uint, uint)> {
|
2014-07-28 06:31:00 +10:00
|
|
|
self.window.get_outer_size()
|
|
|
|
}
|
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Modifies the inner size of the window.
|
|
|
|
///
|
|
|
|
/// See `get_inner_size` for more informations about the values.
|
|
|
|
///
|
|
|
|
/// This is a no-op if the window has already been closed.
|
2014-07-28 06:31:00 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn set_inner_size(&self, x: uint, y: uint) {
|
|
|
|
self.window.set_inner_size(x, y)
|
2014-07-27 20:59:45 +10:00
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-08-02 16:24:30 +10:00
|
|
|
/// Returns an iterator to all the events that are currently in the window's events queue.
|
2014-07-30 22:13:42 +10:00
|
|
|
///
|
|
|
|
/// Contrary to `wait_events`, this function never blocks.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-08-02 16:24:30 +10:00
|
|
|
pub fn poll_events(&self) -> PollEventsIterator {
|
|
|
|
PollEventsIterator { data: self.window.poll_events() }
|
2014-07-27 20:59:45 +10:00
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-08-02 16:24:30 +10:00
|
|
|
/// Waits for an event, then returns an iterator to all the events that are currently
|
|
|
|
/// in the window's events queue.
|
|
|
|
///
|
|
|
|
/// If there are no events in queue when you call the function,
|
|
|
|
/// this function will block until there is one.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-08-02 16:24:30 +10:00
|
|
|
pub fn wait_events(&self) -> WaitEventsIterator {
|
|
|
|
WaitEventsIterator { data: self.window.wait_events() }
|
2014-07-27 20:59:45 +10:00
|
|
|
}
|
|
|
|
|
2014-07-31 02:12:39 +10:00
|
|
|
/// Sets the context as the current context.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-07-31 02:12:39 +10:00
|
|
|
pub unsafe fn make_current(&self) {
|
2014-07-27 20:59:45 +10:00
|
|
|
self.window.make_current()
|
|
|
|
}
|
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Returns the address of an OpenGL function.
|
|
|
|
///
|
|
|
|
/// Contrary to `wglGetProcAddress`, all available OpenGL functions return an address.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
2014-08-11 18:32:33 +10:00
|
|
|
pub fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
|
|
|
|
self.window.get_proc_address(addr) as *const libc::c_void
|
2014-07-27 20:59:45 +10:00
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-07-30 22:13:42 +10:00
|
|
|
/// Swaps the buffers in case of double or triple buffering.
|
|
|
|
///
|
|
|
|
/// You should call this function every time you have finished rendering, or the image
|
|
|
|
/// may not be displayed on the screen.
|
2014-07-27 20:59:45 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn swap_buffers(&self) {
|
|
|
|
self.window.swap_buffers()
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
|
|
|
}
|
2014-07-31 17:56:53 +10:00
|
|
|
|
2014-08-02 16:24:30 +10:00
|
|
|
/// An iterator for the `poll_events` function.
|
|
|
|
// Implementation note: we retreive the list once, then serve each element by one by one.
|
|
|
|
// This may change in the future.
|
|
|
|
pub struct PollEventsIterator<'a> {
|
|
|
|
data: Vec<Event>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator<Event> for PollEventsIterator<'a> {
|
|
|
|
fn next(&mut self) -> Option<Event> {
|
|
|
|
self.data.remove(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator for the `wait_events` function.
|
|
|
|
// Implementation note: we retreive the list once, then serve each element by one by one.
|
|
|
|
// This may change in the future.
|
|
|
|
pub struct WaitEventsIterator<'a> {
|
|
|
|
data: Vec<Event>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator<Event> for WaitEventsIterator<'a> {
|
|
|
|
fn next(&mut self) -> Option<Event> {
|
|
|
|
self.data.remove(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 17:56:53 +10:00
|
|
|
/// An iterator for the list of available monitors.
|
|
|
|
// Implementation note: we retreive the list once, then serve each element by one by one.
|
|
|
|
// This may change in the future.
|
|
|
|
pub struct AvailableMonitorsIter {
|
|
|
|
data: Vec<winimpl::MonitorID>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator<MonitorID> for AvailableMonitorsIter {
|
|
|
|
fn next(&mut self) -> Option<MonitorID> {
|
|
|
|
self.data.remove(0).map(|id| MonitorID(id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the list of all available monitors.
|
|
|
|
pub fn get_available_monitors() -> AvailableMonitorsIter {
|
|
|
|
let data = winimpl::get_available_monitors();
|
|
|
|
AvailableMonitorsIter{ data: data }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the primary monitor of the system.
|
|
|
|
pub fn get_primary_monitor() -> MonitorID {
|
|
|
|
MonitorID(winimpl::get_primary_monitor())
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MonitorID {
|
|
|
|
/// Returns a human-readable name of the monitor.
|
|
|
|
pub fn get_name(&self) -> Option<String> {
|
2014-07-31 18:52:05 +10:00
|
|
|
let &MonitorID(ref id) = self;
|
|
|
|
id.get_name()
|
2014-07-31 17:56:53 +10:00
|
|
|
}
|
2014-08-02 19:17:49 +10:00
|
|
|
|
|
|
|
/// Returns the number of pixels currently displayed on the monitor.
|
|
|
|
pub fn get_dimensions(&self) -> (uint, uint) {
|
|
|
|
let &MonitorID(ref id) = self;
|
|
|
|
id.get_dimensions()
|
|
|
|
}
|
2014-07-31 17:56:53 +10:00
|
|
|
}
|