我有一个简单的程序,在 glfw
,它使用 glfw.glfwSetCursorPos
来将鼠标放在屏幕上的某个位置。然而,它引发了很多错误,直到我找到这个帖子。ctypes.ArgumentError: 不知道如何转换参数. 现在,它工作了,但是它的一个参数是来自于 glfw.CreateWindow
的中心。但是,按照该主题的公认和唯一的答案,我不知道在 ctypes
我应该用这个函数的 argtypes 来表示窗口,如果有一个 glfw 窗口的变量的话。如果没有的话,有没有其他方法可以解决我原来的问题?
谢谢!
EDIT
下面是一个最小可复制的例子。当你移动鼠标的那一刻,你可以看到它开始发垃圾邮件到 sys.stderr
:
import OpenGL
from OpenGL.GL import *
import glfw
glfw.glfwInit()
def onMouseMove(win2, x, y):
print(x, y)
glfw.glfwSetCursorPos.argtypes = [glfw.GLFWwindow,
ctypes.c_float,
ctypes.c_float]
glfw.glfwSetCursorPos(win, float(50), float(50))
glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MAJOR, 3)
glfw.glfwWindowHint(glfw.GLFW_CONTEXT_VERSION_MINOR, 3)
glfw.glfwWindowHint(glfw.GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)
glfw.glfwWindowHint(glfw.GLFW_OPENGL_PROFILE, glfw.GLFW_OPENGL_CORE_PROFILE)
win = glfw.glfwCreateWindow()
glfw.glfwMakeContextCurrent(win)
glfw.glfwSetInputMode(win, glfw.GLFW_CURSOR,
glfw.GLFW_CURSOR_DISABLED)
glfw.glfwSetCursorPosCallback(win, onMouseMove)
while not glfw.glfwWindowShouldClose(win):
glfw.glfwPollEvents()
glfw.glfwTerminate()
glfwCreateWindow
返回一个窗口的句柄。据我所知,这个句柄并没有被任何类型的库所代表,即使是 glfw
. 幸运的是,有一个 builtin
python函数,允许你创建一个可传递给 argtypes
, type
. 据我所知,这只适用于当前的上下文,但它确实有效。
glfw.glfwSetCursorPos.argtypes = [
type(self.win) # or whatever else your window is named
ctypes.c_float
ctypes.c_float]