类内的 GLFW 回调

GLFW callback inside class

本文关键字:回调 GLFW      更新时间:2023-10-16

这有效: (主要(:

glfwSetCharCallback(window, Console_Input);

(全球(:

void Console_Input(GLFWwindow* window, unsigned int letter_i){
}

如果我试着把它放在课堂上: (主要(:

Input_text Text_Input(&Text_Input_bar, &GLOBALS);
glfwSetCharCallback(window, Text_Input.Console_Input);

(全球(:

class Input_text{
...
void Console_Input(GLFWwindow* window, unsigned int letter_i){
}
void Update(){
if(active == 1){
MBar->Re_set_bar_width(str);
MBar->update_bar();
}
}
};

它不起作用。我收到错误: 无法将"Input_text::Console_Input"从类型"void (Input_text::)(GLFWwindow*, unsigned int("转换为类型"GLFWcharfun {aka void (((GLFWwindow, unsigned int(}" 我不想在回调函数中编写功能。我需要自我管理课程。有没有办法将 glfwSetCharCallback 设置为类中的函数?

回调必须是函数(或静态方法(,但您可以将用户指针关联到GLFWindow。请参阅glfwSetWindowUserPointer

指针可以按glfwGetWindowUserPointerGLFWWindow对象检索

将指向Text_Input的指针关联到window

Input_text Text_Input(&Text_Input_bar, &GLOBALS);
glfwSetWindowUserPointer(window, &Text_Input);
glfwSetCharCallback(window, Console_Input);

window获取指针,并将类型为void*的指针投射到Input_text *(可悲的是你必须进行强制转换(。

void Console_Input(GLFWwindow* window, unsigned int letter_i)
{
Input_text *ptr= (Input_text *)glfwGetWindowUserPointer(window); 
ptr->Console_Input(window, i); 
}

void Console_Input(GLFWwindow* window, unsigned int letter_i)应该是全局函数,它不能是类的成员函数

回答你的问题:不,没有办法把它放在课堂上。