通过GLUT打开的窗口与Rust FFI冻结

时间:2013-02-11 00:21:42

标签: macos opengl glut rust

问题的标题几乎描述了这个场景。我尝试使用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();
}

glut.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中最新的。出于某种原因,最近的一次不会编译。

1 个答案:

答案 0 :(得分:2)

此问题可能与您运行代码的线程有关。一些库对进程开始执行的实际主线程具有亲和力,这通常是图形库和调用窗口系统的情况。主要的Rust任务不在主线程上执行,但Rust库确实有一种方法可以在主线程上执行任务。请参阅example from servo

如果这不能让你解开,那么你可能需要创建一个.app文件夹来运行你的OS X应用程序。我对这些细节并不完全熟悉,但我相信,如果没有一个,可口可乐API就无法完全发挥作用。