Struct sdl2::surface::Surface

source ·
pub struct Surface<'a> { /* private fields */ }
Expand description

Holds a Rc<SurfaceContext>.

Note: If a Surface goes out of scope but it cloned its context, then the SDL_Surface will not be free’d until there are no more references to the SurfaceContext.

Implementations§

source§

impl<'a> Surface<'a>

source

pub unsafe fn from_ll<'b>(raw: *mut SDL_Surface) -> Surface<'b>

source

pub fn new( width: u32, height: u32, format: PixelFormatEnum ) -> Result<Surface<'static>, String>

Creates a new surface using a pixel format.

Example
use sdl2::pixels::PixelFormatEnum;
use sdl2::surface::Surface;

let surface = Surface::new(512, 512, PixelFormatEnum::RGB24).unwrap();
source

pub fn from_pixelmasks( width: u32, height: u32, masks: &PixelMasks ) -> Result<Surface<'static>, String>

Creates a new surface using pixel masks.

Example
use sdl2::pixels::PixelFormatEnum;
use sdl2::surface::Surface;

let masks = PixelFormatEnum::RGB24.into_masks().unwrap();
let surface = Surface::from_pixelmasks(512, 512, &masks).unwrap();
source

pub fn from_data( data: &'a mut [u8], width: u32, height: u32, pitch: u32, format: PixelFormatEnum ) -> Result<Surface<'a>, String>

Creates a new surface from an existing buffer, using a pixel format.

source

pub fn from_data_pixelmasks( data: &'a mut [u8], width: u32, height: u32, pitch: u32, masks: &PixelMasks ) -> Result<Surface<'a>, String>

Creates a new surface from an existing buffer, using pixel masks.

source

pub fn as_texture<'b, T>( &self, texture_creator: &'b TextureCreator<T> ) -> Result<Texture<'b>, TextureValueError>

A convenience function for TextureCreator::create_texture_from_surface.

use sdl2::pixels::PixelFormatEnum;
use sdl2::surface::Surface;
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;

// We init systems.
let sdl_context = sdl2::init().expect("failed to init SDL");
let video_subsystem = sdl_context.video().expect("failed to get video context");

// We create a window.
let window = video_subsystem.window("sdl2 demo", 800, 600)
    .build()
    .expect("failed to build window");

// We get the canvas from which we can get the `TextureCreator`.
let mut canvas: Canvas<Window> = window.into_canvas()
    .build()
    .expect("failed to build window's canvas");
let texture_creator = canvas.texture_creator();

let surface = Surface::new(512, 512, PixelFormatEnum::RGB24).unwrap();
let texture = surface.as_texture(&texture_creator).unwrap();
source

pub fn load_bmp_rw(rwops: &mut RWops<'_>) -> Result<Surface<'static>, String>

source

pub fn load_bmp<P: AsRef<Path>>(path: P) -> Result<Surface<'static>, String>

source

pub fn into_canvas(self) -> Result<Canvas<Surface<'a>>, String>

Creates a Software Canvas to allow rendering in the Surface itself. This Canvas will never be accelerated materially, so there is no performance change between Surface and Canvas coming from a Surface.

The only change is this case is that Canvas has a better API to draw stuff in the Surface in that case, but don’t expect any performance changes, there will be none.

source

pub fn context(&self) -> Rc<SurfaceContext<'a>>

Methods from Deref<Target = SurfaceRef>§

source

pub fn raw(&self) -> *mut SDL_Surface

source

pub fn width(&self) -> u32

source

pub fn height(&self) -> u32

source

pub fn pitch(&self) -> u32

source

pub fn size(&self) -> (u32, u32)

source

pub fn rect(&self) -> Rect

Gets the rect of the surface.

source

pub fn pixel_format(&self) -> PixelFormat

source

pub fn pixel_format_enum(&self) -> PixelFormatEnum

source

pub fn with_lock<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R

Locks a surface so that the pixels can be directly accessed safely.

source

pub fn with_lock_mut<R, F: FnOnce(&mut [u8]) -> R>(&mut self, f: F) -> R

Locks a surface so that the pixels can be directly accessed safely.

source

pub fn without_lock(&self) -> Option<&[u8]>

Returns the Surface’s pixel buffer if the Surface doesn’t require locking (e.g. it’s a software surface).

source

pub fn without_lock_mut(&mut self) -> Option<&mut [u8]>

Returns the Surface’s pixel buffer if the Surface doesn’t require locking (e.g. it’s a software surface).

source

pub fn must_lock(&self) -> bool

Returns true if the Surface needs to be locked before accessing the Surface pixels.

source

pub fn save_bmp_rw(&self, rwops: &mut RWops<'_>) -> Result<(), String>

source

pub fn save_bmp<P: AsRef<Path>>(&self, path: P) -> Result<(), String>

source

pub fn set_palette(&mut self, palette: &Palette) -> Result<(), String>

source

pub fn enable_RLE(&mut self)

source

pub fn disable_RLE(&mut self)

source

pub fn set_color_key( &mut self, enable: bool, color: Color ) -> Result<(), String>

source

pub fn color_key(&self) -> Result<Color, String>

The function will fail if the surface doesn’t have color key enabled.

source

pub fn set_color_mod(&mut self, color: Color)

source

pub fn color_mod(&self) -> Color

source

pub fn fill_rect<R>(&mut self, rect: R, color: Color) -> Result<(), String>
where R: Into<Option<Rect>>,

source

pub fn fill_rects(&mut self, rects: &[Rect], color: Color) -> Result<(), String>

source

pub fn set_alpha_mod(&mut self, alpha: u8)

source

pub fn alpha_mod(&self) -> u8

source

pub fn set_blend_mode(&mut self, mode: BlendMode) -> Result<(), String>

The function will fail if the blend mode is not supported by SDL.

source

pub fn blend_mode(&self) -> BlendMode

source

pub fn set_clip_rect<R>(&mut self, rect: R) -> bool
where R: Into<Option<Rect>>,

Sets the clip rectangle for the surface.

If the rectangle is None, clipping will be disabled.

source

pub fn clip_rect(&self) -> Option<Rect>

Gets the clip rectangle for the surface.

Returns None if clipping is disabled.

source

pub fn convert(&self, format: &PixelFormat) -> Result<Surface<'static>, String>

Copies the surface into a new one that is optimized for blitting to a surface of a specified pixel format.

source

pub fn convert_format( &self, format: PixelFormatEnum ) -> Result<Surface<'static>, String>

Copies the surface into a new one of a specified pixel format.

source

pub fn blit<R1, R2>( &self, src_rect: R1, dst: &mut SurfaceRef, dst_rect: R2 ) -> Result<Option<Rect>, String>
where R1: Into<Option<Rect>>, R2: Into<Option<Rect>>,

Performs surface blitting (surface copying).

Returns the final blit rectangle, if a dst_rect was provided.

source

pub unsafe fn lower_blit<R1, R2>( &self, src_rect: R1, dst: &mut SurfaceRef, dst_rect: R2 ) -> Result<(), String>
where R1: Into<Option<Rect>>, R2: Into<Option<Rect>>,

Performs low-level surface blitting.

Unless you know what you’re doing, use blit() instead, which will clip the input rectangles. This function could crash if the rectangles aren’t pre-clipped to the surface, and is therefore unsafe.

source

pub unsafe fn soft_stretch_linear<R1, R2>( &self, src_rect: R1, dst: &mut SurfaceRef, dst_rect: R2 ) -> Result<Option<Rect>, String>
where R1: Into<Option<Rect>>, R2: Into<Option<Rect>>,

Performs bilinear scaling between two surfaces of the same format, 32BPP.

Returns the final blit rectangle, if a dst_rect was provided.

source

pub fn blit_scaled<R1, R2>( &self, src_rect: R1, dst: &mut SurfaceRef, dst_rect: R2 ) -> Result<Option<Rect>, String>
where R1: Into<Option<Rect>>, R2: Into<Option<Rect>>,

Performs scaled surface bliting (surface copying).

Returns the final blit rectangle, if a dst_rect was provided.

source

pub unsafe fn lower_blit_scaled<R1, R2>( &self, src_rect: R1, dst: &mut SurfaceRef, dst_rect: R2 ) -> Result<(), String>
where R1: Into<Option<Rect>>, R2: Into<Option<Rect>>,

Performs low-level scaled surface blitting.

Unless you know what you’re doing, use blit_scaled() instead, which will clip the input rectangles. This function could crash if the rectangles aren’t pre-clipped to the surface, and is therefore unsafe.

Trait Implementations§

source§

impl<'a> AsMut<SurfaceRef> for Surface<'a>

source§

fn as_mut(&mut self) -> &mut SurfaceRef

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<'a> AsRef<SurfaceRef> for Surface<'a>

source§

fn as_ref(&self) -> &SurfaceRef

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<'a> DerefMut for Surface<'a>

source§

fn deref_mut(&mut self) -> &mut SurfaceRef

Mutably dereferences the value.
source§

impl<'a> LoadSurface for Surface<'a>

source§

fn from_file<P: AsRef<Path>>(filename: P) -> Result<Surface<'a>, String>

Loads an SDL Surface from a file

source§

fn from_xpm_array(xpm: *const *const i8) -> Result<Surface<'a>, String>

Loads an SDL Surface from XPM data

source§

impl<'s> RenderTarget for Surface<'s>

source§

impl<'a> RotozoomSurface for Surface<'a>

source§

fn rotozoom( &self, angle: f64, zoom: f64, smooth: bool ) -> Result<Surface<'_>, String>

Rotates and zooms a surface and optional anti-aliasing.
source§

fn rotozoom_xy( &self, angle: f64, zoomx: f64, zoomy: f64, smooth: bool ) -> Result<Surface<'_>, String>

Rotates and zooms a surface with different horizontal and vertical scaling factors and optional anti-aliasing.
source§

fn zoom( &self, zoomx: f64, zoomy: f64, smooth: bool ) -> Result<Surface<'_>, String>

Zoom a surface by independent horizontal and vertical factors with optional smoothing.
source§

fn shrink(&self, factorx: i32, factory: i32) -> Result<Surface<'_>, String>

Shrink a surface by an integer ratio using averaging.
source§

fn rotate_90deg(&self, turns: i32) -> Result<Surface<'_>, String>

Rotates a 8/16/24/32 bit surface in increments of 90 degrees.
source§

impl<'a> SaveSurface for Surface<'a>

source§

fn save<P: AsRef<Path>>(&self, filename: P) -> Result<(), String>

Saves an SDL Surface to a file

source§

fn save_rw(&self, dst: &mut RWops<'_>) -> Result<(), String>

Saves an SDL Surface to an RWops

source§

impl<'a> Deref for Surface<'a>

§

type Target = SurfaceRef

The resulting type after dereferencing.
source§

fn deref(&self) -> &SurfaceRef

Dereferences the value.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Surface<'a>

§

impl<'a> !Send for Surface<'a>

§

impl<'a> !Sync for Surface<'a>

§

impl<'a> Unpin for Surface<'a>

§

impl<'a> UnwindSafe for Surface<'a>

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

source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.