Repository: brendanzab/gl-rs (language: rust)

Issue:
Update the gl/examples to build on latest rust and glfw
I have not, however, been able to update the struct_triangle.rs example



Discussion / hints:
Looks good.

I'm having problems building examples from the "gl" folder.

```
$ cargo build --example static_triangle
no example target named `static_triangle`
```

You need to remove [this line](https://github.com/bjz/gl-rs/blob/b856b6141853adf23bd8fbae13a9de161ab1162e/gl/Cargo.toml#L1).

Both "static_basic" and "static_triangle" need to add `as *const ()` to work.

The reason that "struct_triangle" does not work is, if I remember correctly, that `generate_gl_bindings!` was replaced by a build script.


Fix patch:
diff --git a/gl/Cargo.toml b/gl/Cargo.toml
index 1a6bd025..cb4ed0c5 100644
--- a/gl/Cargo.toml
+++ b/gl/Cargo.toml
@@ -1,5 +1,3 @@
-example = []
-
 [package]
 name = "gl"
 version = "0.4.0"
@@ -20,5 +18,5 @@ version = "0.3.0"
 path = "../khronos_api"
 version = "0.0.8"
 
-#[dev-dependencies.glfw]
-#git = "https://github.com/bjz/glfw-rs"
+[dev-dependencies]
+glutin = "0.4"
diff --git a/gl/examples/static_basic.rs b/gl/examples/basic.rs
similarity index 55%
rename from gl/examples/static_basic.rs
rename to gl/examples/basic.rs
index 014a237e..ee12731a 100644
--- a/gl/examples/static_basic.rs
+++ b/gl/examples/basic.rs
@@ -14,38 +14,28 @@
 // limitations under the License.
 
 extern crate gl;
-extern crate glfw;
-
-use glfw::{Context, OpenGlProfileHint, WindowHint, WindowMode};
+extern crate glutin;
 
 fn main() {
-    let glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
-
-    // Choose a GL profile that is compatible with OS X 10.7+
-    glfw.window_hint(WindowHint::ContextVersion(3, 2));
-    glfw.window_hint(WindowHint::OpenglForwardCompat(true));
-    glfw.window_hint(WindowHint::OpenglProfile(OpenGlProfileHint::Core));
-
-    let (window, _) = glfw.create_window(800, 600, "OpenGL", WindowMode::Windowed)
-        .expect("Failed to create GLFW window.");
+    let window = glutin::Window::new().unwrap();
 
     // It is essential to make the context current before calling `gl::load_with`.
-    window.make_current();
+    unsafe { window.make_current() }.unwrap();
 
     // Load the OpenGL function pointers
-    gl::load_with(|s| window.get_proc_address(s));
+    // TODO: `as *const _` will not be needed once glutin is updated to the latest gl version
+    gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
 
-    while !window.should_close() {
-        // Poll events
-        glfw.poll_events();
-
-        // Clear the screen to black
+    for event in window.wait_events() {
         unsafe {
             gl::ClearColor(0.3, 0.3, 0.3, 1.0);
             gl::Clear(gl::COLOR_BUFFER_BIT);
         }
 
-        // Swap buffers
-        window.swap_buffers();
+        window.swap_buffers().unwrap();
+
+        if let glutin::Event::Closed = event {
+            break;
+        }
     }
 }
diff --git a/gl/examples/struct_triangle.rs b/gl/examples/struct_triangle.rs
deleted file mode 100644
index af0ca2d5..00000000
--- a/gl/examples/struct_triangle.rs
+++ /dev/null
@@ -1,186 +0,0 @@
-// Copyright 2013 The gl-rs developers. For a full listing of the authors,
-// refer to the AUTHORS file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#![feature(globs)]
-#![feature(phase)]
-
-#[phase(plugin)]
-extern crate gl_generator;
-extern crate glfw;
-extern crate libc;
-
-use glfw::{Context, OpenGlProfileHint, WindowHint, WindowMode};
-use std::mem;
-use std::ptr;
-use std::str;
-
-use self::gl::Gl;
-use self::gl::types::*;
-
-#[allow(non_camel_case_types)]
-#[allow(non_snake_case)]
-pub mod gl {
-    use self::types::*;
-    generate_gl_bindings! {
-        api: "gl",
-        profile: "core",
-        version: "3.2",
-        generator: "struct",
-    }
-}
-
-// Vertex data
-static VERTEX_DATA: [GLfloat; 6] = [
-     0.0,  0.5,
-     0.5, -0.5,
-    -0.5, -0.5
-];
-
-// Shader sources
-static VS_SRC: &'static str =
-   "#version 150\n\
-    in vec2 position;\n\
-    void main() {\n\
-       gl_Position = vec4(position, 0.0, 1.0);\n\
-    }";
-
-static FS_SRC: &'static str =
-   "#version 150\n\
-    out vec4 out_color;\n\
-    void main() {\n\
-       out_color = vec4(1.0, 1.0, 1.0, 1.0);\n\
-    }";
-
-fn compile_shader(gl: &Gl, src: &str, ty: GLenum) -> GLuint {
-    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);
-
-        // Get the compile status
-        let mut status = gl::FALSE as GLint;
-        gl.GetShaderiv(shader, gl::COMPILE_STATUS, &mut status);
-
-        // Fail on error
-        if status != (gl::TRUE as GLint) {
-            let mut len = 0;
-            gl.GetShaderiv(shader, 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.GetShaderInfoLog(shader, len, ptr::null_mut(), buf.as_mut_ptr() as *mut GLchar);
-            panic!("{}", str::from_utf8(buf.as_slice()).expect("ShaderInfoLog not valid utf8"));
-        }
-    }
-    shader
-}
-
-fn link_program(gl: &Gl, 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"));
-        }
-    }
-    program
-}}
-
-fn main() {
-    let glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
-
-    // Choose a GL profile that is compatible with OS X 10.7+
-    glfw.window_hint(WindowHint::ContextVersion(3, 2));
-    glfw.window_hint(WindowHint::OpenglForwardCompat(true));
-    glfw.window_hint(WindowHint::OpenglProfile(OpenGlProfileHint::Core));
-
-    let (window, _) = glfw.create_window(800, 600, "OpenGL", WindowMode::Windowed)
-        .expect("Failed to create GLFW window.");
-
-    // It is essential to make the context current before calling `gl::load_with`.
-    window.make_current();
-
-    // Load the OpenGL function pointers
-    let gl = Gl::load_with(|s| window.get_proc_address(s));
-
-    // Create GLSL shaders
-    let vs = compile_shader(&gl, VS_SRC, gl::VERTEX_SHADER);
-    let fs = compile_shader(&gl, FS_SRC, gl::FRAGMENT_SHADER);
-    let program = link_program(&gl, vs, fs);
-
-    let mut vao = 0;
-    let mut vbo = 0;
-
-    unsafe {
-        // Create Vertex Array Object
-        gl.GenVertexArrays(1, &mut vao);
-        gl.BindVertexArray(vao);
-
-        // Create a Vertex Buffer Object and copy the vertex data to it
-        gl.GenBuffers(1, &mut vbo);
-        gl.BindBuffer(gl::ARRAY_BUFFER, vbo);
-        gl.BufferData(gl::ARRAY_BUFFER,
-                      (VERTEX_DATA.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
-                      mem::transmute(&VERTEX_DATA[0]),
-                      gl::STATIC_DRAW);
-
-        // Use shader program
-        gl.UseProgram(program);
-        "out_color".with_c_str(|ptr| gl.BindFragDataLocation(program, 0, ptr));
-
-        // Specify the layout of the vertex data
-        let pos_attr = "position".with_c_str(|ptr| gl.GetAttribLocation(program, ptr));
-        gl.EnableVertexAttribArray(pos_attr as GLuint);
-        gl.VertexAttribPointer(pos_attr as GLuint, 2, gl::FLOAT,
-                               gl::FALSE as GLboolean, 0, ptr::null());
-    }
-
-    while !window.should_close() {
-        // Poll events
-        glfw.poll_events();
-
-        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);
-        }
-
-        // Swap buffers
-        window.swap_buffers();
-    }
-
-    unsafe {
-        // Cleanup
-        gl.DeleteProgram(program);
-        gl.DeleteShader(fs);
-        gl.DeleteShader(vs);
-        gl.DeleteBuffers(1, &vbo);
-        gl.DeleteVertexArrays(1, &vao);
-    }
-}
diff --git a/gl/examples/static_triangle.rs b/gl/examples/triangle.rs
similarity index 86%
rename from gl/examples/static_triangle.rs
rename to gl/examples/triangle.rs
index 439d465f..247af467 100644
--- a/gl/examples/static_triangle.rs
+++ b/gl/examples/triangle.rs
@@ -14,10 +14,9 @@
 // limitations under the License.
 
 extern crate gl;
-extern crate glfw;
+extern crate glutin;
 
 use gl::types::*;
-use glfw::{Context, OpenGlProfileHint, WindowHint, WindowMode};
 use std::mem;
 use std::ptr;
 use std::str;
@@ -93,21 +92,14 @@ fn link_program(vs: GLuint, fs: GLuint) -> GLuint { unsafe {
 } }
 
 fn main() {
-    let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
-
-    // Choose a GL profile that is compatible with OS X 10.7+
-    glfw.window_hint(WindowHint::ContextVersion(3, 2));
-    glfw.window_hint(WindowHint::OpenGlForwardCompat(true));
-    glfw.window_hint(WindowHint::OpenGlProfile(OpenGlProfileHint::Core));
-
-    let (mut window, _) = glfw.create_window(800, 600, "OpenGL", WindowMode::Windowed)
-        .expect("Failed to create GLFW window.");
+    let window = glutin::Window::new().unwrap();
 
     // It is essential to make the context current before calling `gl::load_with`.
-    window.make_current();
+    unsafe { window.make_current() }.unwrap();
 
     // Load the OpenGL function pointers
-    gl::load_with(|s| window.get_proc_address(s));
+    // TODO: `as *const _` will not be needed once glutin is updated to the latest gl version
+    gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
 
     // Create GLSL shaders
     let vs = compile_shader(VS_SRC, gl::VERTEX_SHADER);
@@ -143,10 +135,7 @@ fn main() {
                                 gl::FALSE as GLboolean, 0, ptr::null());
     }
 
-    while !window.should_close() {
-        // Poll events
-        glfw.poll_events();
-
+    for event in window.wait_events() {
         unsafe {
             // Clear the screen to black
             gl::ClearColor(0.3, 0.3, 0.3, 1.0);
@@ -156,12 +145,15 @@ fn main() {
             gl::DrawArrays(gl::TRIANGLES, 0, 3);
         }
 
-        // Swap buffers
-        window.swap_buffers();
+        window.swap_buffers().unwrap();
+
+        if let glutin::Event::Closed = event {
+            break;
+        }
     }
 
-    unsafe {
     // Cleanup
+    unsafe {
         gl::DeleteProgram(program);
         gl::DeleteShader(fs);
         gl::DeleteShader(vs);


Test patch: