Reputation: 131
Were I to set a callback function in GLFW, let's say
glfwSetCursorPosCallback(window, mouse);
the most obvious way to retrieve the cursor position would be
vec2 m;
void mouse (GLFWwindow* window, GLdouble x, GLdouble y)
{
m = vec2 (x, y);
}
However, I would prefer to do so without using a global variable. Can it be done?
Upvotes: 1
Views: 480
Reputation: 211176
You can associate a user pointer to a GLFWindow
. See glfwSetWindowUserPointer
.
the pointer can be retrieved at an time form the GLFWWindow
object by glfwGetWindowUserPointer
struct MyWindowData
{
GLdouble x;
GLdouble y;
}
Associate a pointer to windowData
, to the window
:
MyWindowData windowData;
glfwSetWindowUserPointer( window, &windowData );
glfwSetCursorPosCallback( window, mouse );
Get the pointer form the window
and Cast the pointer of type void*
to MyWindowData *
(Sadly you have to do the cast).
void mouse(GLFWwindow* window, GLdouble x, GLdouble y)
{
MyWindowData *dataPtr = (MyWindowData*)glfwGetWindowUserPointer( window );
dataPtr->x = x;
dataPtr->y = y;
}
Upvotes: 5