8.2 Graphics events 275
To control the animation frequency, we use theglutTimerFunccallback.
The following code contained in the filebounce.ccanimates a bouncing disk:
#include <iostream>
#include <cmath>
#include <freeglut.h>
using namespace std;
void animate(int);
void disk();
void quit(unsigned char, int, int);
//--- Global variables:
int delay=10, N=64;
float Dtheta = 2*3.1415926/N;
float centerx = 0.0, centery = 0.0, radius = 0.1;
float Dx = 0.02, Dy=0.01;
int win;
//---------- main
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUTDEPTH|GLUTDOUBLE | GLUTRGBA);
glutInitWindowPosition(500,100);
glutInitWindowSize(320,320);
win = glutCreateWindow("Bouncing disk");
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glClearColor(0.5,0.0,0.8,1.0);
glutKeyboardFunc(quit);
glutTimerFunc(delay, animate, 0);
glutMainLoop();
return 0;
}
//---------- animate
void animate(int code)
{
disk();
glutTimerFunc(delay, animate, 0);
glutSwapBuffers();
}
//-------- disk