add load_texture_rw interface for load texture from buffer
@@ -135,6 +135,7 @@ impl<'a> SaveSurface for Surface<'a> {
/// Method extensions for creating Textures from a `TextureCreator`
pub trait LoadTexture {
fn load_texture<P: AsRef<Path>>(&self, filename: P) -> Result<Texture, String>;
+ fn load_texture_rw(&self, buf: &[u8]) -> Result<Texture, String>;
}
impl<T> LoadTexture for TextureCreator<T> {
@@ -150,6 +151,18 @@ impl<T> LoadTexture for TextureCreator<T> {
}
}
}
+
+ fn load_texture_rw(&self, buf: &[u8]) -> Result<Texture, String> {
+ unsafe {
+ let buf = sdl2_sys::SDL_RWFromMem(buf.as_ptr() as *mut libc::c_void, buf.len() as i32);
+ let raw = image::IMG_LoadTexture_RW(self.raw(), buf, 1); // close(free) buff after load
+ if (raw as *mut ()).is_null() {
+ Err(get_error())
+ } else {
+ Ok(self.raw_create_texture(raw))
+ }
+ }
+ }
}
/// Context manager for `sdl2_image` to manage quitting. Can't do much with it but
