8.2 Graphics events 269
void quit(unsigned char key, int x, int y)
{
cout << "Key: " << key << " pressed" << endl;
cout << "Cursor position: " << x <<""<<y<<endl;
if(key == ’q’)
{
glutDestroyWindow(win);
exit(0);
}
}
When a key is pressed, an event is registered and the functionquitis executed.
When the “q” key is pressed, the program stops asGlutdestroys the window.
Idle functions
What happens between events? We can have other tasks executed in the
meanwhile. Consider the following main code:
#include <iostream>
#include <freeglut.h>
using namespace std;
void keyboard(unsigned char, int, int);
void mouse(int, int, int, int);
void add(void);
void showme(void);
int a=1; // global variable
//---- main:
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUTDEPTH | GLUTSINGLE | GLUTRGBA);
glutCreateWindow("Prime");
glutDisplayFunc(showme);
glutIdleFunc(add);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
When the window is selected and a key pressed, the functionkeyboardis exe-
cuted; when the mouse is clicked, the functionmouseis executed; otherwise the
functionaddkeeps running. The implementation of these functions may be: