#opengl #vulkan #windowing #glfw-window

glfw_raw_window_handle_0_5_0

GLFW3绑定和Rust的惯用包装

1 个不稳定版本

0.48.0 2022年12月28日

#295 in 图形API

Apache-2.0

165KB
3K SLoC

glfw-rs

Crates.io Docs.rs Build Status

GLFW绑定和Rust编程语言的包装

示例

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)
        }
        _ => {}
    }
}

使用glfw-rs

先决条件

确保您已编译和安装了GLFW 3.x。您可能在软件包管理器中找到它,例如在OS X上:brew install glfw3(您可能需要运行brew tap homebrew/versions)。如果没有,您可以从GLFW网站上提供的源代码下载并构建库。注意,如果您在Linux上使用CMake编译GLFW,您必须提供-DCMAKE_C_FLAGS=-fPIC参数。您可以将GLFW安装到您的PATH中,否则您必须在调用makemake lib时指定包含库二进制的目录。

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

将glfw-rs包含到您的项目中

将以下内容添加到您的Cargo.toml

[dependencies.glfw]
version = "*"

在Windows上

默认情况下,glfw-rs会尝试编译glfw库。如果您想链接到您自定义构建的glfw,或者构建失败(在Windows上可能是这种情况),您可以禁用此功能

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

支持

在irc.mozilla.org上的bjz上联系#rust#rust-gamedev,或在GitHub上提交问题

glfw-rs的使用案例

依赖项

~1.6–5MB
~52K SLoC