pub struct BlendableSurface<'s, S: AsRef<[[f32; 4]]> + AsMut<[[f32; 4]]>> { /* private fields */ }Expand description
A surface that allows you to blend pixels rather than copying them onto each other.
The underlying Surface must use [f32; 4] pixels, such as crate::surface::Rgba32Surface
use blittle::{BlendMode, BlendableSurface, PositionI, Rgba32Surface, Size};
// Define a [f32; 4] surface.
let mut bottom = Rgba32Surface::new_filled(Size { width: 256, height: 256 }, [0.1, 0.3, 0.5, 0.9]);
// We will blend this surface on top of `bottom`.
let mut top = Rgba32Surface::new_filled(Size { width: 64, height: 120 }, [0.3, 0.95, 0.1, 1.]);
// Set the top surface's position relative to the bottom surface.
top.set_position(PositionI::new(1, 5), &bottom);
// Feed the top layer into a BlendableSurface.
let top = BlendableSurface::new(top);
// Blend.
let blend_mode = BlendMode::Overlay;
let alpha = 0.5;
top.blend(blend_mode, alpha, &mut bottom).unwrap();A BlendableSurface can be locked or unlocked.
A locked BlendableSurface usually blends faster but can’t mutate its underlying surface pixel buffer.
When initially locked, a BlendableSurface caches the position of every non-transparent pixel.
In the above example, locking top would be make blending slightly slower
because the surface is filled with a uniform color.
Locking is useful when some pixels are transparent and some are not.
Implementations§
Source§impl<'s, S: AsRef<[[f32; 4]]> + AsMut<[[f32; 4]]>> BlendableSurface<'s, S>
impl<'s, S: AsRef<[[f32; 4]]> + AsMut<[[f32; 4]]>> BlendableSurface<'s, S>
pub fn new(surface: Surface<'s, S, [f32; 4]>) -> Self
Sourcepub const fn surface(&self) -> &Surface<'s, S, [f32; 4]>
pub const fn surface(&self) -> &Surface<'s, S, [f32; 4]>
Returns a reference to the underlying surface.
Sourcepub const fn surface_mut(
&mut self,
) -> Result<&mut Surface<'s, S, [f32; 4]>, Error>
pub const fn surface_mut( &mut self, ) -> Result<&mut Surface<'s, S, [f32; 4]>, Error>
Returns a mutable reference of the surface. Returns an error if the masked surface is locked.
Sourcepub fn blend<B: AsRef<[[f32; 4]]> + AsMut<[[f32; 4]]>>(
&self,
blend_mode: BlendMode,
alpha: f32,
other: &mut Surface<'s, B, [f32; 4]>,
) -> Result<(), Error>
pub fn blend<B: AsRef<[[f32; 4]]> + AsMut<[[f32; 4]]>>( &self, blend_mode: BlendMode, alpha: f32, other: &mut Surface<'s, B, [f32; 4]>, ) -> Result<(), Error>
Blend into other using the blend_mode and an alpha transparency value (0-1).
The alpha value is applied to each pixel.
A value of 0 means that other’s pixels will be the original (non-blended) pixels.
A value of 1 means that other’s pixels will be the blended pixels.
A value of 0.5 means that other’s pixels will be halfway between the original and blended pixels.
Sourcepub fn lock(&mut self)
Available on crate feature std only.
pub fn lock(&mut self)
std only.Lock the surface, optimizing blit speed while preventing pixel manipulation.
Auto Trait Implementations§
impl<'s, S> Freeze for BlendableSurface<'s, S>where
S: Freeze,
impl<'s, S> RefUnwindSafe for BlendableSurface<'s, S>where
S: RefUnwindSafe,
impl<'s, S> Send for BlendableSurface<'s, S>where
S: Send,
impl<'s, S> Sync for BlendableSurface<'s, S>where
S: Sync,
impl<'s, S> Unpin for BlendableSurface<'s, S>where
S: Unpin,
impl<'s, S> UnsafeUnpin for BlendableSurface<'s, S>where
S: UnsafeUnpin,
impl<'s, S> UnwindSafe for BlendableSurface<'s, S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.