在 GLFW
中设置回调函数时要使用什么数据类型
?
我尝试设置一个函数来键入 void
,但给我一个编译错误
。将其更改为 void __stdcall
会给我一个 RT 错误
,但我如何使用 GLFW
的 typedef
比如 GLFW*fun
?我认为这是正确的做法。我真的需要示例源代码。
顺便说一句,我将 GLFW 定义为 GLFW_DLL
更新
我的代码是这样的:
(我用了三种方式)
1
原型(prototype)。因为 these*
在 main
下面
这个给了我一个编译错误(无效转换)
void MouseClick(int, int);
void Keyboard(int, int);
//...
2
//appending `__stdcall` get compiled but gives me `RT error`
void __stdcall MouseClick(int, int);
void __stdcall Keyboard(int, int);
3
在我的其他项目中,我使用 GLFW_BUILD_DLL
并在 MSVS
中使用转换进行编译。
glfwSetKeyCallback((GLFWkeyfun)键盘);
但是现在,我无法在 MinGW
中使用 GLFW_DLL
void MouseClick(int x, int y) {
}
在 main
...
int main() {
//glfw intialization...
glfwSetKeyCallback(Keyboard);
//other setters
}
但是,我该怎么做呢?
GLFWmouseposfun MousePos {
}
//...
最佳答案
根据 the documentation对于 GLFW v2.x,您传递给 glfwSetKeyCallback
(等)的原型(prototype)是
void GLFWCALL functionname( int key, int action );
这是一个完整的 C 程序,它将打印出关键事件:
#include <stdio.h>
#include <GL/glfw.h>
void GLFWCALL keyfun(int key, int action) {
printf("%d %d\n", key, action);
}
int main() {
glfwInit();
glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
glfwSetKeyCallback(keyfun);
while(1) {
glfwSwapBuffers();
}
}
如果此代码不起作用,您可能无法正确链接到 GLFW。
关于c++ - GLFW 设置回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16168342/