Struct Context

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

A struct storing the global state which is used for all operations which require access to the GPU.

§Examples

use crow::{
    glutin::{
        event::{Event, WindowEvent},
        event_loop::{ControlFlow, EventLoop},
        window::WindowBuilder,
    },
    Context, DrawConfig, Texture,
};

fn main() -> Result<(), crow::Error> {
    let event_loop = EventLoop::new();
    let mut ctx = Context::new(WindowBuilder::new(), &event_loop)?;

    let texture = Texture::load(&mut ctx, "./textures/player.png")?;

    event_loop.run(
        move |event: Event<()>, _window_target: _, control_flow: &mut ControlFlow| match event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                ..
            } => *control_flow = ControlFlow::Exit,
            Event::MainEventsCleared => ctx.window().request_redraw(),
            Event::RedrawRequested(_) => {
                let mut surface = ctx.surface();
                ctx.clear_color(&mut surface, (0.4, 0.4, 0.8, 1.0));
                ctx.draw(&mut surface, &texture, (100, 150), &DrawConfig::default());
                ctx.present(surface).unwrap();
            }
            _ => (),
        },
    )
}

Implementations§

Source§

impl Context

Source

pub fn new<T>( window: WindowBuilder, event_loop: &EventLoop<T>, ) -> Result<Self, NewContextError>

Creates a new Context. It is not possible to have more than one Context in a program.

To create a new Context after a previous context was used, The previous context has to be dropped using the method Context::unlock_unchecked(). This is a workaround and will probably be fixed in a future release.

Source

pub fn window_dimensions(&self) -> (u32, u32)

Returns the dimensions of the used window.

Source

pub fn window_width(&self) -> u32

Returns the width of the used window.

Source

pub fn window_height(&self) -> u32

Returns the height of the used window.

Source

pub fn resize_window(&mut self, width: u32, height: u32)

Sets the dimensions of the used window.

Source

pub fn maximum_texture_size(&self) -> (u32, u32)

Returns the size of the biggest supported texture.

Trying to create a texture with a size greater than maximum_texture_size results in an InvalidTextureSize error.

use crow::{Context, glutin::{window::WindowBuilder, event_loop::EventLoop}};

let mut ctx = Context::new(WindowBuilder::new(), &EventLoop::new()).unwrap();
println!("maximum supported texture size: {:?}", ctx.maximum_texture_size());
Source

pub fn draw<T>( &mut self, target: &mut T, source: &Texture, position: (i32, i32), config: &DrawConfig, )
where T: DrawTarget,

Draws the source onto target.

To draw to the window, use Context::window_surface as a target.

Source

pub fn debug_line<T>( &mut self, target: &mut T, from: (i32, i32), to: (i32, i32), color: (f32, f32, f32, f32), )
where T: DrawTarget,

Draws the a line going from from to to onto target with the given color.

To draw this line to the window, use Context::window_surface as a target.

Source

pub fn debug_rectangle<T>( &mut self, target: &mut T, lower_left: (i32, i32), upper_right: (i32, i32), color: (f32, f32, f32, f32), )
where T: DrawTarget,

Draws the bounding box of an axis-aligned rectangle specified by its lower_left and upper_right corner.

In case lower_left is to the right or above upper_right, the two points will be flipped.

To draw this rectangle to the window, use Context::window_surface as a target.

Source

pub fn clear_color<T>(&mut self, target: &mut T, color: (f32, f32, f32, f32))
where T: DrawTarget,

Clears the color of the given DrawTarget, setting each pixel to color

Source

pub fn clear_depth<T>(&mut self, target: &mut T)
where T: DrawTarget,

Resets the depth buffer of the given DrawTarget to 1.0.

Source

pub fn image_data<T>(&mut self, image: &T) -> RgbaImage
where T: DrawTarget,

Loads the current state of a DrawTarget into an image.

Source

pub fn window(&self) -> &Window

Returns the inner window.

§Examples
use crow::{
    glutin::{event_loop::EventLoop, window::WindowBuilder},
    Context,
};

let context = Context::new(
    WindowBuilder::new().with_title("Starting"),
    &EventLoop::new(),
)?;

context.window().set_title("Running");
Source

pub fn surface(&mut self) -> WindowSurface

Returns a handle to the window surface.

This handle implements DrawTarget and can be used to draw to the window.

Use fn Context::present to actually display the resulting image.

Source

pub fn present(&mut self, surface: WindowSurface) -> Result<(), FinalizeError>

Presents the current frame to the screen.

Source

pub unsafe fn unlock_unchecked(self)

Drops this context while allowing the initialization of a new one afterwards.

§Safety

This method may lead to undefined behavior if a struct, for example a Texture, which was created using the current context, is used with the new context.

Trait Implementations§

Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Context

§

impl !RefUnwindSafe for Context

§

impl !Send for Context

§

impl !Sync for Context

§

impl Unpin for Context

§

impl !UnwindSafe for Context

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.