I'm a Java programmer, learning opengl in C for the first time. I wanna dissect this simple code that my instructor gave me without much explanation:
void renderScene (void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0.0);
glEnd();
glFlush();
void init(); {
int submenu;
submenu = glutCreateMenu(menuApp);
glutAddMenuEntry("Option A",1);
glutAddMenuEntry("Option B",2);
glutAddMenuEntry("Option C",3);
glutCreateMenu(menuApp);
glutAddSubMenu("SubMenu",submenu);
glutAddMenuEntry("Salir",4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
}
Ques开发者_开发问答tions:
a) What does void renderScene (void)
means? Why should this function take a void
paramether?
b) What the hell is void init(); {}
? Why both ; and {}? Why is it inside the renderScene
function?
void renderScene (void)
is C's way of saying "declare renderScene
as a function taking no arguments and returning no value". void renderScene()
would be something different - it is an obsolete function declaration style in C, declaring that renderScene
takes a fixed-but-unspecified number of parameters.
The other part is a little odd, and formatted in a misleading way. Formatted correctly, it would read:
void init();
{
int submenu;
submenu = glutCreateMenu(menuApp);
glutAddMenuEntry("Option A",1);
/* ... */
}
The block inside the { }
is unrelated to the void init();
. The void init();
declares a function called init
(it's the obsolete function declaration style mentioned above). A function declaration is just a way of saying to the compiler "there's a function somewhere called this, and this is what its arguments and return value are". Since the init
function is never called, this line could be omitted.
The block inside the { }
is just a compound statement. All it does is open a new scope - the variable submenu
has a scope restricted to that block.
It's also worth pointing out that the declaration void init();
is itself an anachronism - it's a declaration that appears after code in the same block, which is a feature added in the 1999 revision of the C standard, but it's a declaration without a prototype, which as mentioned predates the original 1989 C standard.
Note also that function declarations that aren't at file scope are themselves somewhat unusual, although completely legal.
The function renderScene
having the parameter void
means it does not take any parameters.
The init
function initalizes the program. In other words it starts up and gets ready to do something.
The init function should be defined
void init(){
...
}
instead of
void init();{
...
}
and it SHOULD NOT be within renderScene
.
void renderScene (void)
means the function takes no arguments.
It's similar to void renderScene () in Java.
What the hell is void init(); {}?
Probably a typo, The ; shouldn't be there.
Why is it inside the renderScene function?
Probably another typo, the renderScene should have another }
If the code snippet you posted is correct, it is the definition of the renderScene
function. The function does not return a value and does not accept any parameters.
void init();
is a declaration. It just lets the compiler know the details of the function named init
so that it can generate the correct code when we call it -- but it's an incomplete declaration (not much help to the compiler) and it's not called anywhere the declaration is in scope. So, it's basically just noise.
If we ignore that and continue with the {
, the next lines are a group of statements initiated probably to introduce the submenu
variable, which only lives inside this new block.
精彩评论