Skip to main content

Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

The overarching state of the crate. Intended to live outside of the main game loop.

This is the struct you can get a GraphicsContext from, and which should live as long as you’re drawing anything. Illustrated:

use fae::Context;
let mut fae_context: Context = Context::new();

loop {
    // Here's your gameloop, and now you want to draw something.

    // First, create the GraphicsContext with start_frame.
    let mut ctx: fae::GraphicsContext = fae_context.start_frame(width, height, dpi_factor);

    // Then do your rendering stuff.
    spritesheet.draw(&mut ctx)
        /* ... */
        .finish();

    // Finish frame and consume the GraphicsContext.
    ctx.finish_frame();

    // swap buffers, fae_context.synchronize(), etc.
}

This construct makes the state of fae more clear, as you can only have access to either the Context or the GraphicsContext, as well as providing a good synchronization point (start_frame) where the window’s state is passed to fae, ensuring that all rendering operations are done based on up-to-date information.

Implementations§

Source§

impl Context

Source

pub fn new() -> Context

Creates a new GraphicsContext. See the Safety section.

§Safety

Basically everything in fae assumes that it can call OpenGL, so please ensure you have called something along the lines of:

unsafe { fae::gl::load_with(|symbol| context.get_proc_address(symbol) as *const _); }

Before creating a Context.

The width, height and dpi_factor are only initial values; they are updated in the call to Context::start_frame().

Source

pub fn is_legacy(&self) -> bool

Returns true when running in legacy mode (OpenGL 3.3+ optimizations off).

Source

pub fn get_opengl_version(&self) -> &OpenGlVersion

Returns the OpenGL version if it could be parsed.

Source

pub fn synchronize(&mut self)

Tries to ensure that all the commands queued in the GPU have been processed.

Call this after swap_buffers to ensure that everything after this happens only after the frame has been sent to the screen, but don’t trust this to actually work. Doing vsync properly with OpenGL is a mess, as far as I know.

Source

pub fn start_frame( &mut self, width: f32, height: f32, dpi_factor: f32, ) -> GraphicsContext<'_>

Creates a GraphicsContext for this frame.

The parameters width and height are the dimensions of the window, and dpi_factor is a multiplier, such that: width * dpi_factor is the window’s width in physical pixels, and height * dpi_factor is the height in physical pixels.

Source

pub fn render( &mut self, width: f32, height: f32, clear_color: (f32, f32, f32, f32), )

Renders the frame with the given width, height and clear_color.

The clear_color is defined between 0.0 and 1.0, and the components are (red, green, blue, alpha).

See Context::start_frame for more information on what width and height are, specifically.

This should generally be called after GraphicsContext::finish_frame, but can also be used to redraw the previous frame.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.