Trait riddle_renderer_wgpu::CommonSprite[][src]

pub trait CommonSprite<R> where
    R: CommonRenderer
{ pub fn new_from_image(
        renderer: &R,
        img: &Image,
        init_args: &SpriteInitArgs
    ) -> Result<Self, RendererError>;
pub fn subsprite(&self, source_rect: &Rect<f32>) -> Self;
pub fn dimensions(&self) -> Vector2<f32>;
pub fn render_regions<Ctx>(
        &self,
        render_ctx: &mut Ctx,
        args: &SpriteRenderArgs,
        parts: &[(Rect<f32>, Vector2<f32>)]
    ) -> Result<(), RendererError>
    where
        Ctx: RenderContext<R> + ?Sized
; pub fn render<Ctx>(
        &self,
        render_ctx: &mut Ctx,
        args: &SpriteRenderArgs
    ) -> Result<(), RendererError>
    where
        Ctx: RenderContext<R>
, { ... }
pub fn render_at<Ctx>(
        &self,
        render_ctx: &mut Ctx,
        location: Vector2<f32>
    ) -> Result<(), RendererError>
    where
        Ctx: RenderContext<R>
, { ... } }

Sprites are conceptually both a reference to an image, and the sub region of the image which represents the logical sprite.

Required methods

pub fn new_from_image(
    renderer: &R,
    img: &Image,
    init_args: &SpriteInitArgs
) -> Result<Self, RendererError>
[src]

Construct a new sprite from an image. The image contents are copied to a texture in RGBA8 format. The entire image will be used

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 = Sprite::new_from_image(&renderer, &img, &SpriteInitArgs::new())?;

// 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 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 = Sprite::new_from_image(&renderer, &img, &SpriteInitArgs::new())?;

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

pub fn render_regions<Ctx>(
    &self,
    render_ctx: &mut Ctx,
    args: &SpriteRenderArgs,
    parts: &[(Rect<f32>, Vector2<f32>)]
) -> Result<(), RendererError> where
    Ctx: RenderContext<R> + ?Sized
[src]

Render multiple sub regions of the sprite at once.

The regions are defined by pairs of the region of the sprite to draw in texels, and where to position the region relative to the SpriteRenderArgs::location.

The pivot and rotation are relative to the location arg. A change in rotation will transform all rendered regions as one, not individually.

Loading content...

Provided methods

pub fn render<Ctx>(
    &self,
    render_ctx: &mut Ctx,
    args: &SpriteRenderArgs
) -> Result<(), RendererError> where
    Ctx: RenderContext<R>, 
[src]

Render the entire sprite.

pub fn render_at<Ctx>(
    &self,
    render_ctx: &mut Ctx,
    location: Vector2<f32>
) -> Result<(), RendererError> where
    Ctx: RenderContext<R>, 
[src]

Utility function to simply render the sprite at a given location

See SpriteRenderArgs for how to render the sprite with more control.

Loading content...

Implementors

impl<Device: WGPUDevice> CommonSprite<Renderer<Device>> for Sprite<Device>[src]

Loading content...