cogl/auto/
context.rs

1use crate::{Display, Object, Renderer};
2
3use glib::translate::*;
4use std::{fmt, ptr};
5
6glib_wrapper! {
7    pub struct Context(Object<ffi::CoglContext, ContextClass>) @extends Object;
8
9    match fn {
10        get_type => || ffi::cogl_context_get_gtype(),
11    }
12}
13
14impl Context {
15    /// Creates a new `Context` which acts as an application sandbox
16    /// for any state objects that are allocated.
17    /// ## `display`
18    /// A `Display` pointer
19    ///
20    /// # Returns
21    ///
22    /// A newly allocated `Context`
23    pub fn new(display: Option<&Display>) -> Result<Context, glib::Error> {
24        unsafe {
25            let mut error = ptr::null_mut();
26            let ret = ffi::cogl_context_new(display.to_glib_none().0, &mut error);
27            if error.is_null() {
28                Ok(from_glib_full(ret))
29            } else {
30                Err(from_glib_full(error))
31            }
32        }
33    }
34
35    /// Retrieves the `Display` that is internally associated with the
36    /// given `self`. This will return the same `Display` that was
37    /// passed to `Context::new` or if `None` was passed to
38    /// `Context::new` then this function returns a pointer to the
39    /// display that was automatically setup internally.
40    ///
41    /// # Returns
42    ///
43    /// The `Display` associated with the
44    ///  given `self`.
45    pub fn get_display(&self) -> Option<Display> {
46        unsafe { from_glib_none(ffi::cogl_context_get_display(self.to_glib_none().0)) }
47    }
48
49    /// Retrieves the `Renderer` that is internally associated with the
50    /// given `self`. This will return the same `Renderer` that was
51    /// passed to `Display::new` or if `None` was passed to
52    /// `Display::new` or `Context::new` then this function returns
53    /// a pointer to the renderer that was automatically connected
54    /// internally.
55    ///
56    /// # Returns
57    ///
58    /// The `Renderer` associated with the
59    ///  given `self`.
60    pub fn get_renderer(&self) -> Option<Renderer> {
61        unsafe { from_glib_none(ffi::cogl_context_get_renderer(self.to_glib_none().0)) }
62    }
63}
64
65impl fmt::Display for Context {
66    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67        write!(f, "Context")
68    }
69}