clojurelwjgl

How to create a glfwKeyCallback


I've been trying to follow this tutorial. This is my callback:

(def cb (proxy [java.lang.Object GLFWKeyCallbackI] []
          (invoke [window keycode _ action _]
            (when (and (= keycode GLFW/GLFW_KEY_ESCAPE)
                       (= action GLFW/GLFW_PRESS))
              (GLFW/glfwSetWindowShouldClose window true)))))

and here I register it:

(GLFW/glfwSetKeyCallback window cb)

Which gives me this error:

Execution error (UnsupportedOperationException) at lwjgl_intro.core.proxy$java.lang.Object$GLFWKeyCallbackI$84f3fd50/address (REPL:-1).
address

My full code is here.

Why am I getting this error?

EDIT: This is the code snippet from the tutorial linked above:

    glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
        if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
            glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
    });

Solution

  • Update: the scratched over text below is irrelevant because I didn't notice the default implementation of address.

    In the error, you can see that the method that was actually called is address, and your proxy does not implement it.

    GLFWKeyCallbackI extends CallbackI - that's where that method comes from.

    In order for a type to implement all the interfaces properly, it has to implement all the methods up the whole interface extension chain. And there are methods other than address and invoke, so your proxy has to be extended quite a bit before you can properly use it.

    Also, there's no need to specify java.lang.Object there - all types already inherit it by default, implicitly.

    You can use reify instead:

    (def cb (reify GLFWKeyCallbackI
              (invoke [_ window keycode _ action _]
                (when (and (= keycode GLFW/GLFW_KEY_ESCAPE)
                           (= action GLFW/GLFW_PRESS))
                  (GLFW/glfwSetWindowShouldClose window true)))))