Programming and Graphics

(Kiana) #1

8.2 Graphics events 277


#include <iostream>
#include <cmath>
#include <freeglut.h>

using namespace std;

void pacman(void);
void resize(int, int);
void quit(unsigned char, int, int);
int win;
const float pi = 3.14159265358;
int N=64; // for Pac-Man
float Dtheta = 1.5*pi/N; // for Pac-Man

/*---------- main---------------------*/

int main(int argc, char **argv)
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUTDEPTH | GLUTDOUBLE | GLUTRGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
win=glutCreateWindow("GLUT shapes");
glOrtho(-1.2, 1.2, -1.2, 1.2, -1.2, 1.2);
glutDisplayFunc(pacman);
glutIdleFunc(pacman);
glutReshapeFunc(resize);
glutKeyboardFunc(quit);
glutMainLoop();
return 0;
}

/*---------- Pac-Man ---------------------*/

void pacman()
{
glClear(GLCOLORBUFFERBIT);
glBegin(GLTRIANGLEFAN);
glColor3f(0.1,0.9,0.0);
glVertex2f(0,0);
for (int i=0; i <= N; i++)
{
float angle = i*Dtheta+0.5*pi;
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
Free download pdf