mirror of
https://github.com/encounter/rust-sdl2.git
synced 2026-07-10 21:18:41 -07:00
Merge pull request #362 from mvdnes/fix
Fix range, remove as_slice and some transmutes
This commit is contained in:
@@ -25,7 +25,7 @@ pub const AUDIO_S32SYS : SDL_AudioFormat = AUDIO_S32LSB;
|
||||
pub const AUDIO_F32SYS : SDL_AudioFormat = AUDIO_F32LSB;
|
||||
|
||||
pub type SDL_AudioCallback =
|
||||
Option<extern "C" fn (arg1: *const c_void, arg2: *const uint8_t, arg3: c_int)>;
|
||||
Option<extern "C" fn (arg1: *mut c_void, arg2: *mut uint8_t, arg3: c_int)>;
|
||||
#[allow(missing_copy_implementations)]
|
||||
#[repr(C)]
|
||||
pub struct SDL_AudioSpec {
|
||||
|
||||
@@ -78,7 +78,7 @@ extern "C" {
|
||||
pub fn SDL_GetTextureBlendMode(texture: *const SDL_Texture, blendMode: *const SDL_BlendMode) -> c_int;
|
||||
pub fn SDL_UpdateTexture(texture: *const SDL_Texture, rect: *const SDL_Rect, pixels: *const c_void, pitch: c_int) -> c_int;
|
||||
pub fn SDL_UpdateYUVTexture(texture: *const SDL_Texture, rect: *const SDL_Rect, Yplane: *const uint8_t, Ypitch: c_int, Uplane: *const uint8_t, Upitch: c_int, Vplane: *const uint8_t, Vpitch: c_int) -> c_int;
|
||||
pub fn SDL_LockTexture(texture: *const SDL_Texture, rect: *const SDL_Rect, pixels: *const *const c_void, pitch: *const c_int) -> c_int;
|
||||
pub fn SDL_LockTexture(texture: *const SDL_Texture, rect: *const SDL_Rect, pixels: *mut *mut c_void, pitch: *mut c_int) -> c_int;
|
||||
pub fn SDL_UnlockTexture(texture: *const SDL_Texture);
|
||||
pub fn SDL_RenderTargetSupported(renderer: *const SDL_Renderer) -> SDL_bool;
|
||||
pub fn SDL_SetRenderTarget(renderer: *const SDL_Renderer, texture: *const SDL_Texture) -> c_int;
|
||||
|
||||
+14
-16
@@ -125,13 +125,11 @@ impl AudioSpecWAV {
|
||||
}
|
||||
|
||||
pub fn get_buffer(&self) -> &[u8] {
|
||||
use std::raw::Slice;
|
||||
use std::mem::transmute;
|
||||
use std::slice::from_raw_parts;
|
||||
unsafe {
|
||||
transmute(Slice {
|
||||
data: self.audio_buf,
|
||||
len: self.audio_len as usize
|
||||
})
|
||||
let ptr = self.audio_buf as *const u8;
|
||||
let len = self.audio_len as usize;
|
||||
from_raw_parts(ptr, len)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,15 +192,15 @@ impl AudioFormatNum<f32> for f32 {
|
||||
}
|
||||
|
||||
extern "C" fn audio_callback_marshall<CB: AudioCallback>
|
||||
(userdata: *const c_void, stream: *const uint8_t, len: c_int) {
|
||||
use std::raw::Slice;
|
||||
(userdata: *mut c_void, stream: *mut uint8_t, len: c_int) {
|
||||
use std::slice::from_raw_parts_mut;
|
||||
use std::mem::{size_of, transmute};
|
||||
unsafe {
|
||||
let mut cb_userdata: &mut AudioCallbackUserdata<CB> = transmute(userdata);
|
||||
let buf: &mut [CB::Channel] = transmute(Slice {
|
||||
data: stream,
|
||||
len: len as usize / size_of::<CB::Channel>()
|
||||
});
|
||||
let buf: &mut [CB::Channel] = from_raw_parts_mut(
|
||||
stream as *mut CB::Channel,
|
||||
len as usize / size_of::<CB::Channel>()
|
||||
);
|
||||
|
||||
cb_userdata.callback.callback(buf);
|
||||
}
|
||||
@@ -230,8 +228,8 @@ impl<CB: AudioCallback> AudioSpecDesired<CB> {
|
||||
size: 0,
|
||||
callback: Some(audio_callback_marshall::<CB>
|
||||
as extern "C" fn
|
||||
(arg1: *const c_void,
|
||||
arg2: *const uint8_t,
|
||||
(arg1: *mut c_void,
|
||||
arg2: *mut uint8_t,
|
||||
arg3: c_int)),
|
||||
userdata: transmute(userdata)
|
||||
}
|
||||
@@ -492,10 +490,10 @@ mod test {
|
||||
use std::iter::repeat;
|
||||
|
||||
// 0,1,2,3, ...
|
||||
let buffer: Vec<u8> = range(0, 255).collect();
|
||||
let buffer: Vec<u8> = (0..255).collect();
|
||||
|
||||
// 0,0,1,1,2,2,3,3, ...
|
||||
let new_buffer_expected: Vec<u8> = range(0, 255).flat_map(|v| repeat(v).take(2)).collect();
|
||||
let new_buffer_expected: Vec<u8> = (0..255).flat_map(|v| repeat(v).take(2)).collect();
|
||||
|
||||
let cvt = AudioCVT::new(AUDIOU8, 1, 44100, AUDIOU8, 2, 44100).unwrap();
|
||||
assert!(cvt.is_conversion_needed());
|
||||
|
||||
+4
-6
@@ -416,7 +416,7 @@ impl ::std::fmt::Debug for Event {
|
||||
// TODO: Remove this when from_utf8 is updated in Rust
|
||||
impl Event {
|
||||
fn to_ll(self) -> Option<ll::SDL_Event> {
|
||||
let ret = unsafe { mem::uninitialized() };
|
||||
let mut ret = unsafe { mem::uninitialized() };
|
||||
match self {
|
||||
// just ignore timestamp
|
||||
Event::User { window_id, _type, code, .. } => {
|
||||
@@ -429,7 +429,7 @@ impl Event {
|
||||
data2: ptr::null(),
|
||||
};
|
||||
unsafe {
|
||||
ptr::copy(mem::transmute::<_,*mut ll::SDL_UserEvent>(&ret), &event, 1);
|
||||
ptr::copy(&mut ret as *mut ll::SDL_Event as *mut ll::SDL_UserEvent, &event, 1);
|
||||
}
|
||||
Some(ret)
|
||||
},
|
||||
@@ -525,11 +525,10 @@ impl Event {
|
||||
let ref event = *raw.edit();
|
||||
|
||||
let text = String::from_utf8_lossy(
|
||||
event.text.iter()
|
||||
&event.text.iter()
|
||||
.take_while(|&b| (*b) != 0i8)
|
||||
.map(|&b| b as u8)
|
||||
.collect::<Vec<u8>>()
|
||||
.as_slice()
|
||||
).to_owned().into_owned();
|
||||
Event::TextEditing {
|
||||
timestamp: event.timestamp,
|
||||
@@ -543,11 +542,10 @@ impl Event {
|
||||
let ref event = *raw.text();
|
||||
|
||||
let text = String::from_utf8_lossy(
|
||||
event.text.iter()
|
||||
&event.text.iter()
|
||||
.take_while(|&b| (*b) != 0i8)
|
||||
.map(|&b| b as u8)
|
||||
.collect::<Vec<u8>>()
|
||||
.as_slice()
|
||||
).to_owned().into_owned();
|
||||
Event::TextInput {
|
||||
timestamp: event.timestamp,
|
||||
|
||||
+6
-8
@@ -48,7 +48,6 @@ use get_error;
|
||||
use SdlResult;
|
||||
use std::mem;
|
||||
use std::ptr;
|
||||
use std::raw;
|
||||
use libc::{c_int, uint32_t, c_double, c_void};
|
||||
use rect::Point;
|
||||
use rect::Rect;
|
||||
@@ -720,8 +719,7 @@ impl<'renderer> RenderDrawer<'renderer> {
|
||||
|
||||
// Pass the interior of `pixels: Vec<u8>` to SDL
|
||||
let ret = {
|
||||
let pixels_ref: raw::Slice<u8> = mem::transmute(pixels.as_slice());
|
||||
ll::SDL_RenderReadPixels(self.raw, actual_rect, format as uint32_t, pixels_ref.data as *mut c_void, pitch as c_int)
|
||||
ll::SDL_RenderReadPixels(self.raw, actual_rect, format as uint32_t, pixels.as_mut_ptr() as *mut c_void, pitch as c_int)
|
||||
};
|
||||
|
||||
if ret == 0 {
|
||||
@@ -1061,18 +1059,18 @@ impl<'renderer> Texture<'renderer> {
|
||||
// Call to SDL to populate pixel data
|
||||
let loaded = unsafe {
|
||||
let q = self.query();
|
||||
let pixels : *const c_void = ptr::null();
|
||||
let pitch = 0;
|
||||
let mut pixels = ptr::null_mut();
|
||||
let mut pitch = 0;
|
||||
|
||||
let (rect_raw_ptr, height) = match rect {
|
||||
Some(ref rect) => (rect as *const _, rect.h as usize),
|
||||
None => (ptr::null(), q.height as usize)
|
||||
};
|
||||
|
||||
let ret = ll::SDL_LockTexture(self.raw, rect_raw_ptr, &pixels, &pitch);
|
||||
let ret = ll::SDL_LockTexture(self.raw, rect_raw_ptr, &mut pixels, &mut pitch);
|
||||
if ret == 0 {
|
||||
let size = q.format.byte_size_from_pitch_and_height(pitch as usize, height);
|
||||
Ok( (raw::Slice { data: pixels as *const u8, len: size }, pitch) )
|
||||
Ok( (::std::slice::from_raw_parts_mut(pixels as *mut u8, size ), pitch) )
|
||||
} else {
|
||||
Err(get_error())
|
||||
}
|
||||
@@ -1082,7 +1080,7 @@ impl<'renderer> Texture<'renderer> {
|
||||
Ok((interior, pitch)) => {
|
||||
let result;
|
||||
unsafe {
|
||||
result = func(mem::transmute(interior), pitch as usize);
|
||||
result = func(interior, pitch as usize);
|
||||
ll::SDL_UnlockTexture(self.raw);
|
||||
}
|
||||
Ok(result)
|
||||
|
||||
Reference in New Issue
Block a user