Original Post
Can someone explain how to use glutMainLoopEvent() to me? It's surely one of the most useful additions FreeGLUT provides, but I can't seem to use it properly. Am I missing something?
Specifically, why doesn't the following code work:
It only does one iteration rather than looping fully like I'd expect. Clicking on the window (to generate an event) causes a single further iteration to occur for every click (i.e. the triangle moves round a little bit).
Specifying glutIdleFunc(cback_render) doesn't make a difference.
Specifying glutPostRedisplay() in the display callback DOES work, but it thrashes the CPU whereas standard glutMainLoop() + glutIdleFunc() looping doesn't. I've modified the above code with macros to demonstrate the difference.
It's the difference in CPU use that makes me think my solution isn't the best. I'd appreciate anyone's suggestions.
Specifically, why doesn't the following code work:
#include <GL/freeglut.h>//display function - draws a triangle rotating about the originvoid cback_render(){ //keeps track of rotations static float rotations = 0; //OpenGL stuff for triangle glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(rotations, 0, 0, 1); glBegin(GL_TRIANGLES); glVertex3f(0,0,0); glVertex3f(1,0,0); glVertex3f(0,1,0); glEnd(); //display on screen glutSwapBuffers(); //rotate triangle a little bit, wrapping around at 360° if (++rotations > 360) rotations -= 360;}int main(int argc, char **argv){ //initialisations glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowPosition(100, 100); glutInitWindowSize(512, 512); //create window and register display callback glutCreateWindow("freegluttest"); glutDisplayFunc (cback_render); //loop forever while(1) glutMainLoopEvent(); return 0;}It only does one iteration rather than looping fully like I'd expect. Clicking on the window (to generate an event) causes a single further iteration to occur for every click (i.e. the triangle moves round a little bit).
Specifying glutIdleFunc(cback_render) doesn't make a difference.
Specifying glutPostRedisplay() in the display callback DOES work, but it thrashes the CPU whereas standard glutMainLoop() + glutIdleFunc() looping doesn't. I've modified the above code with macros to demonstrate the difference.
It's the difference in CPU use that makes me think my solution isn't the best. I'd appreciate anyone's suggestions.