Skip to main content
gamedev.net
🔒 Locked

Function pointers in a GUI?

Started by qpwoeiruty 6 replies 1.7k views
apatriarca
apatriarca
Function pointers are the right solution.
The declaration of a function pointer look like this:
void (*funct)()

You can assign it using the sintax:
funct = f;
where f is a function that returns void and that doesn't have any parameter.

You can also use a typedef:
typedef void (*my_funct)();

and declare the pointer:
my_funct funct_pointer;
Zakwayda
Zakwayda
Yes, function pointers are exactly what you're looking for. If you Google 'C function pointers' and look through some of the references that come up, it should become clear fairly quickly how exactly function pointers can (and should) be used in this particular case. In fact, the Quake GUI system(s) use function pointers in exactly this way (other games probably do as well, but Quake comes to mind because the Q3 GUI system, at least, was written in C).

Having gotten that out of the way, I feel obliged to mention that were you to switch to C++, you would have much more flexible tools at your disposal than the aforementioned function pointers (of course your choice of language is not limited to these two - there are other languages you could use as well, most of which are considerably more user-friendly than either C or C++).
JohnHurt
JohnHurt
I use functors in my GUI system (alongside a message handling system too). Functors are C++ classes which behave like functions:

class cFunctor{public:    void operator()(/* ... parameter list ... */)    {        /* function body */    };};


You can pass these classes around like any other class, declare them like a class, and them call them like a function:

cFunctor myfunc; // declare
myFunc(); // call
Zakwayda
Zakwayda
Quote:
Original post by Kada2k6
By the way John Hurt, I didn't know you were into programming! Did it get boring making movies? :-)
Doesn't look like it - according to IMDB, he currently has nine movies in production! :-)

Anyway, back on topic... Since you seem to be committed to the C route, you might find this series of articles to be of interest. The articles offer a fairly in-depth look at the Quake 3 engine's GUI system, including how are where function pointers are used. The articles along with the Q3 source itself should, I think, provide a pretty good reference for GUI design in C (although you might prefer a data-driven approach to Q3's hard-coding of the in-game UI).
Dancin_Fool
Dancin_Fool
Since you're using c, what you could do for the function parameter is pass in a void*.

Then when you want to call the function have a set of struct's you could pass to the function and then cast it to the appropriate type within the function.

Not an elegant solution, definitely not type safe, but it works.
Dancin_Fool
Dancin_Fool
int Function( void *pData ){    tWeapon *pWeapon = (tWeapon*)pData;}


I'll try to post something better later if someone else doesn't clarify, little busy at work right now.
apatriarca
apatriarca
Quote:
tWeapon *pWeapon = (tWeapon*)pData;

In C the cast isn’t required.

typedef struct {int a, b;} int_pair; int sum(void *p){    int_pair *pair = p;    return pair->a + pair->b;}/* ... */int_pair pair = {10, 7};int ret = sum(&pair); 

Topic Locked

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

Sign in to reply to this topic.