74 releases (breaking)

new 0.59.0 Nov 28, 2024
0.58.0 Jul 25, 2024
0.57.0 Jun 14, 2024
0.55.0 Feb 15, 2024
0.0.6 Mar 7, 2015

#26 in Graphics APIs

Download history 2477/week @ 2024-08-13 3321/week @ 2024-08-20 2518/week @ 2024-08-27 2819/week @ 2024-09-03 3087/week @ 2024-09-10 2810/week @ 2024-09-17 3787/week @ 2024-09-24 2655/week @ 2024-10-01 905/week @ 2024-10-08 1591/week @ 2024-10-15 1806/week @ 2024-10-22 1704/week @ 2024-10-29 1580/week @ 2024-11-05 1136/week @ 2024-11-12 1670/week @ 2024-11-19 1672/week @ 2024-11-26

6,332 downloads per month
Used in 100 crates (81 directly)

Apache-2.0

185KB
3.5K SLoC

glfw-rs

Crates.io Docs.rs Build Status

GLFW bindings and wrapper for The Rust Programming Language.

Example

extern crate glfw;

use glfw::{Action, Context, Key};

fn main() {
    let mut glfw = glfw::init(glfw::fail_on_errors).unwrap();

    let (mut window, events) = glfw.create_window(300, 300, "Hello this is window", glfw::WindowMode::Windowed)
        .expect("Failed to create GLFW window.");

    window.set_key_polling(true);
    window.make_current();

    while !window.should_close() {
        glfw.poll_events();
        for (_, event) in glfw::flush_messages(&events) {
            handle_window_event(&mut window, event);
        }
    }
}

fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) {
    match event {
        glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => {
            window.set_should_close(true)
        }
        _ => {}
    }
}

Using glfw-rs

Prerequisites

Make sure you have compiled and installed GLFW 3.x. You might be able to find it on your package manager, for example on OS X: brew install glfw3 (you may need to run brew tap homebrew/versions). If not you can download and build the library from the source supplied on the GLFW website. Note that if you compile GLFW with CMake on Linux, you will have to supply the -DCMAKE_C_FLAGS=-fPIC argument. You may install GLFW to your PATH, otherwise you will have to specify the directory containing the library binaries when you call make or make lib:

GLFW_LIB_DIR=path/to/glfw/lib/directory make

Including glfw-rs in your project

Add this to your Cargo.toml:

[dependencies.glfw]
version = "*"

On Windows

By default, glfw-rs will try to compile the glfw library. If you want to link to your custom build of glfw or if the build doesn't work (which is probably the case on Windows), you can disable this:

[dependencies.glfw]
version = "*"
default-features = false

Raw window handle 0.5.0 compatibility

By default, glfw-rs uses raw-window-handle at v0.6.0. If your project is depending on glfw-rs with raw-window-handle v0.5.0, then use this line in your Cargo.toml:

glfw = { version = 0.56.0 , default-features = false, features = ["with-window-handle-v0-5"] }

Support

Contact bjz on irc.mozilla.org #rust and #rust-gamedev, or post an issue on GitHub.

glfw-rs in use

glfw-rs with other graphical APIs

In certain circumstances OpenGL which GLFW uses can conflict with the new handle that the graphical API is also trying to use, causing crashes, to fix this add

glfw.window_hint(WindowHint::ClientApi(ClientApiHint::NoApi));

before creating the window. If using this however you cannot use certain built-in functions, such as window.swap_buffers(), window.make_current(), and glfw.set_swap_interval(), but these should have equivalents provided by the graphical API.

Dependencies

~1.5–4MB
~92K SLoC