Work with mouse in glfw
-
Started working with C+ and the glfw library, and I don't know how I'm supposed to get a mice position on the screen to be a function that processs the application to the mice button. I mean, if I click, I want to know what part of the screen I threw.
//window.cpp
GLFWwindow* Window::init()
{
if(!glfwInit())
exit(1);glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); glfwWindowHint(GLFW_DECORATED, false); GLFWwindow* window = glfwCreateWindow(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), "Program", NULL, NULL); glfwMakeContextCurrent(window); glfwSwapInterval(1); set_callbacks(window); set_icon(window); return window;
}
void Window::set_callbacks(GLFWwindow* window)
{
Input_Processing input;
glfwSetKeyCallback(window, input.Key_Callback);
glfwSetCursorPosCallback(window, input.Cursor_Position_Callback);
glfwSetMouseButtonCallback(window, input.Mouse_Button_Callback);
}...
//input_processing.cppvoid Input_Processing::Cursor_Position_Callback(GLFWwindow* window, double xPos, double yPos)
{
//Данные по положению мыши тут
}void Input_Processing::Mouse_Button_Callback(GLFWwindow* window, int button, int action, int mods)
{
//а нужны они мне тут
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{} if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) { }
}
In the gide on glfw, all examples of callsback's kind of void, i.e. non-returned
-
Yeah, all the bulbs return void because they're called the library itself. But even if they did, it would be difficult to use it. The problem you describe is known to the librarians and they've decided.
If you look at the colbeks, do they all (or almost everything?) get the first indicator on the specimen?
GLWFWindow
♪ This class has inside. https://www.glfw.org/docs/3.3/window_guide.html#window_userptr (usually referred to as such) user data User data, the contents of which are given to the full user. To access this variable, there are two functionsglfwSetWindowUserPointer
(installation) andglfwGetWindowUserPointer
(induction).How to use it. Start a class that will store all the information you need (which is more important to you). Let it be a class.
MyWordlData
♪Now, after the line.
GLFWwindow* window = glfwCreateWindow(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), "Program", NULL, NULL);
You can write that.
MyWorldData data; glfwSetWindowUserPointer(window, &data);
But that's what you can do if it's called in.
main
♪ If such a code is left in a simple function, it's easy to get. Index♪But you can go easy - this code is called inside the class function. Which means we can get there.
this
♪ That's the root change of business.glfwSetWindowUserPointer(window, this);
Okay, what do we do now?
void Input_Processing::Cursor_Position_Callback(GLFWwindow* window, double xPos, double yPos) { //Данные по положению мыши тут Window* w = static_cast<Window*>(glfwGetWindowUserPointer(window)); assert(w != nullptr); // ну мало чего // все, теперь можно пользоваться w как указателем на Window*!!! w->MoveMyHeroe(xPos, yPos); // Ну или Ваша функция. }
If there's a sign on another class, then go straight to your caste.
Right here. https://stackoverflow.com/questions/27596861/give-static-function-access-to-data-without-passing-the-data-as-a-parameter It's almost the same.