PDA

View Full Version : callback function



guyinhell
12-14-2004, 07:09 AM
hi all:
i am really new to this field. just started to learn OpenGL. but i have a small question. who can explain me what is callback function? why we need that?
thanx a lot
alex

Obli
12-14-2004, 01:01 PM
Welcome alex!

Originally posted by guyinhell:
what is callback function?It's nothing more than a normal function in itself but some APIs (FMOD comes to mind, also used by some GUI-related stuff and the whole win32 messaging system) need a pointer to this func.
So, you have a func and a pointer to it. You pass this to the API and it will automatically jump to it when needed.
Example: win32 calls the message proc func whenever a message needs to be processed.
This is more 'general programming' than GL.

Originally posted by guyinhell:
why we need that?In fact, we don't strictly need it. Some people like to just call a func, check the return value and see what it means.
Example: win32 messaging is callback while X uses a function call which is parsed using a switch. They do the same thing however.

guyinhell
12-14-2004, 11:02 PM
hi obli:
thanx a lot :D

mikael_aronsson
12-15-2004, 01:21 AM
Hi !

In the OpenGL case which I assume you are talking about (glu actually) callbacks are used to modify the behavior of some function, for example when you tesselate a polygon with glu it will call glVertex... and so on by default, but you can give it your own "callback" functions that glu will use instead, so when a polygon is tesselated it will not call glVertex... it will instead call a function defined by you.

Mikael

dorbie
12-15-2004, 10:55 PM
My turn :-)

Callbacks are functions you supply as function pointers that are called by a library you are using when you don't have direct control of the program's flow control.

For example a GUI you build using a library might have a callback function on a button that is called when you press that button. You write the function that is to be called on the button press and pass it to the library and when the button is pressed the library calls your function.

Another example would be something like glut where the application's main loop is controlled by glut. You write your graphics code in a glut draw callback function and pass it to glut using the call glutDisplayFunc, and let glut take over flow control by calling glutMainLoop, each frame it is called and your draw code is invoked.

Other examples of callbacks as it relates to graphics are window resize callbacks, update callbacks that just execute code and input callbacks that handle keyboard events. Sometimes callbacks take arguments and sometimes they don't, you have to declare your function to match what the callback mechanism expects.