0

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

main.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 时也发生了类似的事情,所以我认为我可能做错了一些基本错误,但我不知道是什么。

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

4

1 回答 1

2

此问题可能与您正在运行代码的线程有关。一些库与进程开始执行的实际主线程有密切关系,这通常是图形库和窗口系统调用的情况。Rust 主任务不在主线程上执行,但 Rust 库确实有办法在主线程上执行任务。请参阅伺服的示例

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

于 2013-02-11T23:41:40.783 回答