mirror of
https://github.com/encounter/rust-sdl2.git
synced 2026-07-10 21:18:41 -07:00
added conversion from PixelFormatEnum to PixelFormat
This commit is contained in:
@@ -5,6 +5,7 @@ use self::rand::distributions::{Distribution, Standard};
|
||||
use libc::uint32_t;
|
||||
use num::FromPrimitive;
|
||||
use std::mem::transmute;
|
||||
use std::convert::TryFrom;
|
||||
use crate::sys;
|
||||
|
||||
use crate::get_error;
|
||||
@@ -460,6 +461,21 @@ impl FromPrimitive for PixelFormatEnum {
|
||||
fn from_u64(n: u64) -> Option<PixelFormatEnum> { FromPrimitive::from_i64(n as i64) }
|
||||
}
|
||||
|
||||
impl TryFrom<PixelFormatEnum> for PixelFormat {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(pfe: PixelFormatEnum) -> Result<Self, Self::Error> {
|
||||
unsafe {
|
||||
let pf_ptr = sys::SDL_AllocFormat(pfe as u32);
|
||||
if pf_ptr.is_null() {
|
||||
Err(get_error())
|
||||
} else {
|
||||
Ok(PixelFormat::from_ll(pf_ptr))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Just test a round-trip conversion from PixelFormat to
|
||||
// PixelFormatEnum and back.
|
||||
|
||||
Reference in New Issue
Block a user