Struct riddle_renderer_wgpu::wgpu_ext::WGPUSprite[][src]

pub struct WGPUSprite<Device: WGPUDevice> { /* fields omitted */ }

A renderable region of a texture.

Multiple sprites can share a single texture. Sprites can either be built using crate::SpriteBuilder, or SpriteAtlasBuilder.

Use crate::SpriteRenderCommand for access to all supported paramters when rendering sprites, or use WGPUSprite::render_at to specify only a location and use default arguments for everything else.

Sprites store a reference to the Renderer which built it, which will keep the renderer alive as long as the sprite is alive.

Example

let renderer = Renderer::new_from_window(&window)?;

// Load an image and create a sprite from it
let png_bytes = include_bytes!("../../../example_assets/image.png");
let img = Image::load(&png_bytes[..], ImageFormat::Png)?;
let sprite = SpriteBuilder::new(img).build(&renderer)?;

// Render the sprite at the top left corner of the screen
let mut render_ctx = renderer.begin_render()?;
render_ctx.clear(Color::WHITE);
sprite.render_at(&mut render_ctx, vec2(0.0, 0.0))?;
render_ctx.present()?;

Implementations

impl<Device: WGPUDevice> WGPUSprite<Device>[src]

pub fn subsprite(&self, source_rect: &Rect<f32>) -> Self[src]

Build a sprite that shares the same underlying texture but represents a different portion of the texture.

Arguments

  • source_rect - The portion of the texture that the new sprite will render, relative to the current sprite’s bounds. The bounds of the output sprite will be the intersection of the sprite’s rect and the source_rect, so the dimensions of the output sprite may not match the source_rect dimensions.

Example

let renderer = Renderer::new_from_window(&window)?;

// Load an image and create a sprite from it
let img = Image::new(100, 100);
let sprite = SpriteBuilder::new(img).build(&renderer)?;

// Take a portion of the sprite as a new sprite.
let subsprite = sprite.subsprite(&Rect::new(vec2(75.0, 75.0), vec2(50.0, 50.0)));

// The subsprite dimensions will be the size of the intersection between the
// source sprite and the new bounds.
assert_eq!(vec2(25.0, 25.0), subsprite.dimensions());

pub fn render_at<P: Into<Vector2<f32>>>(
    &self,
    render_ctx: &mut impl RenderContext,
    location: P
) -> Result<(), RendererError>
[src]

Utility function to simply render the sprite at a given location

This is equivalent to SpriteRenderCommand::new(location).render(&mut ctx, &sprite)?;. See SpriteRenderCommand for how to render the sprite with more control.

pub fn dimensions(&self) -> Vector2<f32>[src]

Get the dimensions of the sprite

Example

let renderer = Renderer::new_from_window(&window)?;

// Load an image and create a sprite from it
let img = Image::new(100, 100);
let sprite = SpriteBuilder::new(img).build(&renderer)?;

// The sprite dimensions will be the same of the source image
assert_eq!(vec2(100.0, 100.0), sprite.dimensions());

Auto Trait Implementations

impl<Device> !RefUnwindSafe for WGPUSprite<Device>

impl<Device> Send for WGPUSprite<Device> where
    Device: Send + Sync

impl<Device> Sync for WGPUSprite<Device> where
    Device: Send + Sync

impl<Device> Unpin for WGPUSprite<Device>

impl<Device> !UnwindSafe for WGPUSprite<Device>

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> Instrument for T[src]

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

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

type Error = Infallible

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.