Struct tetra::graphics::Canvas

source ·
pub struct Canvas { /* private fields */ }
Expand description

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.

Performance

Creating a canvas is quite an expensive operation. Try to reuse them, rather than recreating them every frame.

Switching which canvas you are rendering to can be also be slow, as it requires flushing any pending draw calls to the GPU. It’s usually a good idea to do your rendering to a canvas all in one go, if you can.

You can clone a canvas cheaply, as it is a reference-counted handle to a GPU resource. However, this does mean that modifying a canvas (e.g. drawing to it) will also affect any clones that exist of it.

Examples

The canvas example demonstrates how to draw to a canvas, and then draw that canvas to the screen.

Implementations§

source§

impl Canvas

source

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

Creates a new canvas, with the default settings:

Errors
source

pub fn builder(width: i32, height: i32) -> CanvasBuilder

Creates a new canvas builder, which can be used to create a canvas with more advanced configurations, such as multisampling or stencil buffers.

source

pub fn draw<P>(&self, ctx: &mut Context, params: P)where P: Into<DrawParams>,

Draws the canvas to the screen (or to another canvas, if one is enabled).

source

pub fn width(&self) -> i32

Returns the width of the canvas.

source

pub fn height(&self) -> i32

Returns the height of the canvas.

source

pub fn size(&self) -> (i32, i32)

Returns the size of the canvas.

source

pub fn filter_mode(&self) -> FilterMode

Returns the filter mode being used by the canvas.

source

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

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

source

pub fn get_data(&self, ctx: &mut Context) -> ImageData

Gets the canvas’ data from the GPU.

This can be useful if you need to do some image processing on the CPU, or if you want to output the image data somewhere. This is a fairly slow operation, so avoid doing it too often!

If this is the currently active canvas, you should unbind it or call graphics::flush before calling this method, to ensure all pending draw calls are reflected in the output. Similarly, if the canvas is multisampled, it must be resolved before changes will be reflected in this method’s output.

source

pub fn set_data( &self, ctx: &mut Context, x: i32, y: i32, width: i32, height: i32, data: &[u8] ) -> Result

Writes pixel data to a specified region of the canvas.

The data will be interpreted based on the TextureFormat of the canvas’ underlying texture.

This method requires you to provide enough data to fill the target rectangle. If you provide too little data, an error will be returned. If you provide too much data, it will be truncated.

If you want to overwrite the entire canvas, the replace_data method offers a more concise way of doing this.

Errors
  • TetraError::NotEnoughData will be returned if not enough data is provided to fill the target rectangle. This is to prevent the graphics API from trying to read uninitialized memory.
Panics

Panics if any part of the target rectangle is outside the bounds of the canvas.

source

pub fn replace_data(&self, ctx: &mut Context, data: &[u8]) -> Result

Overwrites the entire canvas with new pixel data.

The data will be interpreted based on the TextureFormat of the canvas’ underlying texture.

This method requires you to provide enough data to fill the canvas. If you provide too little data, an error will be returned. If you provide too much data, it will be truncated.

If you only want to write to a subsection of the canvas, use the set_data method instead.

Errors
  • TetraError::NotEnoughData will be returned if not enough data is provided to fill the target rectangle. This is to prevent the graphics API from trying to read uninitialized memory.
source

pub fn texture(&self) -> &Texture

Returns a reference to the canvas’ underlying texture.

If this is the currently active canvas, you may want to unbind it or call graphics::flush before trying to access the underlying texture data, to ensure all pending draw calls are completed. Similarly, if the canvas is multisampled, it must be resolved before changes will be reflected in the texture.

Trait Implementations§

source§

impl Clone for Canvas

source§

fn clone(&self) -> Canvas

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Canvas

source§

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

Formats the value using the given formatter. Read more
source§

impl PartialEq<Canvas> for Canvas

source§

fn eq(&self, other: &Canvas) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for Canvas

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§

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

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

const: unstable · 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.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · 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.
const: unstable · source§

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

Performs the conversion.
§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,