I see a lot of talk about glfwGetWindowUserPointer() and glfwSetWindowUserPointer() but I can't for the life of me understand what these functions actually do.
From http://discourse.glfw.org/t/what-is-a-possible-use-of-glfwgetwindowuserpointer/1294:
"If you are trying to use glfw in a program that follows object-oriented design, for example, you can use this pointer to store the address of the instance that is handling a particular window, and forward the callbacks (which have to be static functions, because of the way the API works) to the appropriate member functions."
The author of that comment then posts the following example:
/* Constructor for the MyWindowHandler class */
MyWindowHandler (GLFWwindow * window)
{
....
glfwSetWindowUserPointer(window, reinterpret_cast<void *>(this));
}
/* Key event callback wrapper function */
static void key_callback (GLFWwindow * window, int key, int scancode, int
action, int mods)
{
MyWindowHandler * handler =
reinterpret_cast<MyWindowHandler *>(glfwGetWindowUserPointer(window));
if (handler)
handler->keyEvent(key, scancode, action, mods);
}
What confuses me is that (at least as far as I can tell) I was able to achieve the exact same outcome in my program without using glfwSetWindowUserPointer() or glfwGetWindowUserPointer() at all.
/* Class constructor (just provided for reference) */
GLFW::GLFW(int right, int top)
: m_Window(0)
{
if (!glfwInit())
{
std::cout << "GLFW initialisation failed!\n";
__debugbreak();
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
m_Window = glfwCreateWindow(right, top, "OpenGL Window", NULL, NULL);
if (!m_Window)
{
glfwTerminate();
std::cout << "Could not create window!\n";
__debugbreak();
}
glfwMakeContextCurrent(m_Window);
glfwSwapInterval(2);
if (glewInit() != GLEW_OK)
{
std::cout << "GLEW initialisation failed!\n";
__debugbreak();
}
std::cout << glGetString(GL_VERSION) << std::endl;
}
/* My callback member function */
void GLFW::MouseMovement(int x, int y)
{
std::cout << "Mouse moved\n";
// do other stuff...
}
/* Main.cpp */
static void MouseMovementCallback(GLFWwindow* window, double x, double y)
{
GLFW* glfw = nullptr;
glfw->MouseMovement(x , y);
}
int main()
{
GLFW glfw(1200, 600);
glfwSetCursorPosCallback(glfw.GetWindowID(), MouseMovementCallback);
// ......
// ......
}
In the above code my 'MouseMovement' callback function was called... so I'm rather stumped as to what the purpose of the glfwGetWindowUserPointer() and glfwSetWindowUserPointer() functions actually is.
I'm sure there is something rather obvious that I am missing here, hopefully someone can enlighten me.
Thanks in advance.