0

I am working with a c library that uses setCallback functions. These functions take a function with a fixed signature, e.g.:

int glfwSetCursorPosCallback(window, mouse_move);

Where mouse_move has signature

void mouse_move(GLFWwindow * window, double xpos, double ypos)

But suppose mouse_move depends on other state beyond these function arguments, like a camera struct? In c, I would just make camera a global variables. However, I am working in Cython and I want to use glfwSetCursorPosCallback without recourse to global variables, which are unpythonic and messy. If camera is a class or instance attribute, is there any way to access it from within mouse_move without passing it in as an argument?

ethanabrooks
  • 747
  • 8
  • 19
  • 1
    This isn't really possible in Cython. I've [recommended using ctypes for similar problems in the past](https://stackoverflow.com/questions/34878942/using-function-pointers-to-methods-of-classes-without-the-gil/34900829#34900829) to allow you to pass a generic Python callable but the `GLFWwindow *` window parameter makes it difficult to do that here. – DavidW Nov 15 '17 at 19:41
  • There is also the possibility that `GLFWwindow` allow you to store custom data (via a void pointer, for instance) that could then be accessed from your callback. This depends on the library being used, of course. This remains quite independent from Cython. – Pierre de Buyl Nov 16 '17 at 08:42

1 Answers1

0

This is not a solution to the general problem, but for glfw callbacks, it is possible to associate a void* with your GLFWwindow as follows:

glfwSetWindowUserPointer(window, yourVoidPtr);

and access it later (e.g. in a callback):

YourData *data = (YourData *) glfwGetWindowUserPointer(window);
ethanabrooks
  • 747
  • 8
  • 19