[][src]Struct tetra::graphics::Canvas

pub struct Canvas { /* fields omitted */ }

A texture that can be used for off-screen rendering.

This is sometimes referred to as a 'render texture' or 'render target' in other frameworks.

Canvases can be useful if you want to do some rendering upfront and then cache the result (e.g. a static background), or if you want to apply transformations/shaders to multiple things simultaneously.

Note that creating a canvas is a relatively expensive operation! You rarely (if ever) should create them in your draw or update methods. Instead, add it as a member of your State struct.

Examples

struct GameState {
    canvas: Canvas,
}

impl GameState {
    fn new(ctx: &mut Context) -> tetra::Result<GameState> {
        Ok(GameState {
            canvas: Canvas::new(ctx, 640, 480)?
        })
    }
}

impl State for GameState {
    fn draw(&mut self, ctx: &mut Context) -> tetra::Result {
        graphics::set_canvas(ctx, &self.canvas);

        // Draw some stuff to the canvas here, using the normal graphics API.

        // When you're done, reset the canvas:
        graphics::reset_canvas(ctx);

        // Now you can draw the canvas to the screen:
        graphics::clear(ctx, Color::BLACK);
        graphics::draw(ctx, &self.canvas, Vec2::new(0.0, 0.0));

        Ok(())
    }
}

Methods

impl Canvas[src]

pub fn new(ctx: &mut Context, width: i32, height: i32) -> Result<Canvas>[src]

Creates a new canvas.

Errors

  • TetraError::PlatformError will be returned if the underlying graphics API encounters an error.

pub fn width(&self) -> i32[src]

Returns the width of the canvas.

pub fn height(&self) -> i32[src]

Returns the height of the canvas.

pub fn size(&self) -> (i32, i32)[src]

Returns the size of the canvas.

pub fn filter_mode(&self) -> FilterMode[src]

Returns the filter mode being used by the canvas.

pub fn set_filter_mode(&mut self, ctx: &mut Context, filter_mode: FilterMode)[src]

Sets the filter mode that should be used by the canvas.

pub fn texture(&self) -> &Texture[src]

Returns a reference to the canvas' underlying texture.

Trait Implementations

impl Clone for Canvas[src]

impl Debug for Canvas[src]

impl Drawable for Canvas[src]

impl PartialEq<Canvas> for Canvas[src]

impl StructuralPartialEq for Canvas[src]

Auto Trait Implementations

impl !RefUnwindSafe for Canvas

impl !Send for Canvas

impl !Sync for Canvas

impl Unpin for Canvas

impl !UnwindSafe for Canvas

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> SetParameter for T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,