Skip to main content

Sprite

Struct Sprite 

Source
pub struct Sprite {
    pub texture: AssetHandle<TextureAsset>,
    pub texture_path: Option<String>,
    pub color: Color,
    pub source_rect: Option<Rect>,
    pub flip_x: bool,
    pub flip_y: bool,
    pub anchor: Vec2,
    pub custom_size: Option<Vec2>,
}
Expand description

A 2D sprite component for rendering textured quads.

The Sprite component defines how a texture should be rendered in 2D space. It must be paired with a Transform2D or GlobalTransform2D component to define the sprite’s position, rotation, and scale.

§Fields

  • texture: Handle to the texture asset to render
  • color: Color tint multiplied with texture pixels (default: white)
  • source_rect: Optional UV rectangle for sprite sheets (default: full texture)
  • flip_x: Flip the sprite horizontally (default: false)
  • flip_y: Flip the sprite vertically (default: false)
  • anchor: Normalized anchor point for rotation/positioning (default: center)
  • custom_size: Optional override for sprite size (default: texture size)

§Examples

§Basic Sprite

use goud_engine::ecs::components::Sprite;
use goud_engine::assets::{AssetServer, loaders::TextureAsset};

let mut asset_server = AssetServer::new();
let texture = asset_server.load::<TextureAsset>("player.png");

let sprite = Sprite::new(texture);

§Sprite with Custom Properties

use goud_engine::ecs::components::Sprite;
use goud_engine::core::math::{Color, Vec2};

let sprite = Sprite::new(texture)
    .with_color(Color::rgba(1.0, 0.5, 0.5, 0.8))
    .with_flip_x(true)
    .with_anchor(0.5, 1.0)
    .with_custom_size(Vec2::new(64.0, 64.0));

§Sprite Sheet Frame

use goud_engine::ecs::components::Sprite;
use goud_engine::core::math::Rect;

// Extract a 32x32 tile from the sprite sheet
let sprite = Sprite::new(texture)
    .with_source_rect(Rect::new(64.0, 32.0, 32.0, 32.0));

Fields§

§texture: AssetHandle<TextureAsset>

Handle to the texture asset to render.

§texture_path: Option<String>

Optional path to the texture asset for serialization.

When a scene is serialized the handle cannot be persisted, but this path string can. At load time a higher-level system resolves the path back to an AssetHandle.

§color: Color

Color tint multiplied with texture pixels.

Each component is in range [0.0, 1.0]. White (1, 1, 1, 1) renders the texture unmodified. RGB values tint the color, alpha controls transparency.

§source_rect: Option<Rect>

Optional source rectangle for sprite sheets and atlases.

If None, the entire texture is rendered. If Some, only the specified rectangle (in pixel coordinates) is rendered.

For normalized UV coordinates, multiply by texture dimensions.

§flip_x: bool

Flip the sprite horizontally.

When true, the texture is mirrored along the Y-axis.

§flip_y: bool

Flip the sprite vertically.

When true, the texture is mirrored along the X-axis.

§anchor: Vec2

Normalized anchor point for rotation and positioning.

Coordinates are in range [0.0, 1.0]:

  • (0.0, 0.0) = Top-left corner
  • (0.5, 0.5) = Center (default)
  • (1.0, 1.0) = Bottom-right corner

The anchor point is the origin for rotation and the point that aligns with the entity’s Transform2D position.

§custom_size: Option<Vec2>

Optional custom size override.

If None, the sprite renders at the texture’s pixel dimensions (or source_rect dimensions if specified). If Some, the sprite is scaled to this size.

Implementations§

Source§

impl Sprite

Source

pub fn new(texture: AssetHandle<TextureAsset>) -> Sprite

Creates a new sprite with default settings.

The sprite will render the entire texture with:

  • White color tint (no modification)
  • No source rectangle (full texture)
  • No flipping
  • Center anchor point (0.5, 0.5)
  • No custom size (uses texture dimensions)
§Example
use goud_engine::ecs::components::Sprite;

let sprite = Sprite::new(texture);
Source

pub fn with_texture_path(self, path: impl Into<String>) -> Sprite

Sets the texture asset path for serialization.

The path is stored alongside the sprite so it survives serialization. A higher-level system is responsible for resolving it back to an AssetHandle after deserialization.

§Example
use goud_engine::ecs::components::Sprite;

let sprite = Sprite::new(texture)
    .with_texture_path("player.png");
assert_eq!(sprite.texture_path.as_deref(), Some("player.png"));
Source

pub fn with_color(self, color: Color) -> Sprite

Sets the color tint for this sprite.

The color is multiplied with each texture pixel. Use white (1, 1, 1, 1) for no tinting.

§Example
use goud_engine::ecs::components::Sprite;
use goud_engine::core::math::Color;

let sprite = Sprite::new(texture)
    .with_color(Color::rgba(1.0, 0.0, 0.0, 0.5)); // Red, 50% transparent
Source

pub fn with_source_rect(self, rect: Rect) -> Sprite

Sets the source rectangle for sprite sheet rendering.

The rectangle is specified in pixel coordinates relative to the top-left corner of the texture.

§Example
use goud_engine::ecs::components::Sprite;
use goud_engine::core::math::Rect;

// Extract a 32x32 tile at position (64, 32)
let sprite = Sprite::new(texture)
    .with_source_rect(Rect::new(64.0, 32.0, 32.0, 32.0));
Source

pub fn without_source_rect(self) -> Sprite

Removes the source rectangle, rendering the full texture.

§Example
let mut sprite = Sprite::new(texture)
    .with_source_rect(Rect::new(0.0, 0.0, 32.0, 32.0));

sprite = sprite.without_source_rect();
assert!(sprite.source_rect.is_none());
Source

pub fn with_flip_x(self, flip: bool) -> Sprite

Sets the horizontal flip flag.

When true, the sprite is mirrored along the Y-axis.

§Example
use goud_engine::ecs::components::Sprite;

let sprite = Sprite::new(texture).with_flip_x(true);
Source

pub fn with_flip_y(self, flip: bool) -> Sprite

Sets the vertical flip flag.

When true, the sprite is mirrored along the X-axis.

§Example
use goud_engine::ecs::components::Sprite;

let sprite = Sprite::new(texture).with_flip_y(true);
Source

pub fn with_flip(self, flip_x: bool, flip_y: bool) -> Sprite

Sets both flip flags at once.

§Example
use goud_engine::ecs::components::Sprite;

let sprite = Sprite::new(texture).with_flip(true, true);
Source

pub fn with_anchor(self, x: f32, y: f32) -> Sprite

Sets the anchor point with individual coordinates.

Coordinates are normalized in range [0.0, 1.0]:

  • (0.0, 0.0) = Top-left
  • (0.5, 0.5) = Center
  • (1.0, 1.0) = Bottom-right
§Example
use goud_engine::ecs::components::Sprite;

// Bottom-center anchor for ground-aligned sprites
let sprite = Sprite::new(texture).with_anchor(0.5, 1.0);
Source

pub fn with_anchor_vec(self, anchor: Vec2) -> Sprite

Sets the anchor point from a Vec2.

§Example
use goud_engine::ecs::components::Sprite;
use goud_engine::core::math::Vec2;

let sprite = Sprite::new(texture)
    .with_anchor_vec(Vec2::new(0.5, 1.0));
Source

pub fn with_custom_size(self, size: Vec2) -> Sprite

Sets a custom size for the sprite.

When set, the sprite is scaled to this size regardless of the texture dimensions.

§Example
use goud_engine::ecs::components::Sprite;
use goud_engine::core::math::Vec2;

// Force sprite to 64x64 size
let sprite = Sprite::new(texture)
    .with_custom_size(Vec2::new(64.0, 64.0));
Source

pub fn without_custom_size(self) -> Sprite

Removes the custom size, using texture dimensions.

§Example
use goud_engine::ecs::components::Sprite;
use goud_engine::core::math::Vec2;

let mut sprite = Sprite::new(texture)
    .with_custom_size(Vec2::new(64.0, 64.0));

sprite = sprite.without_custom_size();
assert!(sprite.custom_size.is_none());
Source

pub fn size_or_rect(&self) -> Vec2

Gets the effective size of the sprite.

Returns the custom size if set, otherwise the source rect size if set, otherwise falls back to a default size (requires texture dimensions).

For actual rendering, you’ll need to query the texture asset to get its dimensions when custom_size and source_rect are both None.

§Example
use goud_engine::ecs::components::Sprite;
use goud_engine::core::math::{Vec2, Rect};

let sprite = Sprite::new(texture)
    .with_source_rect(Rect::new(0.0, 0.0, 32.0, 32.0));

let size = sprite.size_or_rect();
assert_eq!(size, Vec2::new(32.0, 32.0));
Source

pub fn has_source_rect(&self) -> bool

Returns true if the sprite has a source rectangle set.

Source

pub fn has_custom_size(&self) -> bool

Returns true if the sprite has a custom size set.

Source

pub fn is_flipped(&self) -> bool

Returns true if the sprite is flipped on either axis.

Trait Implementations§

Source§

impl Clone for Sprite

Source§

fn clone(&self) -> Sprite

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 Sprite

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Sprite

Source§

fn default() -> Sprite

Creates a sprite with an invalid texture handle.

This is primarily useful for deserialization or when the texture will be set later. The sprite will not render correctly until a valid texture handle is assigned.

Source§

impl<'de> Deserialize<'de> for Sprite

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Sprite, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Sprite

Source§

fn eq(&self, other: &Sprite) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Sprite

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Component for Sprite

Source§

impl StructuralPartialEq for Sprite

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<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
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> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

impl<T> QueryState for T
where T: Send + Sync + Clone + 'static,

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,