Struct Spritesheet

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

Holds a texture for rendering.

This struct is safe to clone in order to use elsewhere: the only data held by this struct is a handle to the internal draw call. Currently, it is not possible to delete draw calls, and the only way to create a new one is by using the SpritesheetBuilder.

§Example

let mut ctx = fae::Context::new();
use fae::SpritesheetBuilder;

// Creating the spritesheet:
let spritesheet = SpritesheetBuilder::default()
    .image(an_image)
    .alpha_blending(false)
    .build(&mut ctx);

// Later, in rendering code:
let mut ctx = ctx.start_frame(width, height, dpi_factor);
spritesheet.draw(&mut ctx)
    .coordinates((100.0, 100.0, 16.0, 16.0))
    .texture_coordinates((0, 0, 16, 16))
    .finish();

Implementations§

Source§

impl Spritesheet

Source

pub fn draw<'a, 'b>( &'b self, ctx: &'a mut GraphicsContext<'_>, ) -> Sprite<'a, 'b>

Creates a Sprite struct, a builder struct which defines the action “draw a sprite from this spritesheet onto the screen.” To “execute” the builder, call finish(). The parameters are set using the builder pattern.

§Optimization tips
  • If possible, make your textures without using alpha values between 1 and 0 (ie. use only 100% and 0% opacity), and disable alpha_blending in SpritesheetBuilder. These kinds of sprites can be drawn much more efficiently when it comes to overdraw.

  • If alpha_blending is disabled, draw the sprites in front first. This way you’ll avoid rendering over already drawn pixels. If you’re rendering lots of sprites, this is a good place to start optimizing.

    • Note: if alpha_blending is enabled, the you should draw the sprites in the back first, to ensure correct rendering.
Source

pub fn upload_texture_region<R: Into<Rect>>( &self, ctx: &mut GraphicsContext<'_>, region: R, image: &Image, ) -> bool

Upload an image into the specified region in the spritesheet.

As the inner values of region will be floored before use, it is recommended to use a (i32, i32, i32, i32) as the region parameter to ensure expected behavior.

If the width and height of region and image don’t match, or the region isn’t completely contained within the texture, this function will do nothing and return false.

See also: Image::with_null_texture.

Source

pub fn resize_texture( &self, ctx: &mut GraphicsContext<'_>, new_width: i32, new_height: i32, ) -> bool

Resize the spritesheet texture to a new width and height, which must be equal or greater than the original dimensions. The previous contents of the texture are preserved in the origin corner of the texture.

If new_width is less than the current width, or new_height is less than the current height, this function will do nothing and return false.

See also: Spritesheet::upload_texture_region.

Trait Implementations§

Source§

impl Clone for Spritesheet

Source§

fn clone(&self) -> Spritesheet

Returns a duplicate 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 Spritesheet

Source§

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

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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