Struct glutin::ContextBuilder

source ·
pub struct ContextBuilder<'a> {
    pub gl_attr: GlAttributes<&'a Context>,
    /* private fields */
}
Expand description

Object that allows you to build Contexts.

Fields§

§gl_attr: GlAttributes<&'a Context>

The attributes to use to create the context.

Implementations§

source§

impl<'a> ContextBuilder<'a>

source

pub fn new() -> Self

Initializes a new ContextBuilder with default values.

Examples found in repository?
examples/window.rs (line 10)
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
Hide additional examples
examples/transparent.rs (line 13)
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
    });
}
examples/multiwindow.rs (line 13)
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
    });
}
examples/grabbing.rs (line 10)
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("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::CursorMoved { .. } => {
                        println!("{:?}", a);
                    }
                    _ => (),
                }
            }
            _ => (),
        }

        gl.draw_frame([0.0, 1.0, 0.0, 1.0]);
        let _ = gl_window.swap_buffers();
        ControlFlow::Continue
    });
}
examples/fullscreen.rs (line 33)
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
61
fn main() {
    let mut events_loop = glutin::EventsLoop::new();

    // enumerating monitors
    let monitor = {
        for (num, monitor) in events_loop.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 = events_loop.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(Some(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
    });
}
examples/cursor.rs (line 10)
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
    });
}
source

pub fn with_gl(self, request: GlRequest) -> Self

Sets how the backend should choose the OpenGL API and version.

source

pub fn with_gl_profile(self, profile: GlProfile) -> Self

Sets the desired OpenGL context profile.

source

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.

source

pub fn with_gl_robustness(self, robustness: Robustness) -> Self

Sets the robustness of the OpenGL context. See the docs of Robustness.

source

pub fn with_vsync(self, vsync: bool) -> Self

Requests that the window has vsync enabled.

By default, vsync is not enabled.

source

pub fn with_shared_lists(self, other: &'a Context) -> Self

Share the display lists with the given Context.

source

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.

source

pub fn with_depth_buffer(self, bits: u8) -> Self

Sets the number of bits in the depth buffer.

source

pub fn with_stencil_buffer(self, bits: u8) -> Self

Sets the number of bits in the stencil buffer.

source

pub fn with_pixel_format(self, color_bits: u8, alpha_bits: u8) -> Self

Sets the number of bits in the color buffer.

source

pub fn with_stereoscopy(self) -> Self

Request the backend to be stereoscopic.

source

pub fn with_srgb(self, srgb_enabled: bool) -> Self

Sets whether sRGB should be enabled on the window.

The default value is false.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for ContextBuilder<'a>

§

impl<'a> Send for ContextBuilder<'a>

§

impl<'a> Sync for ContextBuilder<'a>

§

impl<'a> Unpin for ContextBuilder<'a>

§

impl<'a> UnwindSafe for ContextBuilder<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.