[−][src]Struct riddle_renderer_wgpu::Sprite
A renderable region of a texture.
Multiple sprites can share a single texture. Sprites can either be built using
SpriteBuilder
, or SpriteAtlasBuilder
.
Use SpriteRenderCommand
for access to all supported paramters when rendering
sprites, or use Sprite::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::new_from_png(std::io::Cursor::new(&png_bytes[..]))?; 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 Sprite
[src]
pub fn subsprite(&self, source_rect: &Rect<f32>) -> Sprite
[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]
&self,
render_ctx: &mut impl RenderContext,
location: P
) -> Result<(), RendererError>
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 !RefUnwindSafe for Sprite
impl Send for Sprite
impl Sync for Sprite
impl Unpin for Sprite
impl !UnwindSafe for Sprite
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> SetParameter for T
fn set<T>(&mut self, value: T) -> <T as Parameter<Self>>::Result where
T: Parameter<Self>,
T: Parameter<Self>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,