Struct pixel_engine::Sprite
source · pub struct Sprite { /* private fields */ }
Expand description
Represent a Sprite
Implementations
sourceimpl Sprite
impl Sprite
pub fn size(&self) -> &Vec2d<u32>
pub fn width(&self) -> u32
pub fn height(&self) -> u32
pub fn get_read_lock(&self) -> (&mut [u8], RwLockWriteGuard<'_, RawRwLock, ()>)
sourcepub fn load_rgba(
rgba: &[u8],
width: usize,
height: usize
) -> Result<Sprite, String>
pub fn load_rgba(
rgba: &[u8],
width: usize,
height: usize
) -> Result<Sprite, String>
Load an rgba slice as an Sprite, will clone the slice the dimentions are in pixel
Errors
if the slice isn’t an rgba slice it will return an Err
this means that slice.len() % 4 = 0
and that slice.len() = width * height * 4
sourcepub fn create_sub_sprite(
&self,
pos: Vec2d<i32>,
size: Vec2d<u32>
) -> Result<SpriteMutRef<'_>, OverlappingError>
pub fn create_sub_sprite(
&self,
pos: Vec2d<i32>,
size: Vec2d<u32>
) -> Result<SpriteMutRef<'_>, OverlappingError>
Creates a subsprite on a given parent sprite
The real position will be clamped to the sprite dimentions, so if the pos
has some
negative parts, it will be clipped, only showing the positive part.
Same with if the size + pos
goes outside of the sprite.
Errors
This will return an error if an subsprite already exists and that it overlaps
sourcepub unsafe fn create_sub_sprite_unchecked(
&self,
pos: Vec2d<u32>,
size: Vec2d<u32>,
index: usize
) -> SpriteMutRef<'_>
pub unsafe fn create_sub_sprite_unchecked(
&self,
pos: Vec2d<u32>,
size: Vec2d<u32>,
index: usize
) -> SpriteMutRef<'_>
Creates a subsprite without any checks
Safety
- You need to make sure there is no overlap with another subsprite
- You need to make sure that the size is correct and fit inside the base sprites
- You need to make sure that the area has been inserted in the area list and that the given index is the one pointing to the subsprite’s area;
sourcepub unsafe fn get_areas(&self) -> &Mutex<RawMutex, Slab<Area>>
pub unsafe fn get_areas(&self) -> &Mutex<RawMutex, Slab<Area>>
This is used when using create_sub_sprite_unchecked
()
Safety
Editing this could lead to overlapping subsprite so be careful when handling the list (slab) of areas.
sourcepub fn load_image_bytes(bytes: &[u8]) -> Result<Sprite, String>
pub fn load_image_bytes(bytes: &[u8]) -> Result<Sprite, String>
Load an image from bytes, will clone the slice
Errors
If the slice isn’t an valid image format handled by the image crate, returns an error
sourcepub fn load_from_file<P>(path: P) -> Result<Sprite, String>where
P: AsRef<Path>,
pub fn load_from_file<P>(path: P) -> Result<Sprite, String>where
P: AsRef<Path>,
Load a image file and return a Sprite object representing that image
Errors
If the file isn’t an valid image format handled by the image crate or if the file IO failed, returns an error
sourcepub fn get_pixel(&self, x: u32, y: u32) -> Color
pub fn get_pixel(&self, x: u32, y: u32) -> Color
Return the Color of the pixel at given coordinates, if it exist
Trait Implementations
sourceimpl DrawSpriteTrait for Sprite
impl DrawSpriteTrait for Sprite
sourceunsafe fn get_pixel_unchecked(&self, pos: Vec2d<u32>) -> Color
unsafe fn get_pixel_unchecked(&self, pos: Vec2d<u32>) -> Color
Safety
its up to the caller to make sure that the given position is in bounds
sourceunsafe fn set_pixel_unchecked(&mut self, pos: Vec2d<u32>, col: Color)
unsafe fn set_pixel_unchecked(&mut self, pos: Vec2d<u32>, col: Color)
Safety
its up to the caller to make sure that the given position is in bounds