gpt4 book ai didi

macos - 通过带有 Rust FFI 的 GLUT 打开的窗口卡住

转载 作者:行者123 更新时间:2023-11-29 08:28:09 26 4
gpt4 key购买 nike

问题的标题几乎描述了场景。我正在尝试使用 Rust 的 FFI 和 GLUT 库在 OS X 10.8 上打开一个 OpenGL 窗口。窗口打开,控制权移交给 glutMainLoop,但窗口不绘制任何内容(包括带有关闭、最小化、最大化按钮的标准标题栏!它只是一个白色方 block ),我每次都必须强制退出程序.目前,我使用的 FFI 是我通过查看其他 FFI 拼凑而成的实际功能的一小部分。这是我到目前为止的代码:

主要.rs:

use gl::*;
use glut::*;

fn main() {
glut::init();
glut::initWindowSize(640, 480);
glut::initWindowPosition(100, 100);
glut::initDisplayMode(glut::DOUBLE | glut::RGBA);
glut::createWindow("Test GLUT");

glut::displayFunc(|| {
io::println("Display func start");
gl::clear(gl::COLOR_BUFFER_BIT);

gl::begin(gl::TRIANGLES);
gl::vertex2f(-0.5, 0.0);
gl::vertex2f(0.0, 1.0);
gl::vertex2f(0.5, 0.0);
gl::end();

glut::swapBuffers();
io::println("Swapped buffers");
});
gl::clearColor(0.3, 0.3, 0.3, 0.3);

glut::keyboardFunc(|key: char, x: int, y: int| {
if(key == 'q') {
io::println("Pressed q");
}
});

io::println("Starting main loop");
glut::mainLoop();
}

过剩.rs:

use libc::{c_int, c_uint, c_char, c_uchar};
use task::local_data::{local_data_get, local_data_set};

#[cfg(target_os="macos")]
#[nolink]
#[link_args="-framework GLUT"]
extern mod dummy {
}

fn displayFuncTlsKey(callback: @@fn()) {
// Empty
}
extern fn displayFuncCallback() {
unsafe {
let callback = local_data_get(displayFuncTlsKey).get();
(*callback)();
}
}

fn keyboardFuncTlsKey(callback: @@fn(key: char, x: int, y: int)) {
// Empty
}
extern fn keyboardFuncCallback(key: c_uchar, x: c_int, y: c_int) {
unsafe {
let callback = local_data_get(keyboardFuncTlsKey).get();
(*callback)(key as char, x as int, y as int);
}
}

#[nolink]
extern mod glut_unsafe {
pub fn glutInit(argc: *c_int, argv: **c_char);
pub fn glutInitDisplayMode(mode: c_uint);
pub fn glutInitWindowPosition(x: c_int, y: c_int);
pub fn glutInitWindowSize(width: c_int, height: c_int);
pub fn glutCreateWindow(title: *c_char) -> c_int;

pub fn glutDisplayFunc(func: *u8);
pub fn glutKeyboardFunc(func: *u8);

pub fn glutMainLoop();
pub fn glutSwapBuffers();
}

mod glut {
pub const RGB: u32 = 0;
pub const RGBA: u32 = 0;

pub const SINGLE: u32 = 0;
pub const DOUBLE: u32 = 2;

pub fn init() {
unsafe {
let argc = 1 as c_int;
// I wonder how correct this is...
let command = str::as_c_str("draw", |s| s);
let argv: &[*c_char] = &[command, ptr::null()];
let argv_p: **c_char = vec::raw::to_ptr(argv);
// let argv_p: **c_char = cast::reinterpret_cast(&ptr::to_unsafe_ptr(&argv));
// let argv: (*u8, *u8) = (vec::raw::to_ptr(command), ptr::null());
// let argv_p: **c_char = cast::reinterpret_cast(&ptr::to_unsafe_ptr(&(command, ptr::null)));

glut_unsafe::glutInit(ptr::to_unsafe_ptr(&argc), argv_p);
}
}

pub fn initWindowSize(width: int, height: int) {
unsafe {
glut_unsafe::glutInitWindowSize(width as c_int, height as c_int)
}
}
pub fn initWindowPosition(x: int, y: int) {
unsafe {
glut_unsafe::glutInitWindowPosition(x as c_int, y as c_int)
}
}
pub fn initDisplayMode(mode: u32) {
unsafe {
glut_unsafe::glutInitDisplayMode(mode as c_uint)
}
}

pub fn createWindow(title: &str) -> int {
let mut bytes = str::as_c_str(title, {|s| s});
unsafe {
glut_unsafe::glutCreateWindow(bytes) as int
}
}

pub fn displayFunc(func: @fn()) {
unsafe {
local_data_set(displayFuncTlsKey, @func);
glut_unsafe::glutDisplayFunc(displayFuncCallback)
}
}
pub fn keyboardFunc(func: @fn(key: char, x: int, y: int)) {
unsafe {
local_data_set(keyboardFuncTlsKey, @func);
glut_unsafe::glutKeyboardFunc(keyboardFuncCallback)
}
}

pub fn mainLoop() {
unsafe {
glut_unsafe::glutMainLoop()
}
}
pub fn swapBuffers() {
unsafe {
glut_unsafe::glutSwapBuffers()
}
}
}

任何帮助弄清楚为什么会发生这种情况的帮助都会很棒。当我尝试使用 glfw 的绑定(bind)时也发生了类似的事情,所以我认为我可能做一些基本的错误,但我不知道是什么。

编辑:这是捆绑的 0.5 tarball,不是 GitHub 的最新版本。出于某种原因,最新的不会编译。

最佳答案

此问题可能与您运行代码的线程有关。一些库与进程开始执行的实际主线程有密切关系,图形库和对窗口系统的调用通常就是这种情况。主 Rust 任务不在主线程上执行,但 Rust 库确实有办法在主线程上执行任务。查看 example from servo .

如果这不能让您摆脱困境,那么您可能需要创建一个 .app 文件夹来运行您的 OS X 应用程序。我并不完全熟悉细节,但我相信 coca API 没有它就不能完全工作。

关于macos - 通过带有 Rust FFI 的 GLUT 打开的窗口卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14804256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com