This is a poor manβs decision:
FPS = 60.0; while (game_loop) { int t = getticks(); if ((t - t_prev) > 1000/FPS) process_animation_tick(); t_prev = t; }
this is the best solution:
GAME_SPEED = ... while (game_loop) { int t = getticks(); process_animation((t - t_prev)*GAME_SPEED/1000.0); t_prev = t; }
In the first, getframe moves your object by a fixed amount, but this is error prone if the frame rate drops.
In the latter case, you move objects depending on the elapsed time. For example. if the gap is 20 ms, you rotate the object by 12 degrees, and if 10 ms pass, you rotate it by 6 degrees. In general, animation if the function of time has passed.
The implementation of getticks() up to you. For starters, you can use glutGet(GLUT_ELAPSED_TIME) .
In your case, it will look something like this:
int old_t; void idle(void) { int t = glutGet(GLUT_ELAPSED_TIME); int passed = t - old_t; old_t = t; animate( passed ); glutPostRedisplay(); } void animate( int ms ) { if (!wantPause){ circleSpin = circleSpin + ms*0.01;
Kornel kisielewicz
source share