2

我最近开始涉足 DerelictGLFW。我有两个类,一个是一个Window类,另一个是一个InputHandler类(窗口事件的事件管理器)。在我的光标位置回调中,我获取窗口用户指针并尝试设置位置,但在尝试设置回调和 GLFW 之外的任何值时,我立即收到访问冲突错误。GLFW 已初始化,不报告任何错误。感谢您的时间。

Class Window
{
    private:
        double cursorX;

    ...other stuffs...

    @property
    void cursorX(double x) nothrow
    {
        cursorX = x;
    }
}

extern(C) void mousePosCallback(GLFWwindow* window, double x, double y)
{
    Window* _window = window.userPointer 
    //userPointer is a static function that gets the window user pointer
    //and casts it to a Window*

    _window.cursorX = x;
}

static Window* userPointer(GLFWwindow* window)
{
    return cast(Window*) glfwGetWindowUserPointer(window);
}

编辑:

添加extern(C)到回调,错误仍然存​​在。

将“立即进入回调”更正为“立即尝试设置回调和 GLFW 之外的任何值”。

增加userPointer提问功能

4

2 回答 2

1

mousePosCallback 必须在 extern(C) 块中声明。这是为了使调用约定匹配。

extern (C) void mousePosCallback(GLFWwindow* window, double x, double y)
{
    Window* _window = window.userPointer 
    //userPointer is a static function that gets the window user pointer
    //and casts it to a Window*

    _window.cursorX = x;
}
于 2015-04-19T13:47:24.497 回答
0

看来我已经发现了错误的根源。在窗口初始化期间,我尝试将用户指针设置为this. 我不知道为什么,但是将它移到一个不从构造函数调用的不同函数中似乎可以解决这个问题。问题已解决,但谁能帮我理解为什么?这对我来说似乎很奇怪。

于 2015-04-22T12:07:05.453 回答