What does glfwGetWindowUserPointer do?
Asked Answered
D

1

7

When looking through the GLFW reference I came across the glfwGetWindowUserPointer function (and the glfwSetWindowUserPointer function). In the reference it says the following about the user-pointer:

Each window has a user pointer that can be set with glfwSetWindowUserPointer and fetched with glfwGetWindowUserPointer. This can be used for any purpose you need and will not be modified by GLFW throughout the life-time of the window.

Now I wonder for what purpose one could use this?

Decal answered 13/3, 2019 at 15:46 Comment(0)
D
14

I am not going to take credit for this answer, as it is not my answer, but the answer of someone else on the GLFW forum.

A UserData field is a fairly common paradigm in C APIs that lets the user access contextual data from within callbacks without needing to make everything global. In essence, it lets you associate an arbitrary piece of data relevant to your program with a glfw window.

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.

Decal answered 16/3, 2019 at 9:42 Comment(1)
I'm going to give you thumbs up tor this, because by simply sharing it you are solving so many of my problems at once! Thanks! +1Doering

© 2022 - 2024 — McMap. All rights reserved.