glutTimerFunc() not limiting framerate

1k views Asked by At

glutTimerFunc isnt making a delay it just loops forever. Like fxp. while(1). Did I something wrong? Or is it a compatibility issue? I am using arch linux x64 with gcc. And I've been kinda mixing 32 bit programs with 64 bit ones.
I am trying to make a program that checks for input whilst updating frames constantly under a delay
My includes are:

#include <GL/glut.h>
#include <GL/glu.h>
#include <stdio.h>
#include <string.h>

And my main functions are:

void timer(void)
{
    glutPostRedisplay();
    glutTimerFunc ( 30 , mainloop , 0 );
}

int main() {
    loadconfiguration();
    char *myargv [1];
    int myargc=1;
    myargv [0]=strdup ("./file");
    glutInit(&myargc, myargv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(displayx, displayy);
    printf("Making a window\n");
    winIDMain = glutCreateWindow("GL Game");
    mainloop();
}
void mainloop(void){

    Initilize();
    glutSetWindow (winIDMain);
    glutDisplayFunc (render);
    glutReshapeFunc (reshape);
    glutKeyboardFunc (keyboard);
    glutMouseFunc (mouse);
    glutIdleFunc (timer);
    glutMainLoop();
}

Don't worry other functions are clean :)
The code worked earlier I don't know why it doesn't work now.

1

There are 1 answers

0
jozxyqk On BEST ANSWER

Your mainloop should be called init. All it does is set glut callbacks. Rather than call glutPostRedisplay in the idle function, you should call it in a timer function. In other words, don't call glutIdleFunc(timer);. Instead, call timer() once yourself and have it add a timer to itself glutTimerFunc (30 , timer, 0);.

However, I would recommend doing the timing for a frame limiter yourself as it will be much more accurate. I wrote this answer for exactly that.