Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience functions #988

Merged
merged 1 commit into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion src/sdl2/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,32 @@ impl<T> TextureCreator<T> {
///
/// # Remarks
///
/// The access hint for the created texture is `TextureAccess::Static`.
/// The access hint for the created texture is [`TextureAccess::Static`].
///
/// ```no_run
/// 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 = texture_creator.create_texture_from_surface(surface).unwrap();
/// ```
pub fn create_texture_from_surface<S: AsRef<SurfaceRef>>
(&self,
surface: S)
Expand Down Expand Up @@ -2126,6 +2151,68 @@ impl<'r> Texture<'r> {
pub const fn raw(&self) -> *mut sys::SDL_Texture {
self.raw
}

/// A convenience function for [`TextureCreator::create_texture_from_surface`].
///
/// ```no_run
/// 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 = Texture::from_surface(&surface, &texture_creator).unwrap();
/// ```
#[cfg(not(feature = "unsafe_textures"))]
pub fn from_surface<'a, T>(surface: &Surface, texture_creator: &'a TextureCreator<T>) -> Result<Texture<'a>, TextureValueError> {
texture_creator.create_texture_from_surface(surface)
}

/// A convenience function for [`TextureCreator::create_texture_from_surface`].
///
/// ```no_run
/// 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 = Texture::from_surface(&surface, &texture_creator).unwrap();
/// ```
#[cfg(feature = "unsafe_textures")]
pub fn from_surface<T>(surface: &Surface, texture_creator: &TextureCreator<T>) -> Result<Texture, TextureValueError> {
texture_creator.create_texture_from_surface(surface)
}
}

#[cfg(feature = "unsafe_textures")]
Expand Down
63 changes: 63 additions & 0 deletions src/sdl2/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::pixels;
use crate::render::{BlendMode, Canvas};
use crate::rwops::RWops;
use std::mem::transmute;
use crate::render::{Texture, TextureCreator, TextureValueError};

use crate::sys;

Expand Down Expand Up @@ -173,6 +174,68 @@ impl<'a> Surface<'a> {
}
}

/// A convenience function for [`TextureCreator::create_texture_from_surface`].
///
/// ```no_run
/// 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();
/// ```
#[cfg(not(feature = "unsafe_textures"))]
pub fn as_texture<'b, T>(&self, texture_creator: &'b TextureCreator<T>) -> Result<Texture<'b>, TextureValueError> {
texture_creator.create_texture_from_surface(self)
}

/// A convenience function for [`TextureCreator::create_texture_from_surface`].
///
/// ```no_run
/// 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();
/// ```
#[cfg(feature = "unsafe_textures")]
pub fn as_texture<T>(&self, texture_creator: &TextureCreator<T>) -> Result<Texture, TextureValueError> {
texture_creator.create_texture_from_surface(self)
}

pub fn load_bmp_rw(rwops: &mut RWops) -> Result<Surface<'static>, String> {
let raw = unsafe {
sys::SDL_LoadBMP_RW(rwops.raw(), 0)
Expand Down