Skip to content

Commit 36a1f61

Browse files
authored
Merge pull request #1014 from netcan/master
add load_texture_bytes interface for load texture from buffer
2 parents dcd311d + d9659e0 commit 36a1f61

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/sdl2/image/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl<'a> SaveSurface for Surface<'a> {
135135
/// Method extensions for creating Textures from a `TextureCreator`
136136
pub trait LoadTexture {
137137
fn load_texture<P: AsRef<Path>>(&self, filename: P) -> Result<Texture, String>;
138+
fn load_texture_bytes(&self, buf: &[u8]) -> Result<Texture, String>;
138139
}
139140

140141
impl<T> LoadTexture for TextureCreator<T> {
@@ -150,6 +151,19 @@ impl<T> LoadTexture for TextureCreator<T> {
150151
}
151152
}
152153
}
154+
155+
fn load_texture_bytes(&self, buf: &[u8]) -> Result<Texture, String> {
156+
//! 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)
157+
unsafe {
158+
let buf = sdl2_sys::SDL_RWFromMem(buf.as_ptr() as *mut libc::c_void, buf.len() as i32);
159+
let raw = image::IMG_LoadTexture_RW(self.raw(), buf, 1); // close(free) buff after load
160+
if (raw as *mut ()).is_null() {
161+
Err(get_error())
162+
} else {
163+
Ok(self.raw_create_texture(raw))
164+
}
165+
}
166+
}
153167
}
154168

155169
/// Context manager for `sdl2_image` to manage quitting. Can't do much with it but

0 commit comments

Comments
 (0)