Poll a single input event at a time without a callback function?

Using GLFW
  • Lyle

    Lyle - 2014-06-24

    Hi. I would like to start by saying thank you for creating GLFW. I'm creating a 3D game engine and am having some difficulty trying to integrate your input handling into my component based design. I am using GLFW 3.0.4

    In SFML (which was what I was using when I was learning how to do 2D games) I was able to process the input events one at a time from within my input system object like this

    // check all the window's events that were triggered since the last iteration of the loop
    sf::Event event;
    while(pWindow->pollEvent(event)) {
    // Handle event

    As far as I can tell with GLFW, I must register a callback function in order to process input events (or explicitly check the state of every key). However, my input system does not follow a singleton design pattern so giving it a static function to use as a callback would complicate my system more than I would like.

    Is there a way to handle input in a way which is similar to the above SFML example? I would rather not include SFML in my project just for it's input handling.

    Last edit: Lyle 2014-06-24
  • Camilla Löwy

    Camilla Löwy - 2014-06-24

    Use the window user pointer to find your input system object from the callbacks. That's what I do.

  • Lyle

    Lyle - 2014-06-25

    Thank you for the fast response. I've been messing with it trying to get even a dummy static member function to work, but it crashes every time.

    I have a
    static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);

    KeyCallback is just a dummy function defined as
    void Input::KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {}

    I tried
    glfwSetKeyCallback(pWindow, KeyCallback);

    But this function causes my program to crash. The compiler doesn't give me any clues. When I comment out the glfwSetKeyCallback function, my program does not crash. I looked at your code and it looks like I'm using the same syntax as you.

    Do you have any idea what could be going on?

    EDIT: nevermind. I figured out what was wrong. I accidentally gave it a null pointer that I thought pointed to the GLFWwindow. My bad.

    EDIT2: I now have glfwSetWindowUserPointer glfwGetWindowUserPointer working with static callback functions in my engine. Thank you for the advice and example.

    Last edit: Lyle 2014-06-25

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks