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 load_texture_bytes interface for load texture from buffer #1014

Merged
merged 2 commits into from
Jul 14, 2020
Merged
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
14 changes: 14 additions & 0 deletions src/sdl2/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_bytes(&self, buf: &[u8]) -> Result<Texture, String>;
}

impl<T> LoadTexture for TextureCreator<T> {
Expand All @@ -150,6 +151,19 @@ impl<T> LoadTexture for TextureCreator<T> {
}
}
}

fn load_texture_bytes(&self, buf: &[u8]) -> Result<Texture, String> {
//! Loads an SDL Texture from a buffer that the format must be something supported by SDL2_image (png, jpeg, ect, but NOT RGBA8888 bytes for instance)
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
Expand Down