mardi 27 septembre 2016

Use lambda as GLFWkeyfun

For one of my project, I am using GLFW3. And I am trying to give a C++11 lambda to the function GLFWSetKeyCallback, who takes as parameters a GFLWwindow, and a GLFWfunkey function, which is a typedef to void(* GLFWkeyfun) (GLFWwindow *, int, int, int, int).

The function is called from my Window class, here's the code

window.hpp

class Window
{
        public:
                Window(GLuint width, GLuint height, std::string title);
                ~Window();

                GLFWwindow* get() { return window_.get(); }
                bool is_open() { return open_; };
                void close();

        private:
                bool open_ = false;
                std::unique_ptr<GLFWwindow, DestroyWindow> window_;
                std::vector<Drawable*> drawables_;
};

And window.cpp

#include <pck/window.hpp>

Window::Window(GLuint width, GLuint height, std::string title) :
        open_(true)
{
        window_.reset(glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr));
    glfwMakeContextCurrent(window_.get());

        Global::width = width;
        Global::height = height;

        glfwSetKeyCallback(window_.get(), [this](GLFWwindow* window, int key, int scancode, int action, int mode){
                for(auto it : drawables_)
                        it->input(window, key, scancode, action, mode);
        });
}

Window::~Window()
{
        window_.reset();
}

void
Window::close()
{
        open_ = false;
        window_.reset();
}

And the error from the compiler (clang++ 3.8.1)

fatal error: no matching function for call to 'glfwSetKeyCallback'
        glfwSetKeyCallback(window_.get(), [this](GLFWwindow* window, i...
        ^~~~~~~~~~~~~~~~~~
/usr/include/GLFW/glfw3.h:3307:20: note: candidate function not viable: no
      known conversion from '(lambda at
      /path/to/file.cpp)' to
      'GLFWkeyfun' (aka 'void (*)(GLFWwindow *, int, int, int, int)') for 2nd argument
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow* window, GLFWkeyfun cbfun);

Can somebody point me to what I did wrong please?

Thanks!

Aucun commentaire:

Enregistrer un commentaire