Struct tetra::graphics::Texture

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

A texture, held in GPU memory.

The data can be stored in a variety of formats, as represented by the TextureFormat enum.

Supported File Formats

Images can be decoded from various common file formats via the new and from_encoded constructors. Individual decoders can be enabled or disabled via Cargo feature flags.

FormatCargo featureEnabled by default?
PNGtexture_pngYes
JPEGtexture_jpegYes
GIFtexture_gifYes
BMPtexture_bmpYes
TIFFtexture_tiffNo
TGAtexture_tgaNo
WebPtexture_webpNo
ICOtexture_icoNo
PNMtexture_pnmNo
DDS/DXTtexture_ddsNo

Performance

Creating a texture is quite an expensive operation, as it involves ‘uploading’ the texture data to the GPU. Try to reuse textures, rather than recreating them every frame.

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

Examples

The texture example demonstrates how to draw a simple texture.

Implementations§

source§

impl Texture

source

pub fn new<P>(ctx: &mut Context, path: P) -> Result<Texture>where P: AsRef<Path>,

Creates a new texture from the given file.

The format will be determined based on the file extension.

Errors
source

pub fn from_data( ctx: &mut Context, width: i32, height: i32, format: TextureFormat, data: &[u8] ) -> Result<Texture>

Creates a new texture from a slice of pixel data.

This is useful if you wish to create a texture at runtime.

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

Errors
  • TetraError::PlatformError will be returned if the underlying graphics API encounters an error.
  • TetraError::NotEnoughData will be returned if not enough data is provided to fill the texture. This is to prevent the graphics API from trying to read uninitialized memory.
source

pub fn from_encoded(ctx: &mut Context, data: &[u8]) -> Result<Texture>

Creates a new texture from a slice of data, encoded in one of Tetra’s supported file formats (except for TGA).

This is useful in combination with include_bytes, as it allows you to include your textures directly in the binary.

The format will be determined based on the ‘magic bytes’ at the beginning of the data. This should be reasonably reliable, but a from_data_with_format function might have to be added later. Note that TGA files do not have recognizable magic bytes, so this function will not recognize them.

Errors
source

pub fn from_image_data(ctx: &mut Context, data: &ImageData) -> Result<Texture>

Creates a new texture from an ImageData.

Errors
source

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

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

source

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

Draws a region of the texture to the screen (or to a canvas, if one is enabled).

source

pub fn draw_nine_slice<P>( &self, ctx: &mut Context, config: &NineSlice, width: f32, height: f32, params: P )where P: Into<DrawParams>,

Draws a region of the texture by splitting it into nine slices, allowing it to be stretched or squashed without distorting the borders.

source

pub fn width(&self) -> i32

Returns the width of the texture.

source

pub fn height(&self) -> i32

Returns the height of the texture.

source

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

Returns the size of the texture.

source

pub fn format(&self) -> TextureFormat

Returns the data format of the texture.

source

pub fn filter_mode(&self) -> FilterMode

Returns the filter mode being used by the texture.

source

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

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

source

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

Gets the texture’s 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!

The returned ImageData will have the same format as the texture itself.

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 texture.

The data will be interpreted based on the TextureFormat of the 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 texture, 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 texture.

source

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

Overwrites the entire texture with new RGBA pixel data.

This method requires you to provide enough data to fill the texture. 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 texture, use the set_data method instead.

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

Trait Implementations§

source§

impl Clone for Texture

source§

fn clone(&self) -> Texture

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 Texture

source§

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

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

impl PartialEq<Texture> for Texture

source§

fn eq(&self, other: &Texture) -> 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 Texture

source§

impl UniformValue for Texture

Can be accessed via a sampler2D in your shader.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Texture

§

impl !Send for Texture

§

impl !Sync for Texture

§

impl Unpin for Texture

§

impl !UnwindSafe for Texture

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>,