callback function plz simplify

Hi,

I am having some "stuckness" on the idea of a function, of the "callback type", as it differs from a "normal" function. I was kicking it around and it stopped making sense to me. From what I have read it is a function called by another function. But how is this different from just a vanilla function? Don't they BOTH 'callback' to some degree?

E.g. glutDisplayFunc(drawScene);

Where is the "callback"part occuring and why is it called a callback?

Thx way in advance
drawscene is a function that YOU write, and the library function glutDisplayFunc will call it after doing some other stuff for you.

it is unlike this

void foo()
{
otherfunc(); //this is known at compile time .. it could be inline optimized, for one example.
}

in the call back, the library was already compiled (you downloaded and are using a .lib file or a .dll file somewhere or the unix equivalent) and is calling your function which it did not know anything about back when it was compiled (since you had not written it yet).

also you can call this one with many functions:
glutDisplayFunc(foo);
glutDisplayFunc (bar);

so it is NOT the same as a normal function calling a function.
It is closest to c's function pointer concept, and C++ uses something like it quietly from time to time (threads, sort comparison functions, etc use this concept).

I won't go into the callback name, you can google it. I don't think it fits well, but the name stuck.




Last edited on
Here is a simple setup (commentary after it):

...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
void changeSize(int w, int h) {

	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if (h == 0)
		h = 1;

	float ratio =  w * 1.0 / h;

	// Use the Projection Matrix
	glMatrixMode(GL_PROJECTION);

	// Reset Matrix
	glLoadIdentity();

	// Set the viewport to be the entire window
	glViewport(0, 0, w, h);

	// Set the correct perspective.
	gluPerspective(45,ratio,1,100);

	// Get Back to the Modelview
	glMatrixMode(GL_MODELVIEW);
}

void renderScene(void) {

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glBegin(GL_TRIANGLES);
		glVertex3f(-2,-2,-5.0);
		glVertex3f(2,0.0,-5.0);
		glVertex3f(0.0,2,-5.0);
	glEnd();

	glutSwapBuffers();
}

int main(int argc, char **argv) {

	// init GLUT and create window
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("Lighthouse3D - GLUT Tutorial");

	// register callbacks
	glutDisplayFunc(renderScene);
	glutReshapeFunc(changeSize);

	// enter GLUT event processing loop
	glutMainLoop();

	return 1;
}


Thx. Ok my question regards:
 
glutReshapeFunc(changeSize);

So when the window is changed it "calls" the function changeSize. This is ideal b/c the computer can go about doing other things while leaving this function call resting on a trigger which in this case is the change in window dimensions.

Then as far as pursuing nomenclature, in this case, what is being called "back". It presumes its called already in the first place. Yet, its waiting to be triggered.
that looks like you understand it just fine.

Look, here is the wiki/web define of a callback:
---------
In computer programming, a callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time.

--------

Don't get a hang-up over the word call or the word back or any implied meaning. It is kind of like vectors (which are NOT vectors, in terms of physics or math) which have a push-back (arbitrary define of back, eg if you used a 'vector' as a 'stack' it might make more sense that the back is really 'top' ... but you are forced to use push-back, not push-top or simply 'push'). Sometimes, you just have to go with the words the person who coded something chose to use and move on :)

It is kind of like vectors (which are NOT vectors, in terms of physics or math) which have a push-back (arbitrary define of back, eg if you used a 'vector' as a 'stack' it might make more sense that the back is really 'top' ... but you are forced to use push-back, not push-top or simply 'push').


Isn't that the truth. Little bit of it here.

Well thx that really helped me out. I'll try to distance myself from the trees and into the forest with the nomenclature : ). I feel I've got the concept down much better.
Topic archived. No new replies allowed.