On Friday, 28 October 2016 at 13:19:19 UTC, WhatMeWorry wrote:


Anyone have a good example of what I should be doing?

I generally use GLFW event callbacks to populate an event queue with custom event types, then process the queue and handle events elsewhere. That way, the callbacks need no nothing at all about the game code.

```
void setCallbacks() {
    glfwSetKeyCallback(&key_callback);
}

private:
MyEventQueue _eventq;

extern(C) void key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier) {
    eventq.push(KeyEvent(window, key, scancode, action, modifier);
}

An alternative approach is to forego the event queue and just have the callbacks process event listeners. The listeners could be delegates, function pointers, or interfaces, whatever works for your scenario, or some specific event handler class. They might be stored in flat arrays, or aas keyed on the window or a KeyEvent structure. Whatever works.

```
alias KeyHandler = bool delegate(GLFWwindow, int, int, int, int);
void registerKeyHandler(KeyHandler handler) { _keyHandlers ~= handler; }

private:
KeyHandler[] _keyHandlers;

extern(C) void key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier) {
    foreach(handler; _keyHandlers) {
if(_keyHandler(window, key, scancode, action, modifier)) break;
    }
}
```

Reply via email to