Skip to main content
gamedev.net
🔒 Locked

how does glut do that?

Started by pavel989 2 replies 650+ views
Original Post
pavel989
pavel989
i forget all the calls, because I've moved away from glut and instead now do all the code by hand. but I never got a chance to understand how glut works. How can a function, take in a function? for example; glutMouseFunc actually does glutMouseFunc(somefun); i found this: void glutMouseFunc(void (*func)(int button, int state, int x, int y)); so does that mean that the first param, is a pointer to a function? I am having a hard time getting this.
CaspianB
CaspianB
You're exactly correct. It's allowed by function pointers. They're by no means exclusive to glut, either.

Take the example you gave:
void glutMouseFunc(void (*func)(int button, int state, int x, int y));

This declares a variable named "func" which is a pointer to a function with the parameters (int, int, int, int);

Here's a small example of how function pointers can be used below.

#include <iostream>using namespace std;void Func1(){   cout << "Func1 called!" << endl;}void Func2(){   cout << "Func2 called!" << endl;}int main(){   void (*functionPtr)();   functionPtr = Func1;   functionPtr();   functionPtr = Func2;   functionPtr();   return 0;}
othello
othello
Be careful; you can't call nonstatic methods of classes in this way.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.