diff --git a/examples/static_basic.rs b/examples/static_basic.rs index ff8c6953..d8ad4228 100644 --- a/examples/static_basic.rs +++ b/examples/static_basic.rs @@ -46,8 +46,10 @@ fn main() { glfw.poll_events(); // Clear the screen to black - gl::ClearColor(0.3, 0.3, 0.3, 1.0); - gl::Clear(gl::COLOR_BUFFER_BIT); + unsafe { + gl::ClearColor(0.3, 0.3, 0.3, 1.0); + gl::Clear(gl::COLOR_BUFFER_BIT); + } // Swap buffers window.swap_buffers(); diff --git a/examples/static_triangle.rs b/examples/static_triangle.rs index cdce09c9..5092b9a6 100644 --- a/examples/static_triangle.rs +++ b/examples/static_triangle.rs @@ -53,8 +53,9 @@ fn start(argc: int, argv: *const *const u8) -> int { } fn compile_shader(src: &str, ty: GLenum) -> GLuint { - let shader = gl::CreateShader(ty); + let shader; unsafe { + shader = gl::CreateShader(ty); // Attempt to compile the shader src.with_c_str(|ptr| gl::ShaderSource(shader, 1, &ptr, ptr::null())); gl::CompileShader(shader); @@ -75,27 +76,25 @@ fn compile_shader(src: &str, ty: GLenum) -> GLuint { shader } -fn link_program(vs: GLuint, fs: GLuint) -> GLuint { +fn link_program(vs: GLuint, fs: GLuint) -> GLuint { unsafe { let program = gl::CreateProgram(); gl::AttachShader(program, vs); gl::AttachShader(program, fs); gl::LinkProgram(program); - unsafe { - // Get the link status - let mut status = gl::FALSE as GLint; - gl::GetProgramiv(program, gl::LINK_STATUS, &mut status); - - // Fail on error - if status != (gl::TRUE as GLint) { - let mut len: GLint = 0; - gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut len); - let mut buf = Vec::from_elem(len as uint - 1, 0u8); // subtract 1 to skip the trailing null character - gl::GetProgramInfoLog(program, len, ptr::null_mut(), buf.as_mut_ptr() as *mut GLchar); - panic!("{}", str::from_utf8(buf.as_slice()).expect("ProgramInfoLog not valid utf8")); - } + // Get the link status + let mut status = gl::FALSE as GLint; + gl::GetProgramiv(program, gl::LINK_STATUS, &mut status); + + // Fail on error + if status != (gl::TRUE as GLint) { + let mut len: GLint = 0; + gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut len); + let mut buf = Vec::from_elem(len as uint - 1, 0u8); // subtract 1 to skip the trailing null character + gl::GetProgramInfoLog(program, len, ptr::null_mut(), buf.as_mut_ptr() as *mut GLchar); + panic!("{}", str::from_utf8(buf.as_slice()).expect("ProgramInfoLog not valid utf8")); } program -} +} } fn main() { let glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); @@ -150,22 +149,24 @@ fn main() { // Poll events glfw.poll_events(); - // Clear the screen to black - gl::ClearColor(0.3, 0.3, 0.3, 1.0); - gl::Clear(gl::COLOR_BUFFER_BIT); + unsafe { + // Clear the screen to black + gl::ClearColor(0.3, 0.3, 0.3, 1.0); + gl::Clear(gl::COLOR_BUFFER_BIT); - // Draw a triangle from the 3 vertices - gl::DrawArrays(gl::TRIANGLES, 0, 3); + // Draw a triangle from the 3 vertices + gl::DrawArrays(gl::TRIANGLES, 0, 3); + } // Swap buffers window.swap_buffers(); } - // Cleanup - gl::DeleteProgram(program); - gl::DeleteShader(fs); - gl::DeleteShader(vs); unsafe { + // Cleanup + gl::DeleteProgram(program); + gl::DeleteShader(fs); + gl::DeleteShader(vs); gl::DeleteBuffers(1, &vbo); gl::DeleteVertexArrays(1, &vao); } diff --git a/examples/struct_triangle.rs b/examples/struct_triangle.rs index 054ebf88..64ad2f46 100644 --- a/examples/struct_triangle.rs +++ b/examples/struct_triangle.rs @@ -70,8 +70,9 @@ fn start(argc: int, argv: *const *const u8) -> int { } fn compile_shader(gl: &Gl, src: &str, ty: GLenum) -> GLuint { - let shader = gl.CreateShader(ty); + let shader; unsafe { + shader = gl.CreateShader(ty); // Attempt to compile the shader src.with_c_str(|ptr| gl.ShaderSource(shader, 1, &ptr, ptr::null())); gl.CompileShader(shader); @@ -92,7 +93,7 @@ fn compile_shader(gl: &Gl, src: &str, ty: GLenum) -> GLuint { shader } -fn link_program(gl: &Gl, vs: GLuint, fs: GLuint) -> GLuint { +fn link_program(gl: &Gl, vs: GLuint, fs: GLuint) -> GLuint { unsafe { let program = gl.CreateProgram(); gl.AttachShader(program, vs); gl.AttachShader(program, fs); @@ -112,7 +113,7 @@ fn link_program(gl: &Gl, vs: GLuint, fs: GLuint) -> GLuint { } } program -} +}} fn main() { let glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); @@ -167,22 +168,24 @@ fn main() { // Poll events glfw.poll_events(); - // Clear the screen to black - gl.ClearColor(0.3, 0.3, 0.3, 1.0); - gl.Clear(gl::COLOR_BUFFER_BIT); + unsafe { + // Clear the screen to black + gl.ClearColor(0.3, 0.3, 0.3, 1.0); + gl.Clear(gl::COLOR_BUFFER_BIT); - // Draw a triangle from the 3 vertices - gl.DrawArrays(gl::TRIANGLES, 0, 3); + // Draw a triangle from the 3 vertices + gl.DrawArrays(gl::TRIANGLES, 0, 3); + } // Swap buffers window.swap_buffers(); } - // Cleanup - gl.DeleteProgram(program); - gl.DeleteShader(fs); - gl.DeleteShader(vs); unsafe { + // Cleanup + gl.DeleteProgram(program); + gl.DeleteShader(fs); + gl.DeleteShader(vs); gl.DeleteBuffers(1, &vbo); gl.DeleteVertexArrays(1, &vao); } diff --git a/src/gl/lib.rs b/src/gl/lib.rs index 621c2241..eb26fa8e 100644 --- a/src/gl/lib.rs +++ b/src/gl/lib.rs @@ -54,7 +54,7 @@ //! gl::TEXTURE_2D; //! //! // calling a function -//! gl::DrawArrays(gl::TRIANGLES, 0, 3); +//! unsafe { gl::DrawArrays(gl::TRIANGLES, 0, 3) }; //! //! // functions that take pointers are unsafe //! # let shader = 0; diff --git a/src/gl_generator/generators/global_gen.rs b/src/gl_generator/generators/global_gen.rs index 4bf1b033..2a451d2f 100644 --- a/src/gl_generator/generators/global_gen.rs +++ b/src/gl_generator/generators/global_gen.rs @@ -107,23 +107,7 @@ fn write_fns(ecx: &ExtCtxt, registry: &Registry) -> Vec
> {
None => "".to_string()
};
- ecx.parse_item(if c.is_safe {
- format!(
- "#[allow(non_snake_case)] #[allow(unused_variables)] #[allow(dead_code)]
- #[inline] #[unstable] {doc} pub fn {name}({params}) -> {return_suffix} {{ \
- unsafe {{ \
- __gl_imports::mem::transmute::<_, extern \"system\" fn({types}) -> {return_suffix}>\
- (storage::{name}.f)({idents}) \
- }} \
- }}",
- name = c.proto.ident,
- doc = doc,
- params = super::gen_parameters(ecx, c).into_iter().map(|p| p.to_source()).collect:: > {
return_suffix = super::gen_return_type(ecx, c).to_source(),
idents = super::gen_parameters(ecx, c).into_iter().map(|p| p.pat.to_source()).collect::