Trait riddle_renderer_common::CommonSprite [−][src]
pub trait CommonSprite<R: CommonRenderer>: Sized { fn new_from_image(
renderer: &R,
img: &Image,
init_args: &SpriteInitArgs
) -> Result<Self, RendererError>; fn subsprite(&self, source_rect: &Rect<f32>) -> Self; fn dimensions(&self) -> Vector2<f32>; fn render_regions<Ctx: RenderContext<R> + ?Sized>(
&self,
render_ctx: &mut Ctx,
args: &SpriteRenderArgs,
parts: &[(Rect<f32>, Vector2<f32>)]
) -> Result<(), RendererError>; fn render<Ctx: RenderContext<R>>(
&self,
render_ctx: &mut Ctx,
args: &SpriteRenderArgs
) -> Result<(), RendererError> { ... } fn render_at<Ctx: RenderContext<R>>(
&self,
render_ctx: &mut Ctx,
location: Vector2<f32>
) -> Result<(), RendererError> { ... } }
Sprites are conceptually both a reference to an image, and the sub region of the image which represents the logical sprite.
Required methods
fn new_from_image(
renderer: &R,
img: &Image,
init_args: &SpriteInitArgs
) -> Result<Self, RendererError>
[src]
renderer: &R,
img: &Image,
init_args: &SpriteInitArgs
) -> Result<Self, RendererError>
Construct a new sprite from an image. The image contents are copied to a texture in RGBA8 format. The entire image will be used
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());
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());
fn render_regions<Ctx: RenderContext<R> + ?Sized>(
&self,
render_ctx: &mut Ctx,
args: &SpriteRenderArgs,
parts: &[(Rect<f32>, Vector2<f32>)]
) -> Result<(), RendererError>
[src]
&self,
render_ctx: &mut Ctx,
args: &SpriteRenderArgs,
parts: &[(Rect<f32>, Vector2<f32>)]
) -> Result<(), RendererError>
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.
Provided methods
fn render<Ctx: RenderContext<R>>(
&self,
render_ctx: &mut Ctx,
args: &SpriteRenderArgs
) -> Result<(), RendererError>
[src]
&self,
render_ctx: &mut Ctx,
args: &SpriteRenderArgs
) -> Result<(), RendererError>
Render the entire sprite.
fn render_at<Ctx: RenderContext<R>>(
&self,
render_ctx: &mut Ctx,
location: Vector2<f32>
) -> Result<(), RendererError>
[src]
&self,
render_ctx: &mut Ctx,
location: Vector2<f32>
) -> Result<(), RendererError>
Utility function to simply render the sprite at a given location
See SpriteRenderArgs
for how to render the sprite with more control.