3 * Program to invoke all the callbacks that "freeglut" supports
7 #include <GL/freeglut.h>
12 static int sequence_number = 0 ;
14 int reshape_called = 0, key_called = 0, special_called = 0, visibility_called = 0,
15 keyup_called = 0, specialup_called = 0, joystick_called = 0, mouse_called = 0,
16 mousewheel_called = 0, motion_called = 0, passivemotion_called = 0, entry_called = 0,
17 close_called = 0, overlaydisplay_called = 0, windowstatus_called = 0,
18 spacemotion_called = 0, spacerotation_called = 0, spacebutton_called = 0,
19 buttonbox_called = 0, dials_called = 0, tabletmotion_called = 0, tabletbutton_called = 0,
20 menudestroy_called = 0, menustatus_called = 0 ;
21 int reshape_width = -1, reshape_height = -1, reshape_seq = -1 ;
22 int key_key = -1, key_x = -1, key_y = -1, key_seq = -1 ;
23 int special_key = -1, special_x = -1, special_y = -1, special_seq = -1 ;
24 int visibility_vis = -1, visibility_seq = -1 ;
25 int keyup_key = -1, keyup_x = -1, keyup_y = -1, keyup_seq = -1 ;
26 int specialup_key = -1, specialup_x = -1, specialup_y = -1, specialup_seq = -1 ;
27 int joystick_a = -1, joystick_b = -1, joystick_c = -1, joystick_d = -1, joystick_seq = -1 ; /* Need meaningful names */
28 int mouse_button = -1, mouse_updown = -1, mouse_x = -1, mouse_y = -1, mouse_seq = -1 ;
29 int mousewheel_number = -1, mousewheel_direction = -1, mousewheel_x = -1, mousewheel_y = -1, mousewheel_seq = -1 ;
30 int motion_x = -1, motion_y = -1, motion_seq = -1 ;
31 int passivemotion_x = -1, passivemotion_y = -1, passivemotion_seq = -1 ;
34 bitmapPrintf (const char *fmt, ...)
40 #if defined(WIN32) && !defined(__CYGWIN__)
41 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
43 (void) vsnprintf (buf, sizeof(buf), fmt, args);
46 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
53 int window = glutGetWindow () ;
54 glClear ( GL_COLOR_BUFFER_BIT );
56 glDisable ( GL_DEPTH_TEST );
57 glMatrixMode ( GL_PROJECTION );
60 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
61 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
62 glMatrixMode ( GL_MODELVIEW );
65 glColor3ub ( 0, 0, 0 );
66 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
70 bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height );
75 bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
80 bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
83 if ( visibility_called )
85 bitmapPrintf ( "Visibility %d: %d\n", visibility_seq, visibility_vis );
90 bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
93 if ( specialup_called )
95 bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
98 if ( joystick_called )
100 bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
105 bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
108 if ( mousewheel_called )
110 bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
115 bitmapPrintf ( "Motion %d: %d %d\n", motion_seq, motion_x, motion_y );
118 if ( passivemotion_called )
120 bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
123 glMatrixMode ( GL_PROJECTION );
125 glMatrixMode ( GL_MODELVIEW );
127 glEnable ( GL_DEPTH_TEST );
129 printf ( "%6d Window %d Display Callback\n",
130 ++sequence_number, window ) ;
135 Reshape(int width, int height)
137 int window = glutGetWindow () ;
138 printf ( "%6d Window %d Reshape Callback: %d %d\n",
139 ++sequence_number, window, width, height ) ;
141 reshape_width = width ;
142 reshape_height = height ;
143 reshape_seq = sequence_number ;
144 glutPostRedisplay () ;
148 Key(unsigned char key, int x, int y)
150 int window = glutGetWindow () ;
151 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
152 ++sequence_number, window, key, x, y ) ;
157 key_seq = sequence_number ;
158 glutPostRedisplay () ;
162 Special(int key, int x, int y)
164 int window = glutGetWindow () ;
165 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
166 ++sequence_number, window, key, x, y ) ;
171 special_seq = sequence_number ;
172 glutPostRedisplay () ;
178 int window = glutGetWindow () ;
179 printf ( "%6d Window %d Visibility Callback: %d\n",
180 ++sequence_number, window, vis ) ;
181 visibility_called = 1 ;
182 visibility_vis = vis ;
183 visibility_seq = sequence_number ;
184 glutPostRedisplay () ;
188 KeyUp(unsigned char key, int x, int y)
190 int window = glutGetWindow () ;
191 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
192 ++sequence_number, window, key, x, y ) ;
197 keyup_seq = sequence_number ;
198 glutPostRedisplay () ;
202 SpecialUp(int key, int x, int y)
204 int window = glutGetWindow () ;
205 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
206 ++sequence_number, window, key, x, y ) ;
207 specialup_called = 1 ;
208 specialup_key = key ;
211 specialup_seq = sequence_number ;
212 glutPostRedisplay () ;
216 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
218 int window = glutGetWindow () ;
219 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
220 ++sequence_number, window, a, b, c, d ) ;
221 joystick_called = 1 ;
226 joystick_seq = sequence_number ;
227 glutPostRedisplay () ;
231 Mouse(int button, int updown, int x, int y)
233 int window = glutGetWindow () ;
234 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
235 ++sequence_number, window, button, updown, x, y ) ;
237 mouse_button = button ;
238 mouse_updown = updown ;
241 mouse_seq = sequence_number ;
242 glutPostRedisplay () ;
246 MouseWheel(int wheel_number, int direction, int x, int y)
248 int window = glutGetWindow () ;
249 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
250 ++sequence_number, window, wheel_number, direction, x, y ) ;
251 mousewheel_called = 1 ;
252 mousewheel_number = wheel_number ;
253 mousewheel_direction = direction ;
256 mousewheel_seq = sequence_number ;
257 glutPostRedisplay () ;
263 int window = glutGetWindow () ;
264 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
265 ++sequence_number, window, x, y ) ;
269 motion_seq = sequence_number ;
270 glutPostRedisplay () ;
274 PassiveMotion(int x, int y)
276 int window = glutGetWindow () ;
277 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
278 ++sequence_number, window, x, y ) ;
279 passivemotion_called = 1 ;
280 passivemotion_x = x ;
281 passivemotion_y = y ;
282 passivemotion_seq = sequence_number ;
283 glutPostRedisplay () ;
289 int window = glutGetWindow () ;
291 printf ( "%6d Window %d Entry Callback: %d\n",
292 ++sequence_number, window, state ) ;
293 glutPostRedisplay () ;
299 int window = glutGetWindow () ;
301 printf ( "%6d Window %d Close Callback\n",
302 ++sequence_number, window ) ;
308 int window = glutGetWindow () ;
309 overlaydisplay_called = 1 ;
310 printf ( "%6d Window %d OverlayDisplay Callback\n",
311 ++sequence_number, window ) ;
312 glutPostRedisplay () ;
316 WindowStatus(int state)
318 int window = glutGetWindow () ;
319 windowstatus_called = 1 ;
320 printf ( "%6d Window %d WindowStatus Callback: %d\n",
321 ++sequence_number, window, state ) ;
322 glutPostRedisplay () ;
326 SpaceMotion(int x, int y, int z)
328 int window = glutGetWindow () ;
329 spacemotion_called = 1 ;
330 printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n",
331 ++sequence_number, window, x, y, z ) ;
332 glutPostRedisplay () ;
336 SpaceRotation(int x, int y, int z)
338 int window = glutGetWindow () ;
339 spacerotation_called = 1 ;
340 printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n",
341 ++sequence_number, window, x, y, z ) ;
342 glutPostRedisplay () ;
346 SpaceButton(int button, int updown)
348 int window = glutGetWindow () ;
349 spacebutton_called = 1 ;
350 printf ( "%6d Window %d SpaceButton Callback: %d %d\n",
351 ++sequence_number, window, button, updown ) ;
352 glutPostRedisplay () ;
358 int window = glutGetWindow () ;
360 printf ( "%6d Window %d Dials Callback: %d %d\n",
361 ++sequence_number, window, x, y ) ;
362 glutPostRedisplay () ;
366 ButtonBox(int button, int updown)
368 int window = glutGetWindow () ;
369 buttonbox_called = 1 ;
370 printf ( "%6d Window %d ButtonBox Callback: %d %d\n",
371 ++sequence_number, window, button, updown ) ;
372 glutPostRedisplay () ;
376 TabletMotion(int x, int y)
378 int window = glutGetWindow () ;
379 tabletmotion_called = 1 ;
380 printf ( "%6d Window %d TabletMotion Callback: %d %d\n",
381 ++sequence_number, window, x, y ) ;
382 glutPostRedisplay () ;
386 TabletButton(int button, int updown, int x, int y)
388 int window = glutGetWindow () ;
389 tabletbutton_called = 1 ;
390 printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n",
391 ++sequence_number, window, button, updown, x, y ) ;
392 glutPostRedisplay () ;
396 MenuCallback ( int menuID )
398 int window = glutGetWindow () ;
399 printf( "%6d Window %d MenuCallback - menuID is %d\n",
400 ++sequence_number, window, menuID );
406 int window = glutGetWindow () ;
407 menudestroy_called = 1 ;
408 printf ( "%6d Window %d MenuDestroy Callback\n",
409 ++sequence_number, window ) ;
410 glutPostRedisplay () ;
414 MenuStatus( int status, int x, int y )
416 int window = glutGetWindow () ;
417 menudestroy_called = 1 ;
418 printf ( "%6d Window %d MenuStatus Callback: %d %d %d\n",
419 ++sequence_number, window, status, x, y ) ;
420 glutPostRedisplay () ;
423 static void Idle ( void )
429 main(int argc, char *argv[])
431 #define STRING_LENGTH 10
432 int freeglut_window, aux_window ;
433 char dummy_string[STRING_LENGTH];
435 int menuID, subMenuA, subMenuB;
437 glutInitWindowSize(500, 250);
438 glutInitWindowPosition ( 140, 140 );
439 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
440 glutInit(&argc, argv);
442 freeglut_window = glutCreateWindow( "Callback Demo" );
443 printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
445 glClearColor(1.0, 1.0, 1.0, 1.0);
447 glutDisplayFunc( Display );
448 glutReshapeFunc( Reshape );
449 glutKeyboardFunc( Key );
450 glutSpecialFunc( Special );
451 glutVisibilityFunc( Visibility );
452 glutKeyboardUpFunc( KeyUp );
453 glutSpecialUpFunc( SpecialUp );
454 glutJoystickFunc( Joystick, 100 );
455 glutMouseFunc ( Mouse ) ;
456 glutMouseWheelFunc ( MouseWheel ) ;
457 glutMotionFunc ( Motion ) ;
458 glutPassiveMotionFunc ( PassiveMotion ) ;
459 glutEntryFunc ( Entry ) ;
460 glutCloseFunc ( Close ) ;
461 glutOverlayDisplayFunc ( OverlayDisplay ) ;
462 glutWindowStatusFunc ( WindowStatus ) ;
463 glutSpaceballMotionFunc ( SpaceMotion ) ;
464 glutSpaceballRotateFunc ( SpaceRotation ) ;
465 glutSpaceballButtonFunc ( SpaceButton ) ;
466 glutButtonBoxFunc ( ButtonBox ) ;
467 glutDialsFunc ( Dials ) ;
468 glutTabletMotionFunc ( TabletMotion ) ;
469 glutTabletButtonFunc ( TabletButton ) ;
470 glutMenuStatusFunc ( MenuStatus );
471 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
473 subMenuA = glutCreateMenu( MenuCallback );
474 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
475 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
476 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
477 glutMenuDestroyFunc ( MenuDestroy );
479 subMenuB = glutCreateMenu( MenuCallback );
480 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
481 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
482 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
483 glutAddSubMenu( "Going to sub menu A", subMenuA );
484 glutMenuDestroyFunc ( MenuDestroy );
486 menuID = glutCreateMenu( MenuCallback );
487 glutAddMenuEntry( "Entry one", 1 );
488 glutAddMenuEntry( "Entry two", 2 );
489 glutAddMenuEntry( "Entry three", 3 );
490 glutAddMenuEntry( "Entry four", 4 );
491 glutAddMenuEntry( "Entry five", 5 );
492 glutAddSubMenu( "Enter sub menu A", subMenuA );
493 glutAddSubMenu( "Enter sub menu B", subMenuB );
494 glutMenuDestroyFunc ( MenuDestroy );
496 glutAttachMenu( GLUT_LEFT_BUTTON );
498 aux_window = glutCreateWindow( "Second Window" );
499 printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
501 glClearColor(1.0, 1.0, 1.0, 1.0);
503 glutDisplayFunc( Display );
504 glutReshapeFunc( Reshape );
505 glutKeyboardFunc( Key );
506 glutSpecialFunc( Special );
507 glutVisibilityFunc( Visibility );
508 glutKeyboardUpFunc( KeyUp );
509 glutSpecialUpFunc( SpecialUp );
510 /* glutJoystickFunc( Joystick, 100 ); */
511 glutMouseFunc ( Mouse ) ;
512 glutMouseWheelFunc ( MouseWheel ) ;
513 glutMotionFunc ( Motion ) ;
514 glutPassiveMotionFunc ( PassiveMotion ) ;
515 glutEntryFunc ( Entry ) ;
516 glutCloseFunc ( Close ) ;
517 glutOverlayDisplayFunc ( OverlayDisplay ) ;
518 glutWindowStatusFunc ( WindowStatus ) ;
519 glutSpaceballMotionFunc ( SpaceMotion ) ;
520 glutSpaceballRotateFunc ( SpaceRotation ) ;
521 glutSpaceballButtonFunc ( SpaceButton ) ;
522 glutButtonBoxFunc ( ButtonBox ) ;
523 glutDialsFunc ( Dials ) ;
524 glutTabletMotionFunc ( TabletMotion ) ;
525 glutTabletButtonFunc ( TabletButton ) ;
526 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
528 glutIdleFunc ( Idle );
530 printf ( "Please enter something to continue: " );
531 fgets ( dummy_string, STRING_LENGTH, stdin );
535 printf ( "Back from the 'freeglut' main loop\n" ) ;
537 return 0; /* ANSI C requires main to return int. */