glfw-rs passing Closure as fn-pointer to glfwSet*Callback

Viewed 102

I am trying to set up glfw event callbacks. I haven't found any glfw-rs examples of it yet, unfortunately.

This is what I've been trying to call so far:

glfwSetWindowCloseCallback(
  window  as *mut GLFWwindow,
  Some(|window| {
      /* some code (doesn't work without it either) */
  })
);

It gives the following mismatched error at `Some(|window|{})`:
note: expected fn pointer `extern "C" fn(*mut glfw_window::glfw::ffi::GLFWwindow)` found closure `[closure@src\platform\glfw\glfw_window.rs:91:22: 95:18]`
label: expected "C" fn, found "Rust" fn

I haven't found any way yet to convert the closure to a C Fn Pointer (GLFWwindowclosefun specifically in my example). There's unfortunately not a single glfw-rs example of the glfwSet*Callback functions that I could find. If anyone's got some resources, that would be helpful too!

1 Answers

Basically, glfw-rs does not expose functions which set the custom callback for a specific event. (See this link) Instead, glfw-rs merged such callback-based functionalities into their fancy event system.

For example, with respect to glfwSetWindowCloseCallback, the similar can be achieved by doing the following, i.e. calling window.set_close_polling(true) after window creation, and then waiting for the close event in the event polling.

This link will be your help.

Related