3 * Program to invoke all the callbacks that "freeglut" supports
7 #include <GL/freeglut.h>
11 static int sequence_number = 0 ;
13 int reshape_called = 0, key_called = 0, special_called = 0, visibility_called = 0,
14 keyup_called = 0, specialup_called = 0, joystick_called = 0, mouse_called = 0,
15 mousewheel_called = 0, motion_called = 0, passivemotion_called = 0, entry_called = 0,
17 int reshape_width = -1, reshape_height = -1, reshape_seq = -1 ;
18 int key_key = -1, key_x = -1, key_y = -1, key_seq = -1 ;
19 int special_key = -1, special_x = -1, special_y = -1, special_seq = -1 ;
20 int visibility_vis = -1, visibility_seq = -1 ;
21 int keyup_key = -1, keyup_x = -1, keyup_y = -1, keyup_seq = -1 ;
22 int specialup_key = -1, specialup_x = -1, specialup_y = -1, specialup_seq = -1 ;
23 int joystick_a = -1, joystick_b = -1, joystick_c = -1, joystick_d = -1, joystick_seq = -1 ; /* Need meaningful names */
24 int mouse_button = -1, mouse_updown = -1, mouse_x = -1, mouse_y = -1, mouse_seq = -1 ;
25 int mousewheel_number = -1, mousewheel_direction = -1, mousewheel_x = -1, mousewheel_y = -1, mousewheel_seq = -1 ;
26 int motion_x = -1, motion_y = -1, motion_seq = -1 ;
27 int passivemotion_x = -1, passivemotion_y = -1, passivemotion_seq = -1 ;
32 int window = glutGetWindow () ;
33 glClear ( GL_COLOR_BUFFER_BIT );
35 glDisable ( GL_DEPTH_TEST );
36 glMatrixMode ( GL_PROJECTION );
39 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
40 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
41 glMatrixMode ( GL_MODELVIEW );
44 glColor3ub ( 0, 0, 0 );
45 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
49 sprintf ( line, "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height );
50 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
55 sprintf ( line, "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
56 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
61 sprintf ( line, "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
62 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
65 if ( visibility_called )
67 sprintf ( line, "Visibility %d: %d\n", visibility_seq, visibility_vis );
68 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
73 sprintf ( line, "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
74 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
77 if ( specialup_called )
79 sprintf ( line, "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
80 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
83 if ( joystick_called )
85 sprintf ( line, "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
86 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
91 sprintf ( line, "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
92 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
95 if ( mousewheel_called )
97 sprintf ( line, "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
98 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
103 sprintf ( line, "Motion %d: %d %d\n", motion_seq, motion_x, motion_y );
104 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
107 if ( passivemotion_called )
109 sprintf ( line, "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
110 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, line );
113 glMatrixMode ( GL_PROJECTION );
115 glMatrixMode ( GL_MODELVIEW );
117 glEnable ( GL_DEPTH_TEST );
119 printf ( "%6d Window %d Display Callback\n",
120 ++sequence_number, window ) ;
125 Reshape(int width, int height)
127 int window = glutGetWindow () ;
128 printf ( "%6d Window %d Reshape Callback: %d %d\n",
129 ++sequence_number, window, width, height ) ;
131 reshape_width = width ;
132 reshape_height = height ;
133 reshape_seq = sequence_number ;
134 glutPostRedisplay () ;
138 Key(unsigned char key, int x, int y)
140 int window = glutGetWindow () ;
141 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
142 ++sequence_number, window, key, x, y ) ;
147 key_seq = sequence_number ;
148 glutPostRedisplay () ;
152 Special(int key, int x, int y)
154 int window = glutGetWindow () ;
155 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
156 ++sequence_number, window, key, x, y ) ;
161 special_seq = sequence_number ;
162 glutPostRedisplay () ;
168 int window = glutGetWindow () ;
169 printf ( "%6d Window %d Visibility Callback: %d\n",
170 ++sequence_number, window, vis ) ;
171 visibility_called = 1 ;
172 visibility_vis = vis ;
173 visibility_seq = sequence_number ;
174 glutPostRedisplay () ;
178 KeyUp(unsigned char key, int x, int y)
180 int window = glutGetWindow () ;
181 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
182 ++sequence_number, window, key, x, y ) ;
187 keyup_seq = sequence_number ;
188 glutPostRedisplay () ;
192 SpecialUp(int key, int x, int y)
194 int window = glutGetWindow () ;
195 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
196 ++sequence_number, window, key, x, y ) ;
197 specialup_called = 1 ;
198 specialup_key = key ;
201 specialup_seq = sequence_number ;
202 glutPostRedisplay () ;
206 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
208 int window = glutGetWindow () ;
209 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
210 ++sequence_number, window, a, b, c, d ) ;
211 joystick_called = 1 ;
216 joystick_seq = sequence_number ;
217 glutPostRedisplay () ;
221 Mouse(int button, int updown, int x, int y)
223 int window = glutGetWindow () ;
224 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
225 ++sequence_number, window, button, updown, x, y ) ;
227 mouse_button = button ;
228 mouse_updown = updown ;
231 mouse_seq = sequence_number ;
232 glutPostRedisplay () ;
236 MouseWheel(int wheel_number, int direction, int x, int y)
238 int window = glutGetWindow () ;
239 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
240 ++sequence_number, window, wheel_number, direction, x, y ) ;
241 mousewheel_called = 1 ;
242 mousewheel_number = wheel_number ;
243 mousewheel_direction = direction ;
246 mousewheel_seq = sequence_number ;
247 glutPostRedisplay () ;
253 int window = glutGetWindow () ;
254 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
255 ++sequence_number, window, x, y ) ;
259 motion_seq = sequence_number ;
260 glutPostRedisplay () ;
264 PassiveMotion(int x, int y)
266 int window = glutGetWindow () ;
267 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
268 ++sequence_number, window, x, y ) ;
269 passivemotion_called = 1 ;
270 passivemotion_x = x ;
271 passivemotion_y = y ;
272 passivemotion_seq = sequence_number ;
273 glutPostRedisplay () ;
279 int window = glutGetWindow () ;
281 printf ( "%6d Window %d Entry Callback: %d\n",
282 ++sequence_number, window, state ) ;
283 glutPostRedisplay () ;
289 int window = glutGetWindow () ;
291 printf ( "%6d Window %d Close Callback\n",
292 ++sequence_number, window ) ;
298 main(int argc, char *argv[])
300 int freeglut_window, aux_window ;
302 glutInitWindowSize(500, 250);
303 glutInitWindowPosition ( 140, 140 );
304 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
305 glutInit(&argc, argv);
307 freeglut_window = glutCreateWindow( "Callback Demo" );
308 printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
310 glClearColor(1.0, 1.0, 1.0, 1.0);
312 glutDisplayFunc( Display );
313 glutReshapeFunc( Reshape );
314 glutKeyboardFunc( Key );
315 glutSpecialFunc( Special );
316 glutVisibilityFunc( Visibility );
317 glutKeyboardUpFunc( KeyUp );
318 glutSpecialUpFunc( SpecialUp );
319 glutJoystickFunc( Joystick, 100 );
320 glutMouseFunc ( Mouse ) ;
321 glutMouseWheelFunc ( MouseWheel ) ;
322 glutMotionFunc ( Motion ) ;
323 glutPassiveMotionFunc ( PassiveMotion ) ;
324 glutEntryFunc ( Entry ) ;
325 glutCloseFunc ( Close ) ;
327 aux_window = glutCreateWindow( "Second Window" );
328 printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
330 glClearColor(1.0, 1.0, 1.0, 1.0);
332 glutDisplayFunc( Display );
333 glutReshapeFunc( Reshape );
334 glutKeyboardFunc( Key );
335 glutSpecialFunc( Special );
336 glutVisibilityFunc( Visibility );
337 glutKeyboardUpFunc( KeyUp );
338 glutSpecialUpFunc( SpecialUp );
339 // glutJoystickFunc( Joystick, 100 );
340 glutMouseFunc ( Mouse ) ;
341 glutMouseWheelFunc ( MouseWheel ) ;
342 glutMotionFunc ( Motion ) ;
343 glutPassiveMotionFunc ( PassiveMotion ) ;
344 glutEntryFunc ( Entry ) ;
345 glutCloseFunc ( Close ) ;
349 printf ( "Back from the 'freeglut' main loop\n" ) ;
351 return 0; /* ANSI C requires main to return int. */