简体   繁体   English

从回调函数检索数据

[英]Retrieving data from callback function

Were I to set a callback function in GLFW, let's say 我是否在GLFW中设置了回调函数

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? 能做到吗

You can associate a user pointer to a GLFWindow . 您可以将用户指针关联到GLFWindow See glfwSetWindowUserPointer . 请参阅glfwSetWindowUserPointer

the pointer can be retrieved at an time form the GLFWWindow object by glfwGetWindowUserPointer 可以通过glfwGetWindowUserPointer一次从GLFWWindow对象检索指针

struct MyWindowData
{
  GLdouble x;
  GLdouble y;
}

Associate a pointer to windowData , to the window : 将指向windowData的指针与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). window获取指针,并将void*类型的指针强制转换为MyWindowData * (很遗憾,您必须进行强制转换)。

void mouse(GLFWwindow* window, GLdouble x, GLdouble y)
{
   MyWindowData *dataPtr = (MyWindowData*)glfwGetWindowUserPointer( window ); 
   dataPtr->x = x;
   dataPtr->y = y; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM