0

I'm relatively new to writing input-based applications. I'm using OpenGL and GLFW to write games at the moment and user input is at the heart of it. From what I understand, OpenGL being a C program means that I cannot use classes to encapsulate the existing, predefined keycallback functions.

One way around this, is to use a self-defined callback as a global function or as a static method in a class. Link provides quite a few interesting ways to do this. Some of these are better than what I'm doing right now but my question remains- is it worth such an effort?

void glfwKeyCallBack(some parameters)  // GLFW-defined
{ // does something}

void SelfDefinedFun(some parameters)  // my definition of it
{ // does something}

int main()
{
  while(gamelooptrue) // inside game loop here
  {
    1. SelfDefinedFun(some parameters) // call function here instead of using glfwKeyCallBack
    2. action 2
    3. action 3
    ...
  }

}

Callback functions are automatically called the moment the corresponding action takes place. This means that regardless of how slow my game loop is, each time a key is pressed, glfwkeyCallBack will be called. I can vaguely see some of the advantages such a method provides as opposed to calling SelfDefinedFun repeatedly in the game loop.

However... the game loop requires that the drawing of a scene take place each frame. Given current standards, this is usually 60 times a second but I imagine we would need at least 15fps to have a non-jerky visualization of movement on a screen. Given that, I can forget about the callback and simply check for key-press every frame. The advantage here is that I can easily encapsulate the function and this allows me to perform more complex event-based operations.

So, why are callbacks even necessary if we are drawing the game so many times a second? One reason I think callbacks could be better is because you don't check for user-actions unless there is one => more time for drawing. Since my application is simple, this is negligible time. But would it be different in full-blown 3D games like Skyrim, Bioshock etc.?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • "OpenGL being a C program means that I cannot use classes to encapsulate the existing, predefined keycallback functions." Bo idea what you're talking about, there are no _predifined callback functions_. `glfwKeyCallBack` doesn't exsist in GLFW, and even less so in OpenGL. – derhass Apr 12 '20 at 11:55
  • 1
    "This means that regardless of how slow my game loop is, each time a key is pressed, glfwkeyCallBack will be called. " Also not true. GLFWs event handling just polls for events when you tell it to. – derhass Apr 12 '20 at 11:57
  • Of course, glfwKeyCallBack is still a user defined routine that is based on a function framework defined in GLFW. This means, I cannot add more parameters/return values or let this function be encapsulated in a class. The alternative would be to define a function that is not according to this *prototype*. But that means, that the event polling function cannot call it anymore since it is not according to the expected format. – speed_of_light Apr 12 '20 at 15:34
  • As for the second comment, once I *connect* a function to a specific action, I imagine the polling function automatically calls it when that action occurs. Would there be instances where this connection needs to be broken or redefined? – speed_of_light Apr 12 '20 at 15:36
  • "As for the second comment, once I connect a function to a specific action, I imagine the polling function automatically calls it " Yes, the polling function calls it, but _you_ have to call the polling function. – derhass Apr 12 '20 at 16:05
  • This whole question is based on a false assumption: "So, why are callbacks even necessary if we are drawing the game so many times a second?" Who says they are _necessary_? They aren't. – derhass Apr 12 '20 at 16:09
  • Thanks a lot, that clears a couple of things for me! Most library functions I've come across tend to have advantages in terms of time. Given that, I was hoping there would be some compelling reason to use defined template and polling function. I found a few interesting things about event-based functions that can clarify which option could be more advantageous. Will put a few things together and make the answer. Thanks again! – speed_of_light Apr 12 '20 at 19:03

0 Answers0