Skip to main content

TextureAsset

Struct TextureAsset 

Source
pub struct TextureAsset {
    pub data: Vec<u8>,
    pub width: u32,
    pub height: u32,
    pub format: TextureFormat,
}
Expand description

A loaded texture asset containing image data.

TextureAsset stores decoded image data in memory. It does not contain GPU resources - those should be created separately from this data.

§Fields

  • data: Raw pixel data in RGBA8 format (4 bytes per pixel)
  • width: Image width in pixels
  • height: Image height in pixels
  • format: The original image format (PNG, JPG, etc.)

§Example

use goud_engine::assets::{Asset, loaders::TextureAsset};

let texture = TextureAsset {
    data: vec![255; 64 * 64 * 4], // 64x64 white texture
    width: 64,
    height: 64,
    format: goud_engine::assets::loaders::TextureFormat::Png,
};

assert_eq!(texture.pixel_count(), 64 * 64);
assert_eq!(texture.bytes_per_pixel(), 4);

Fields§

§data: Vec<u8>

Raw pixel data in RGBA8 format (4 bytes per pixel).

§width: u32

Width of the texture in pixels.

§height: u32

Height of the texture in pixels.

§format: TextureFormat

The original image format this texture was loaded from.

Implementations§

Source§

impl TextureAsset

Source

pub fn new( data: Vec<u8>, width: u32, height: u32, format: TextureFormat, ) -> Self

Creates a new texture asset from raw RGBA8 data.

§Arguments
  • data: Raw pixel data in RGBA8 format (must be width × height × 4 bytes)
  • width: Image width in pixels
  • height: Image height in pixels
  • format: The image format this data represents
§Panics

Panics if data length doesn’t match width × height × 4.

§Example
use goud_engine::assets::loaders::{TextureAsset, TextureFormat};

let data = vec![255; 4 * 4 * 4]; // 4x4 white texture
let texture = TextureAsset::new(data, 4, 4, TextureFormat::Png);
assert_eq!(texture.pixel_count(), 16);
Source

pub fn pixel_count(&self) -> u32

Returns the total number of pixels in the texture.

Source

pub const fn bytes_per_pixel(&self) -> u32

Returns the number of bytes per pixel (always 4 for RGBA8).

Source

pub fn size_bytes(&self) -> usize

Returns the total size of the texture data in bytes.

Source

pub fn aspect_ratio(&self) -> f32

Returns the aspect ratio (width / height) of the texture.

Source

pub fn is_power_of_two(&self) -> bool

Returns true if the texture dimensions are powers of two.

Power-of-two textures are required by some older GPUs and can be more efficient for certain operations.

Source

pub fn get_pixel(&self, x: u32, y: u32) -> Option<&[u8]>

Returns a slice of the pixel data for a specific pixel.

Returns None if the coordinates are out of bounds.

§Example
use goud_engine::assets::loaders::{TextureAsset, TextureFormat};

let data = vec![255, 0, 0, 255, 0, 255, 0, 255]; // 2 pixels: red, green
let texture = TextureAsset::new(data, 2, 1, TextureFormat::Png);

let pixel = texture.get_pixel(0, 0).unwrap();
assert_eq!(pixel, &[255, 0, 0, 255]); // Red

let pixel = texture.get_pixel(1, 0).unwrap();
assert_eq!(pixel, &[0, 255, 0, 255]); // Green

Trait Implementations§

Source§

impl Asset for TextureAsset

Source§

fn asset_type_name() -> &'static str

Returns the human-readable name of this asset type. Read more
Source§

fn asset_type() -> AssetType

Returns the category of this asset type. Read more
Source§

fn extensions() -> &'static [&'static str]

Returns the file extensions typically associated with this asset type. Read more
Source§

impl Clone for TextureAsset

Source§

fn clone(&self) -> TextureAsset

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 TextureAsset

Source§

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

Formats the value using the given formatter. Read more

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<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<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,