Struct glutin::ContextBuilder
source · pub struct ContextBuilder<'a> {
pub gl_attr: GlAttributes<&'a Context>,
/* private fields */
}
Expand description
Object that allows you to build Context
s.
Fields§
§gl_attr: GlAttributes<&'a Context>
The attributes to use to create the context.
Implementations§
source§impl<'a> ContextBuilder<'a>
impl<'a> ContextBuilder<'a>
sourcepub fn new() -> Self
pub fn new() -> Self
Initializes a new ContextBuilder
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 28 29 30 31 32 33 34
fn main() {
let mut events_loop = glutin::EventsLoop::new();
let window = glutin::WindowBuilder::new().with_title("A fantastic window!");
let context = glutin::ContextBuilder::new();
let gl_window = glutin::GlWindow::new(window, context, &events_loop).unwrap();
let _ = unsafe { gl_window.make_current() };
println!("Pixel format of the window's GL context: {:?}", gl_window.get_pixel_format());
let gl = support::load(&gl_window);
events_loop.run_forever(|event| {
println!("{:?}", event);
match event {
glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::Closed => return glutin::ControlFlow::Break,
glutin::WindowEvent::Resized(w, h) => gl_window.resize(w, h),
_ => (),
},
_ => ()
}
gl.draw_frame([0.0, 1.0, 0.0, 1.0]);
let _ = gl_window.swap_buffers();
glutin::ControlFlow::Continue
});
}
More examples
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
fn main() {
let mut events_loop = glutin::EventsLoop::new();
let window = glutin::WindowBuilder::new()
.with_title("A fantastic window!")
.with_decorations(false)
.with_transparency(true);
let context = glutin::ContextBuilder::new();
let gl_window = glutin::GlWindow::new(window, context, &events_loop).unwrap();
let _ = unsafe { gl_window.make_current() };
println!("Pixel format of the window's GL context: {:?}", gl_window.get_pixel_format());
let gl = support::load(&gl_window);
events_loop.run_forever(|event| {
println!("{:?}", event);
match event {
glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::Closed => return glutin::ControlFlow::Break,
glutin::WindowEvent::Resized(w, h) => gl_window.resize(w, h),
_ => (),
},
_ => (),
}
gl.draw_frame([0.0, 0.0, 0.0, 0.0]);
let _ = gl_window.swap_buffers();
glutin::ControlFlow::Continue
});
}
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
fn main() {
let mut events_loop = glutin::EventsLoop::new();
let window = glutin::WindowBuilder::new().with_title("glutin - Cursor grabbing test");
let context = glutin::ContextBuilder::new();
let gl_window = glutin::GlWindow::new(window, context, &events_loop).unwrap();
let _ = unsafe { gl_window.make_current() };
let gl = support::load(&gl_window);
let mut grabbed = false;
events_loop.run_forever(|event| {
use glutin::{CursorState, ControlFlow, Event, WindowEvent, ElementState};
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput { input, .. } if ElementState::Pressed == input.state => {
if grabbed {
grabbed = false;
gl_window.set_cursor_state(CursorState::Normal)
.ok().expect("could not ungrab mouse cursor");
} else {
grabbed = true;
gl_window.set_cursor_state(CursorState::Grab)
.ok().expect("could not grab mouse cursor");
}
},
WindowEvent::Closed => return ControlFlow::Break,
WindowEvent::Resized(w, h) => gl_window.resize(w, h),
a @ WindowEvent::MouseMoved { .. } => {
println!("{:?}", a);
},
_ => (),
},
_ => (),
}
gl.draw_frame([0.0, 1.0, 0.0, 1.0]);
let _ = gl_window.swap_buffers();
ControlFlow::Continue
});
}
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 51
fn main() {
let mut events_loop = glutin::EventsLoop::new();
let mut windows = std::collections::HashMap::new();
for _ in 0..3 {
let window = glutin::WindowBuilder::new();
let context = glutin::ContextBuilder::new();
let gl_window = glutin::GlWindow::new(window, context, &events_loop).unwrap();
let _ = unsafe { gl_window.make_current() };
let gl = support::load(&gl_window);
let window_id = gl_window.id();
windows.insert(window_id, (gl_window, gl));
}
events_loop.run_forever(|event| {
println!("{:?}", event);
match event {
glutin::Event::WindowEvent { event, window_id } => match event {
glutin::WindowEvent::Resized(w, h) => {
windows[&window_id].0.resize(w, h)
},
glutin::WindowEvent::Closed => {
if windows.remove(&window_id).is_some() {
println!("Window with ID {:?} has been closed", window_id);
if windows.is_empty() {
return glutin::ControlFlow::Break;
}
}
},
_ => (),
},
_ => (),
}
for (i, window) in windows.values().enumerate() {
let mut color = [0.0, 0.0, 0.0, 1.0];
color[i] = 1.0; // Color each of the three windows a different color.
let _ = unsafe { window.0.make_current() };
window.1.draw_frame(color);
let _ = window.0.swap_buffers();
}
glutin::ControlFlow::Continue
});
}
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 51 52 53 54 55 56 57 58 59 60
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 mut events_loop = glutin::EventsLoop::new();
let window = glutin::WindowBuilder::new()
.with_title("Hello world!")
.with_fullscreen(monitor);
let context = glutin::ContextBuilder::new();
let gl_window = glutin::GlWindow::new(window, context, &events_loop).unwrap();
let _ = unsafe { gl_window.make_current() };
let gl = support::load(&gl_window);
events_loop.run_forever(|event| {
use glutin::{ControlFlow, Event, WindowEvent, VirtualKeyCode};
println!("{:?}", event);
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::Closed => return ControlFlow::Break,
WindowEvent::Resized(w, h) => gl_window.resize(w, h),
WindowEvent::KeyboardInput { input, .. } => {
if let Some(VirtualKeyCode::Escape) = input.virtual_keycode {
return ControlFlow::Break;
}
},
_ => (),
},
_ => (),
}
gl.draw_frame([0.0, 1.0, 0.0, 1.0]);
let _ = gl_window.swap_buffers();
ControlFlow::Continue
});
}
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 51 52 53 54 55
fn main() {
let mut events_loop = glutin::EventsLoop::new();
let window = glutin::WindowBuilder::new().with_title("A fantastic window!");
let context = glutin::ContextBuilder::new();
let gl_window = glutin::GlWindow::new(window, context, &events_loop).unwrap();
unsafe { gl_window.make_current().unwrap() };
let gl = support::load(&gl_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;
events_loop.run_forever(|event| {
use glutin::{ControlFlow, Event, WindowEvent, ElementState};
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::KeyboardInput {
input: glutin::KeyboardInput { state: ElementState::Pressed, .. }, ..
} => {
println!("Setting cursor to \"{:?}\"", cursors[cursor_idx]);
gl_window.set_cursor(cursors[cursor_idx]);
if cursor_idx < cursors.len() - 1 {
cursor_idx += 1;
} else {
cursor_idx = 0;
}
},
WindowEvent::Closed => return ControlFlow::Break,
WindowEvent::Resized(w, h) => gl_window.resize(w, h),
_ => (),
}
}
gl.draw_frame([0.0, 1.0, 0.0, 1.0]);
gl_window.swap_buffers().unwrap();
ControlFlow::Continue
});
}
sourcepub fn with_gl(self, request: GlRequest) -> Self
pub fn with_gl(self, request: GlRequest) -> Self
Sets how the backend should choose the OpenGL API and version.
sourcepub fn with_gl_profile(self, profile: GlProfile) -> Self
pub fn with_gl_profile(self, profile: GlProfile) -> Self
Sets the desired OpenGL context profile.
sourcepub fn with_gl_debug_flag(self, flag: bool) -> Self
pub fn with_gl_debug_flag(self, flag: bool) -> Self
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) -> Self
pub fn with_gl_robustness(self, robustness: Robustness) -> Self
Sets the robustness of the OpenGL context. See the docs of Robustness
.
sourcepub fn with_vsync(self, vsync: bool) -> Self
pub fn with_vsync(self, vsync: bool) -> Self
Requests that the window has vsync enabled.
By default, vsync is not enabled.
Share the display lists with the given Context
.
sourcepub fn with_multisampling(self, samples: u16) -> Self
pub fn with_multisampling(self, samples: u16) -> Self
Sets the multisampling level to request. A value of 0
indicates that multisampling must
not be enabled.
Panic
Will panic if samples
is not a power of two.
sourcepub fn with_depth_buffer(self, bits: u8) -> Self
pub fn with_depth_buffer(self, bits: u8) -> Self
Sets the number of bits in the depth buffer.
sourcepub fn with_stencil_buffer(self, bits: u8) -> Self
pub fn with_stencil_buffer(self, bits: u8) -> Self
Sets the number of bits in the stencil buffer.
sourcepub fn with_pixel_format(self, color_bits: u8, alpha_bits: u8) -> Self
pub fn with_pixel_format(self, color_bits: u8, alpha_bits: u8) -> Self
Sets the number of bits in the color buffer.
sourcepub fn with_stereoscopy(self) -> Self
pub fn with_stereoscopy(self) -> Self
Request the backend to be stereoscopic.