cleaner and safer code

This commit is contained in:
Bódi Balázs
2014-10-10 06:44:34 +02:00
committed by bbodi
parent ed79bbf416
commit e5d91cdbda
+12 -2
View File
@@ -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) }
}