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,
16 close_called = 0, overlaydisplay_called = 0, windowstatus_called = 0,
17 spacemotion_called = 0, spacerotation_called = 0, spacebutton_called = 0,
18 buttonbox_called = 0, dials_called = 0, tabletmotion_called = 0, tabletbutton_called = 0,
19 menudestroy_called = 0, menustatus_called = 0 ;
20 int reshape_width = -1, reshape_height = -1, reshape_seq = -1 ;
21 int key_key = -1, key_x = -1, key_y = -1, key_seq = -1 ;
22 int special_key = -1, special_x = -1, special_y = -1, special_seq = -1 ;
23 int visibility_vis = -1, visibility_seq = -1 ;
24 int keyup_key = -1, keyup_x = -1, keyup_y = -1, keyup_seq = -1 ;
25 int specialup_key = -1, specialup_x = -1, specialup_y = -1, specialup_seq = -1 ;
26 int joystick_a = -1, joystick_b = -1, joystick_c = -1, joystick_d = -1, joystick_seq = -1 ; /* Need meaningful names */
27 int mouse_button = -1, mouse_updown = -1, mouse_x = -1, mouse_y = -1, mouse_seq = -1 ;
28 int mousewheel_number = -1, mousewheel_direction = -1, mousewheel_x = -1, mousewheel_y = -1, mousewheel_seq = -1 ;
29 int motion_x = -1, motion_y = -1, motion_seq = -1 ;
30 int passivemotion_x = -1, passivemotion_y = -1, passivemotion_seq = -1 ;
35 int window = glutGetWindow () ;
36 glClear ( GL_COLOR_BUFFER_BIT );
38 glDisable ( GL_DEPTH_TEST );
39 glMatrixMode ( GL_PROJECTION );
42 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
43 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
44 glMatrixMode ( GL_MODELVIEW );
47 glColor3ub ( 0, 0, 0 );
48 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
52 sprintf ( line, "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height );
53 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
58 sprintf ( line, "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
59 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
64 sprintf ( line, "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
65 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
68 if ( visibility_called )
70 sprintf ( line, "Visibility %d: %d\n", visibility_seq, visibility_vis );
71 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
76 sprintf ( line, "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
77 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
80 if ( specialup_called )
82 sprintf ( line, "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
83 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
86 if ( joystick_called )
88 sprintf ( line, "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
89 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
94 sprintf ( line, "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
95 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
98 if ( mousewheel_called )
100 sprintf ( line, "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
101 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
106 sprintf ( line, "Motion %d: %d %d\n", motion_seq, motion_x, motion_y );
107 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
110 if ( passivemotion_called )
112 sprintf ( line, "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
113 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
116 glMatrixMode ( GL_PROJECTION );
118 glMatrixMode ( GL_MODELVIEW );
120 glEnable ( GL_DEPTH_TEST );
122 printf ( "%6d Window %d Display Callback\n",
123 ++sequence_number, window ) ;
128 Reshape(int width, int height)
130 int window = glutGetWindow () ;
131 printf ( "%6d Window %d Reshape Callback: %d %d\n",
132 ++sequence_number, window, width, height ) ;
134 reshape_width = width ;
135 reshape_height = height ;
136 reshape_seq = sequence_number ;
137 glutPostRedisplay () ;
141 Key(unsigned char key, int x, int y)
143 int window = glutGetWindow () ;
144 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
145 ++sequence_number, window, key, x, y ) ;
150 key_seq = sequence_number ;
151 glutPostRedisplay () ;
155 Special(int key, int x, int y)
157 int window = glutGetWindow () ;
158 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
159 ++sequence_number, window, key, x, y ) ;
164 special_seq = sequence_number ;
165 glutPostRedisplay () ;
171 int window = glutGetWindow () ;
172 printf ( "%6d Window %d Visibility Callback: %d\n",
173 ++sequence_number, window, vis ) ;
174 visibility_called = 1 ;
175 visibility_vis = vis ;
176 visibility_seq = sequence_number ;
177 glutPostRedisplay () ;
181 KeyUp(unsigned char key, int x, int y)
183 int window = glutGetWindow () ;
184 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
185 ++sequence_number, window, key, x, y ) ;
190 keyup_seq = sequence_number ;
191 glutPostRedisplay () ;
195 SpecialUp(int key, int x, int y)
197 int window = glutGetWindow () ;
198 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
199 ++sequence_number, window, key, x, y ) ;
200 specialup_called = 1 ;
201 specialup_key = key ;
204 specialup_seq = sequence_number ;
205 glutPostRedisplay () ;
209 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
211 int window = glutGetWindow () ;
212 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
213 ++sequence_number, window, a, b, c, d ) ;
214 joystick_called = 1 ;
219 joystick_seq = sequence_number ;
220 glutPostRedisplay () ;
224 Mouse(int button, int updown, int x, int y)
226 int window = glutGetWindow () ;
227 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
228 ++sequence_number, window, button, updown, x, y ) ;
230 mouse_button = button ;
231 mouse_updown = updown ;
234 mouse_seq = sequence_number ;
235 glutPostRedisplay () ;
239 MouseWheel(int wheel_number, int direction, int x, int y)
241 int window = glutGetWindow () ;
242 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
243 ++sequence_number, window, wheel_number, direction, x, y ) ;
244 mousewheel_called = 1 ;
245 mousewheel_number = wheel_number ;
246 mousewheel_direction = direction ;
249 mousewheel_seq = sequence_number ;
250 glutPostRedisplay () ;
256 int window = glutGetWindow () ;
257 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
258 ++sequence_number, window, x, y ) ;
262 motion_seq = sequence_number ;
263 glutPostRedisplay () ;
267 PassiveMotion(int x, int y)
269 int window = glutGetWindow () ;
270 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
271 ++sequence_number, window, x, y ) ;
272 passivemotion_called = 1 ;
273 passivemotion_x = x ;
274 passivemotion_y = y ;
275 passivemotion_seq = sequence_number ;
276 glutPostRedisplay () ;
282 int window = glutGetWindow () ;
284 printf ( "%6d Window %d Entry Callback: %d\n",
285 ++sequence_number, window, state ) ;
286 glutPostRedisplay () ;
292 int window = glutGetWindow () ;
294 printf ( "%6d Window %d Close Callback\n",
295 ++sequence_number, window ) ;
301 int window = glutGetWindow () ;
302 overlaydisplay_called = 1 ;
303 printf ( "%6d Window %d OverlayDisplay Callback\n",
304 ++sequence_number, window ) ;
305 glutPostRedisplay () ;
309 WindowStatus(int state)
311 int window = glutGetWindow () ;
312 windowstatus_called = 1 ;
313 printf ( "%6d Window %d WindowStatus Callback: %d\n",
314 ++sequence_number, window, state ) ;
315 glutPostRedisplay () ;
319 SpaceMotion(int x, int y, int z)
321 int window = glutGetWindow () ;
322 spacemotion_called = 1 ;
323 printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n",
324 ++sequence_number, window, x, y, z ) ;
325 glutPostRedisplay () ;
329 SpaceRotation(int x, int y, int z)
331 int window = glutGetWindow () ;
332 spacerotation_called = 1 ;
333 printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n",
334 ++sequence_number, window, x, y, z ) ;
335 glutPostRedisplay () ;
339 SpaceButton(int button, int updown)
341 int window = glutGetWindow () ;
342 spacebutton_called = 1 ;
343 printf ( "%6d Window %d SpaceButton Callback: %d %d\n",
344 ++sequence_number, window, button, updown ) ;
345 glutPostRedisplay () ;
351 int window = glutGetWindow () ;
353 printf ( "%6d Window %d Dials Callback: %d %d\n",
354 ++sequence_number, window, x, y ) ;
355 glutPostRedisplay () ;
359 ButtonBox(int button, int updown)
361 int window = glutGetWindow () ;
362 buttonbox_called = 1 ;
363 printf ( "%6d Window %d ButtonBox Callback: %d %d\n",
364 ++sequence_number, window, button, updown ) ;
365 glutPostRedisplay () ;
369 TabletMotion(int x, int y)
371 int window = glutGetWindow () ;
372 tabletmotion_called = 1 ;
373 printf ( "%6d Window %d TabletMotion Callback: %d %d\n",
374 ++sequence_number, window, x, y ) ;
375 glutPostRedisplay () ;
379 TabletButton(int button, int updown, int x, int y)
381 int window = glutGetWindow () ;
382 tabletbutton_called = 1 ;
383 printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n",
384 ++sequence_number, window, button, updown, x, y ) ;
385 glutPostRedisplay () ;
389 MenuCallback ( int menuID )
391 int window = glutGetWindow () ;
392 printf( "%6d Window %d MenuCallback - menuID is %d\n",
393 ++sequence_number, window, menuID );
399 int window = glutGetWindow () ;
400 menudestroy_called = 1 ;
401 printf ( "%6d Window %d MenuDestroy Callback\n",
402 ++sequence_number, window ) ;
403 glutPostRedisplay () ;
407 MenuStatus( int status, int x, int y )
409 int window = glutGetWindow () ;
410 menudestroy_called = 1 ;
411 printf ( "%6d Window %d MenuStatus Callback: %d %d %d\n",
412 ++sequence_number, window, status, x, y ) ;
413 glutPostRedisplay () ;
416 static void Idle ( void )
422 main(int argc, char *argv[])
424 #define STRING_LENGTH 10
425 int freeglut_window, aux_window ;
426 char dummy_string[STRING_LENGTH];
428 int menuID, subMenuA, subMenuB;
430 glutInitWindowSize(500, 250);
431 glutInitWindowPosition ( 140, 140 );
432 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
433 glutInit(&argc, argv);
435 freeglut_window = glutCreateWindow( "Callback Demo" );
436 printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
438 glClearColor(1.0, 1.0, 1.0, 1.0);
440 glutDisplayFunc( Display );
441 glutReshapeFunc( Reshape );
442 glutKeyboardFunc( Key );
443 glutSpecialFunc( Special );
444 glutVisibilityFunc( Visibility );
445 glutKeyboardUpFunc( KeyUp );
446 glutSpecialUpFunc( SpecialUp );
447 glutJoystickFunc( Joystick, 100 );
448 glutMouseFunc ( Mouse ) ;
449 glutMouseWheelFunc ( MouseWheel ) ;
450 glutMotionFunc ( Motion ) ;
451 glutPassiveMotionFunc ( PassiveMotion ) ;
452 glutEntryFunc ( Entry ) ;
453 glutCloseFunc ( Close ) ;
454 glutOverlayDisplayFunc ( OverlayDisplay ) ;
455 glutWindowStatusFunc ( WindowStatus ) ;
456 glutSpaceballMotionFunc ( SpaceMotion ) ;
457 glutSpaceballRotateFunc ( SpaceRotation ) ;
458 glutSpaceballButtonFunc ( SpaceButton ) ;
459 glutButtonBoxFunc ( ButtonBox ) ;
460 glutDialsFunc ( Dials ) ;
461 glutTabletMotionFunc ( TabletMotion ) ;
462 glutTabletButtonFunc ( TabletButton ) ;
463 glutMenuDestroyFunc ( MenuDestroy );
464 glutMenuStatusFunc ( MenuStatus );
465 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
467 subMenuA = glutCreateMenu( MenuCallback );
468 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
469 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
470 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
472 subMenuB = glutCreateMenu( MenuCallback );
473 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
474 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
475 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
476 glutAddSubMenu( "Going to sub menu A", subMenuA );
478 menuID = glutCreateMenu( MenuCallback );
479 glutAddMenuEntry( "Entry one", 1 );
480 glutAddMenuEntry( "Entry two", 2 );
481 glutAddMenuEntry( "Entry three", 3 );
482 glutAddMenuEntry( "Entry four", 4 );
483 glutAddMenuEntry( "Entry five", 5 );
484 glutAddSubMenu( "Enter sub menu A", subMenuA );
485 glutAddSubMenu( "Enter sub menu B", subMenuB );
487 glutAttachMenu( GLUT_LEFT_BUTTON );
489 aux_window = glutCreateWindow( "Second Window" );
490 printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
492 glClearColor(1.0, 1.0, 1.0, 1.0);
494 glutDisplayFunc( Display );
495 glutReshapeFunc( Reshape );
496 glutKeyboardFunc( Key );
497 glutSpecialFunc( Special );
498 glutVisibilityFunc( Visibility );
499 glutKeyboardUpFunc( KeyUp );
500 glutSpecialUpFunc( SpecialUp );
501 /* glutJoystickFunc( Joystick, 100 ); */
502 glutMouseFunc ( Mouse ) ;
503 glutMouseWheelFunc ( MouseWheel ) ;
504 glutMotionFunc ( Motion ) ;
505 glutPassiveMotionFunc ( PassiveMotion ) ;
506 glutEntryFunc ( Entry ) ;
507 glutCloseFunc ( Close ) ;
508 glutOverlayDisplayFunc ( OverlayDisplay ) ;
509 glutWindowStatusFunc ( WindowStatus ) ;
510 glutSpaceballMotionFunc ( SpaceMotion ) ;
511 glutSpaceballRotateFunc ( SpaceRotation ) ;
512 glutSpaceballButtonFunc ( SpaceButton ) ;
513 glutButtonBoxFunc ( ButtonBox ) ;
514 glutDialsFunc ( Dials ) ;
515 glutTabletMotionFunc ( TabletMotion ) ;
516 glutTabletButtonFunc ( TabletButton ) ;
517 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
519 glutIdleFunc ( Idle );
521 printf ( "Please enter something to continue: " );
522 fgets ( dummy_string, STRING_LENGTH, stdin );
526 printf ( "Back from the 'freeglut' main loop\n" ) ;
528 return 0; /* ANSI C requires main to return int. */