mirror of
https://github.com/encounter/rust-sdl2.git
synced 2026-07-10 21:18:41 -07:00
cleaner and safer code
This commit is contained in:
+12
-2
@@ -702,14 +702,13 @@ impl Texture {
|
||||
else { Err(get_error()) }
|
||||
}
|
||||
|
||||
pub fn lock(&self, rect: Option<Rect>) -> SdlResult<(CVec<u8>, i32)> {
|
||||
fn unsafe_lock(&self, rect: Option<Rect>) -> SdlResult<(CVec<u8>, i32)> {
|
||||
let q = try!(self.query());
|
||||
unsafe {
|
||||
let actual_rect = match rect {
|
||||
Some(ref rect) => rect as *const _,
|
||||
None => ptr::null()
|
||||
};
|
||||
|
||||
let pixels : *const c_void = ptr::null();
|
||||
let pitch = 0i32;
|
||||
let ret = ll::SDL_LockTexture(self.raw, actual_rect, &pixels, &pitch);
|
||||
@@ -722,6 +721,17 @@ impl Texture {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lock(&self, rect: Option<Rect>, func: |CVec<u8>, i32| -> ()) -> SdlResult<()> {
|
||||
match self.unsafe_lock(rect) {
|
||||
Ok((cvec, pitch)) => {
|
||||
func(cvec, pitch);
|
||||
self.unlock();
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unlock(&self) {
|
||||
unsafe { ll::SDL_UnlockTexture(self.raw) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user