0

I have a simple program in glfw, which uses glfw.glfwSetCursorPos to put the mouse in a certain spot on the screen. However, it was raising a lot of errors, until I found this thread: ctypes.ArgumentError: Don't know how to convert parameter. Now, it works, but one of its parameters is the returned value from glfw.CreateWindow that you want to center on. But, following the accepted and only answer on that thread, I have no idea what variable in ctypes I should use to represent the window in the argtypes for that function, if there even is a variable for a glfw window. And if there isn't one, is there any other way to fix my original problem?

Thanks!

EDIT

Here is a minimal reproductible example. The moment you move the mouse, you can see that it starts spamming to 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()
User 12692182
  • 927
  • 5
  • 16
  • Please tell me if there is anything else relevant you think I should add to my question, for example, my OS, or my `glfw` version – User 12692182 May 07 '20 at 13:33
  • I think it should be struct? Worse case, just try all the ctypes, shouldnt be too many. Check this page out. See typedef struct GLFWwindow https://www.glfw.org/docs/3.0/group__window.html#ga3c96d80d363e67d13a41b5d1821f3242 – Jason Chia May 07 '20 at 13:44
  • Thanks for the suggestion, but it wants an instance of `GLFWwindow`, and it says I pass it an LP_GLFWwindow. I'll try to do a bit more browsing in the docs, see if I can find anything – User 12692182 May 07 '20 at 13:51
  • Can you see if the following answer has any help for you? https://stackoverflow.com/questions/19993078/looking-for-a-simple-opengl-3-2-python-example-that-uses-glfw – Jason Chia May 07 '20 at 14:08
  • Thanks, but it does not. This is what I originally built my code off of, and my problem is about how to safely execute `glfw.glfwSetCursorPos`, not have a simple example of a rotating star. – User 12692182 May 07 '20 at 14:12
  • Well, then I need to have a look at your code snippet to help any further. – Jason Chia May 07 '20 at 14:55
  • I managed to solve my problem. My answer is below – User 12692182 May 07 '20 at 16:15
  • You don't have to specify anything. *glfw* already does that. – CristiFati May 07 '20 at 17:24
  • Then why was it not working? – User 12692182 May 07 '20 at 18:31
  • I don't know. You didn't specify what is not working. also when I tried to run your code, I got an error at `glfw.glfwInit()` (nit found). Don't know what you're doing there (also those *OpenGL* imports don;t seem to be needed). – CristiFati May 08 '20 at 07:20

1 Answers1

0

glfwCreateWindow returns a handle to your window. As far as I know, this handle is not represented by any sort of library out there, even glfw. Thankfully, there is a builtin python function that will allow you to create an argument passable to argtypes, type. As far as I know, this only works with the current context, but it does work:

glfw.glfwSetCursorPos.argtypes = [
                                  type(self.win) # or whatever else your window is named
                                  ctypes.c_float
                                  ctypes.c_float]
User 12692182
  • 927
  • 5
  • 16