Struct glutin::WindowBuilder
source · pub struct WindowBuilder<'a> {
pub opengl: GlAttributes<&'a Window>,
/* private fields */
}
Expand description
Object that allows you to build windows.
Fields§
§opengl: GlAttributes<&'a Window>
The attributes to use to create the context.
Implementations§
source§impl<'a> WindowBuilder<'a>
impl<'a> WindowBuilder<'a>
sourcepub fn new() -> WindowBuilder<'a>
pub fn new() -> WindowBuilder<'a>
Initializes a new WindowBuilder
with default values.
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() {
let window1 = glutin::WindowBuilder::new().build().unwrap();
let window2 = glutin::WindowBuilder::new().build().unwrap();
let window3 = glutin::WindowBuilder::new().build().unwrap();
let t1 = thread::spawn(move || {
run(window1, (0.0, 1.0, 0.0, 1.0));
});
let t2 = thread::spawn(move || {
run(window2, (0.0, 0.0, 1.0, 1.0));
});
let t3 = thread::spawn(move || {
run(window3, (1.0, 0.0, 0.0, 1.0));
});
let _ = t1.join();
let _ = t2.join();
let _ = t3.join();
}
More examples
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let mut window = glutin::WindowBuilder::new().build().unwrap();
window.set_title("A fantastic window!");
window.set_window_resize_callback(Some(resize_callback as fn(u32, u32)));
let _ = unsafe { window.make_current() };
println!("Pixel format of the window: {:?}", window.get_pixel_format());
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 1.0, 0.0, 1.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
_ => ()
}
}
}
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
let mut window = glutin::WindowBuilder::new().with_decorations(false)
.with_transparency(true)
.build().unwrap();
window.set_title("A fantastic window!");
window.set_window_resize_callback(Some(resize_callback as fn(u32, u32)));
let _ = unsafe { window.make_current() };
println!("Pixel format of the window: {:?}", window.get_pixel_format());
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 0.0, 0.0, 0.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
_ => ()
}
}
}
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let window = glutin::WindowBuilder::new().build().unwrap();
window.set_title("glutin - Cursor grabbing test");
let _ = unsafe { window.make_current() };
let context = support::load(&window);
let mut grabbed = false;
for event in window.wait_events() {
match event {
Event::KeyboardInput(ElementState::Pressed, _, _) => {
if grabbed {
grabbed = false;
window.set_cursor_state(glutin::CursorState::Normal)
.ok().expect("could not ungrab mouse cursor");
} else {
grabbed = true;
window.set_cursor_state(glutin::CursorState::Grab)
.ok().expect("could not grab mouse cursor");
}
},
Event::Closed => break,
a @ Event::MouseMoved(_, _) => {
println!("{:?}", a);
},
_ => (),
}
context.draw_frame((0.0, 1.0, 0.0, 1.0));
let _ = window.swap_buffers();
}
}
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
// enumerating monitors
let monitor = {
for (num, monitor) in glutin::get_available_monitors().enumerate() {
println!("Monitor #{}: {:?}", num, monitor.get_name());
}
print!("Please write the number of the monitor to use: ");
io::stdout().flush().unwrap();
let mut num = String::new();
io::stdin().read_line(&mut num).unwrap();
let num = num.trim().parse().ok().expect("Please enter a number");
let monitor = glutin::get_available_monitors().nth(num).expect("Please enter a valid ID");
println!("Using {:?}", monitor.get_name());
monitor
};
let window = glutin::WindowBuilder::new()
.with_title("Hello world!")
.with_fullscreen(monitor)
.build()
.unwrap();
let _ = unsafe { window.make_current() };
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 1.0, 0.0, 1.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
glutin::Event::KeyboardInput(_, _, Some(glutin::VirtualKeyCode::Escape)) => break,
_ => ()
}
}
}
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() {
let window = glutin::WindowBuilder::new().build().unwrap();
window.set_title("A fantastic window!");
unsafe { window.make_current().unwrap() };
let context = support::load(&window);
let cursors = [MouseCursor::Default, MouseCursor::Crosshair, MouseCursor::Hand, MouseCursor::Arrow, MouseCursor::Move, MouseCursor::Text, MouseCursor::Wait, MouseCursor::Help, MouseCursor::Progress, MouseCursor::NotAllowed, MouseCursor::ContextMenu, MouseCursor::NoneCursor, MouseCursor::Cell, MouseCursor::VerticalText, MouseCursor::Alias, MouseCursor::Copy, MouseCursor::NoDrop, MouseCursor::Grab, MouseCursor::Grabbing, MouseCursor::AllScroll, MouseCursor::ZoomIn, MouseCursor::ZoomOut, MouseCursor::EResize, MouseCursor::NResize, MouseCursor::NeResize, MouseCursor::NwResize, MouseCursor::SResize, MouseCursor::SeResize, MouseCursor::SwResize, MouseCursor::WResize, MouseCursor::EwResize, MouseCursor::NsResize, MouseCursor::NeswResize, MouseCursor::NwseResize, MouseCursor::ColResize, MouseCursor::RowResize];
let mut cursor_idx = 0;
for event in window.wait_events() {
match event {
Event::KeyboardInput(ElementState::Pressed, _, _) => {
println!("Setting cursor to \"{:?}\"", cursors[cursor_idx]);
window.set_cursor(cursors[cursor_idx]);
if cursor_idx < cursors.len() - 1 {
cursor_idx += 1;
} else {
cursor_idx = 0;
}
},
Event::Closed => break,
_ => (),
}
context.draw_frame((0.0, 1.0, 0.0, 1.0));
window.swap_buffers().unwrap();
}
}
sourcepub fn with_dimensions(self, width: u32, height: u32) -> WindowBuilder<'a>
pub fn with_dimensions(self, width: u32, height: u32) -> WindowBuilder<'a>
Requests the window to be of specific dimensions.
Width and height are in pixels.
sourcepub fn with_min_dimensions(self, width: u32, height: u32) -> WindowBuilder<'a>
pub fn with_min_dimensions(self, width: u32, height: u32) -> WindowBuilder<'a>
Sets a minimum dimension size for the window
Width and height are in pixels.
sourcepub fn with_max_dimensions(self, width: u32, height: u32) -> WindowBuilder<'a>
pub fn with_max_dimensions(self, width: u32, height: u32) -> WindowBuilder<'a>
Sets a maximum dimension size for the window
Width and height are in pixels.
sourcepub fn with_title<T: Into<String>>(self, title: T) -> WindowBuilder<'a>
pub fn with_title<T: Into<String>>(self, title: T) -> WindowBuilder<'a>
Requests a specific title for the window.
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
// enumerating monitors
let monitor = {
for (num, monitor) in glutin::get_available_monitors().enumerate() {
println!("Monitor #{}: {:?}", num, monitor.get_name());
}
print!("Please write the number of the monitor to use: ");
io::stdout().flush().unwrap();
let mut num = String::new();
io::stdin().read_line(&mut num).unwrap();
let num = num.trim().parse().ok().expect("Please enter a number");
let monitor = glutin::get_available_monitors().nth(num).expect("Please enter a valid ID");
println!("Using {:?}", monitor.get_name());
monitor
};
let window = glutin::WindowBuilder::new()
.with_title("Hello world!")
.with_fullscreen(monitor)
.build()
.unwrap();
let _ = unsafe { window.make_current() };
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 1.0, 0.0, 1.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
glutin::Event::KeyboardInput(_, _, Some(glutin::VirtualKeyCode::Escape)) => break,
_ => ()
}
}
}
sourcepub fn with_fullscreen(self, monitor: MonitorId) -> WindowBuilder<'a>
pub fn with_fullscreen(self, monitor: MonitorId) -> WindowBuilder<'a>
Requests fullscreen mode.
If you don’t specify dimensions for the window, it will match the monitor’s.
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
// enumerating monitors
let monitor = {
for (num, monitor) in glutin::get_available_monitors().enumerate() {
println!("Monitor #{}: {:?}", num, monitor.get_name());
}
print!("Please write the number of the monitor to use: ");
io::stdout().flush().unwrap();
let mut num = String::new();
io::stdin().read_line(&mut num).unwrap();
let num = num.trim().parse().ok().expect("Please enter a number");
let monitor = glutin::get_available_monitors().nth(num).expect("Please enter a valid ID");
println!("Using {:?}", monitor.get_name());
monitor
};
let window = glutin::WindowBuilder::new()
.with_title("Hello world!")
.with_fullscreen(monitor)
.build()
.unwrap();
let _ = unsafe { window.make_current() };
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 1.0, 0.0, 1.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
glutin::Event::KeyboardInput(_, _, Some(glutin::VirtualKeyCode::Escape)) => break,
_ => ()
}
}
}
The created window will share all its OpenGL objects with the window in the parameter.
There are some exceptions, like FBOs or VAOs. See the OpenGL documentation.
sourcepub fn with_gl(self, request: GlRequest) -> WindowBuilder<'a>
pub fn with_gl(self, request: GlRequest) -> WindowBuilder<'a>
Sets how the backend should choose the OpenGL API and version.
sourcepub fn with_gl_profile(self, profile: GlProfile) -> WindowBuilder<'a>
pub fn with_gl_profile(self, profile: GlProfile) -> WindowBuilder<'a>
Sets the desired OpenGL context profile.
sourcepub fn with_gl_debug_flag(self, flag: bool) -> WindowBuilder<'a>
pub fn with_gl_debug_flag(self, flag: bool) -> WindowBuilder<'a>
Sets the debug flag for the OpenGL context.
The default value for this flag is cfg!(debug_assertions)
, which means that it’s enabled
when you run cargo build
and disabled when you run cargo build --release
.
sourcepub fn with_gl_robustness(self, robustness: Robustness) -> WindowBuilder<'a>
pub fn with_gl_robustness(self, robustness: Robustness) -> WindowBuilder<'a>
Sets the robustness of the OpenGL context. See the docs of Robustness
.
sourcepub fn with_vsync(self) -> WindowBuilder<'a>
pub fn with_vsync(self) -> WindowBuilder<'a>
Requests that the window has vsync enabled.
sourcepub fn with_visibility(self, visible: bool) -> WindowBuilder<'a>
pub fn with_visibility(self, visible: bool) -> WindowBuilder<'a>
Sets whether the window will be initially hidden or visible.
sourcepub fn with_multisampling(self, samples: u16) -> WindowBuilder<'a>
pub fn with_multisampling(self, samples: u16) -> WindowBuilder<'a>
sourcepub fn with_depth_buffer(self, bits: u8) -> WindowBuilder<'a>
pub fn with_depth_buffer(self, bits: u8) -> WindowBuilder<'a>
Sets the number of bits in the depth buffer.
sourcepub fn with_stencil_buffer(self, bits: u8) -> WindowBuilder<'a>
pub fn with_stencil_buffer(self, bits: u8) -> WindowBuilder<'a>
Sets the number of bits in the stencil buffer.
sourcepub fn with_pixel_format(
self,
color_bits: u8,
alpha_bits: u8
) -> WindowBuilder<'a>
pub fn with_pixel_format( self, color_bits: u8, alpha_bits: u8 ) -> WindowBuilder<'a>
Sets the number of bits in the color buffer.
sourcepub fn with_stereoscopy(self) -> WindowBuilder<'a>
pub fn with_stereoscopy(self) -> WindowBuilder<'a>
Request the backend to be stereoscopic.
sourcepub fn with_srgb(self, srgb_enabled: Option<bool>) -> WindowBuilder<'a>
pub fn with_srgb(self, srgb_enabled: Option<bool>) -> WindowBuilder<'a>
Sets whether sRGB should be enabled on the window. None
means “I don’t care”.
sourcepub fn with_transparency(self, transparent: bool) -> WindowBuilder<'a>
pub fn with_transparency(self, transparent: bool) -> WindowBuilder<'a>
Sets whether the background of the window should be transparent.
Examples found in repository?
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
let mut window = glutin::WindowBuilder::new().with_decorations(false)
.with_transparency(true)
.build().unwrap();
window.set_title("A fantastic window!");
window.set_window_resize_callback(Some(resize_callback as fn(u32, u32)));
let _ = unsafe { window.make_current() };
println!("Pixel format of the window: {:?}", window.get_pixel_format());
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 0.0, 0.0, 0.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
_ => ()
}
}
}
sourcepub fn with_decorations(self, decorations: bool) -> WindowBuilder<'a>
pub fn with_decorations(self, decorations: bool) -> WindowBuilder<'a>
Sets whether the window should have a border, a title bar, etc.
Examples found in repository?
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
let mut window = glutin::WindowBuilder::new().with_decorations(false)
.with_transparency(true)
.build().unwrap();
window.set_title("A fantastic window!");
window.set_window_resize_callback(Some(resize_callback as fn(u32, u32)));
let _ = unsafe { window.make_current() };
println!("Pixel format of the window: {:?}", window.get_pixel_format());
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 0.0, 0.0, 0.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
_ => ()
}
}
}
sourcepub fn with_multitouch(self) -> WindowBuilder<'a>
pub fn with_multitouch(self) -> WindowBuilder<'a>
Enables multitouch
sourcepub fn build(self) -> Result<Window, CreationError>
pub fn build(self) -> Result<Window, CreationError>
Builds the window.
Error should be very rare and only occur in case of permission denied, incompatible system, out of memory, etc.
Examples found in repository?
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
fn main() {
let window1 = glutin::WindowBuilder::new().build().unwrap();
let window2 = glutin::WindowBuilder::new().build().unwrap();
let window3 = glutin::WindowBuilder::new().build().unwrap();
let t1 = thread::spawn(move || {
run(window1, (0.0, 1.0, 0.0, 1.0));
});
let t2 = thread::spawn(move || {
run(window2, (0.0, 0.0, 1.0, 1.0));
});
let t3 = thread::spawn(move || {
run(window3, (1.0, 0.0, 0.0, 1.0));
});
let _ = t1.join();
let _ = t2.join();
let _ = t3.join();
}
More examples
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let mut window = glutin::WindowBuilder::new().build().unwrap();
window.set_title("A fantastic window!");
window.set_window_resize_callback(Some(resize_callback as fn(u32, u32)));
let _ = unsafe { window.make_current() };
println!("Pixel format of the window: {:?}", window.get_pixel_format());
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 1.0, 0.0, 1.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
_ => ()
}
}
}
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
let mut window = glutin::WindowBuilder::new().with_decorations(false)
.with_transparency(true)
.build().unwrap();
window.set_title("A fantastic window!");
window.set_window_resize_callback(Some(resize_callback as fn(u32, u32)));
let _ = unsafe { window.make_current() };
println!("Pixel format of the window: {:?}", window.get_pixel_format());
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 0.0, 0.0, 0.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
_ => ()
}
}
}
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
let window = glutin::WindowBuilder::new().build().unwrap();
window.set_title("glutin - Cursor grabbing test");
let _ = unsafe { window.make_current() };
let context = support::load(&window);
let mut grabbed = false;
for event in window.wait_events() {
match event {
Event::KeyboardInput(ElementState::Pressed, _, _) => {
if grabbed {
grabbed = false;
window.set_cursor_state(glutin::CursorState::Normal)
.ok().expect("could not ungrab mouse cursor");
} else {
grabbed = true;
window.set_cursor_state(glutin::CursorState::Grab)
.ok().expect("could not grab mouse cursor");
}
},
Event::Closed => break,
a @ Event::MouseMoved(_, _) => {
println!("{:?}", a);
},
_ => (),
}
context.draw_frame((0.0, 1.0, 0.0, 1.0));
let _ = window.swap_buffers();
}
}
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
// enumerating monitors
let monitor = {
for (num, monitor) in glutin::get_available_monitors().enumerate() {
println!("Monitor #{}: {:?}", num, monitor.get_name());
}
print!("Please write the number of the monitor to use: ");
io::stdout().flush().unwrap();
let mut num = String::new();
io::stdin().read_line(&mut num).unwrap();
let num = num.trim().parse().ok().expect("Please enter a number");
let monitor = glutin::get_available_monitors().nth(num).expect("Please enter a valid ID");
println!("Using {:?}", monitor.get_name());
monitor
};
let window = glutin::WindowBuilder::new()
.with_title("Hello world!")
.with_fullscreen(monitor)
.build()
.unwrap();
let _ = unsafe { window.make_current() };
let context = support::load(&window);
for event in window.wait_events() {
context.draw_frame((0.0, 1.0, 0.0, 1.0));
let _ = window.swap_buffers();
println!("{:?}", event);
match event {
glutin::Event::Closed => break,
glutin::Event::KeyboardInput(_, _, Some(glutin::VirtualKeyCode::Escape)) => break,
_ => ()
}
}
}
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() {
let window = glutin::WindowBuilder::new().build().unwrap();
window.set_title("A fantastic window!");
unsafe { window.make_current().unwrap() };
let context = support::load(&window);
let cursors = [MouseCursor::Default, MouseCursor::Crosshair, MouseCursor::Hand, MouseCursor::Arrow, MouseCursor::Move, MouseCursor::Text, MouseCursor::Wait, MouseCursor::Help, MouseCursor::Progress, MouseCursor::NotAllowed, MouseCursor::ContextMenu, MouseCursor::NoneCursor, MouseCursor::Cell, MouseCursor::VerticalText, MouseCursor::Alias, MouseCursor::Copy, MouseCursor::NoDrop, MouseCursor::Grab, MouseCursor::Grabbing, MouseCursor::AllScroll, MouseCursor::ZoomIn, MouseCursor::ZoomOut, MouseCursor::EResize, MouseCursor::NResize, MouseCursor::NeResize, MouseCursor::NwResize, MouseCursor::SResize, MouseCursor::SeResize, MouseCursor::SwResize, MouseCursor::WResize, MouseCursor::EwResize, MouseCursor::NsResize, MouseCursor::NeswResize, MouseCursor::NwseResize, MouseCursor::ColResize, MouseCursor::RowResize];
let mut cursor_idx = 0;
for event in window.wait_events() {
match event {
Event::KeyboardInput(ElementState::Pressed, _, _) => {
println!("Setting cursor to \"{:?}\"", cursors[cursor_idx]);
window.set_cursor(cursors[cursor_idx]);
if cursor_idx < cursors.len() - 1 {
cursor_idx += 1;
} else {
cursor_idx = 0;
}
},
Event::Closed => break,
_ => (),
}
context.draw_frame((0.0, 1.0, 0.0, 1.0));
window.swap_buffers().unwrap();
}
}
sourcepub fn build_strict(self) -> Result<Window, CreationError>
pub fn build_strict(self) -> Result<Window, CreationError>
Builds the window.
The context is build in a strict way. That means that if the backend couldn’t give
you what you requested, an Err
will be returned.