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 ;
33 #define STRING_LENGTH 10
36 bitmapPrintf (const char *fmt, ...)
42 #if defined(WIN32) && !defined(__CYGWIN__)
43 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
45 (void) vsnprintf (buf, sizeof(buf), fmt, args);
48 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
55 int window = glutGetWindow () ;
56 glClear ( GL_COLOR_BUFFER_BIT );
58 glDisable ( GL_DEPTH_TEST );
59 glMatrixMode ( GL_PROJECTION );
62 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
63 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
64 glMatrixMode ( GL_MODELVIEW );
67 glColor3ub ( 0, 0, 0 );
68 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
72 bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height );
77 bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
82 bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
85 if ( visibility_called )
87 bitmapPrintf ( "Visibility %d: %d\n", visibility_seq, visibility_vis );
92 bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
95 if ( specialup_called )
97 bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
100 if ( joystick_called )
102 bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
107 bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
110 if ( mousewheel_called )
112 bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
117 bitmapPrintf ( "Motion %d: %d %d\n", motion_seq, motion_x, motion_y );
120 if ( passivemotion_called )
122 bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
125 glMatrixMode ( GL_PROJECTION );
127 glMatrixMode ( GL_MODELVIEW );
129 glEnable ( GL_DEPTH_TEST );
131 printf ( "%6d Window %d Display Callback\n",
132 ++sequence_number, window ) ;
137 Warning(const char *fmt, va_list ap)
139 printf("%6d Warning callback:\n");
141 /* print warning message */
146 Error(const char *fmt, va_list ap)
148 char dummy_string[STRING_LENGTH];
149 printf("%6d Error callback:\n");
151 /* print warning message */
154 /* deInitialize the freeglut state */
155 /* This all goes haywire of course when the error occurs during deinitialize,
156 * one might want to call exit directly as its possible freeglut is messed
157 * up internally when error is called.
159 printf("Error callback: calling glutExit() to prepare for clean exit\n");
162 /* terminate program */
163 printf ( "Please enter something to exit: " );
164 fgets ( dummy_string, STRING_LENGTH, stdin );
170 Reshape(int width, int height)
172 int window = glutGetWindow () ;
173 printf ( "%6d Window %d Reshape Callback: %d %d\n",
174 ++sequence_number, window, width, height ) ;
176 reshape_width = width ;
177 reshape_height = height ;
178 reshape_seq = sequence_number ;
179 glutPostRedisplay () ;
183 Key(unsigned char key, int x, int y)
185 int window = glutGetWindow () ;
186 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
187 ++sequence_number, window, key, x, y ) ;
192 key_seq = sequence_number ;
193 glutPostRedisplay () ;
197 Special(int key, int x, int y)
199 int window = glutGetWindow () ;
200 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
201 ++sequence_number, window, key, x, y ) ;
206 special_seq = sequence_number ;
207 glutPostRedisplay () ;
213 int window = glutGetWindow () ;
214 printf ( "%6d Window %d Visibility Callback: %d\n",
215 ++sequence_number, window, vis ) ;
216 visibility_called = 1 ;
217 visibility_vis = vis ;
218 visibility_seq = sequence_number ;
219 glutPostRedisplay () ;
223 KeyUp(unsigned char key, int x, int y)
225 int window = glutGetWindow () ;
226 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
227 ++sequence_number, window, key, x, y ) ;
232 keyup_seq = sequence_number ;
233 glutPostRedisplay () ;
237 SpecialUp(int key, int x, int y)
239 int window = glutGetWindow () ;
240 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
241 ++sequence_number, window, key, x, y ) ;
242 specialup_called = 1 ;
243 specialup_key = key ;
246 specialup_seq = sequence_number ;
247 glutPostRedisplay () ;
251 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
253 int window = glutGetWindow () ;
254 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
255 ++sequence_number, window, a, b, c, d ) ;
256 joystick_called = 1 ;
261 joystick_seq = sequence_number ;
262 glutPostRedisplay () ;
266 Mouse(int button, int updown, int x, int y)
268 int window = glutGetWindow () ;
269 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
270 ++sequence_number, window, button, updown, x, y ) ;
272 mouse_button = button ;
273 mouse_updown = updown ;
276 mouse_seq = sequence_number ;
277 glutPostRedisplay () ;
281 MouseWheel(int wheel_number, int direction, int x, int y)
283 int window = glutGetWindow () ;
284 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
285 ++sequence_number, window, wheel_number, direction, x, y ) ;
286 mousewheel_called = 1 ;
287 mousewheel_number = wheel_number ;
288 mousewheel_direction = direction ;
291 mousewheel_seq = sequence_number ;
292 glutPostRedisplay () ;
298 int window = glutGetWindow () ;
299 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
300 ++sequence_number, window, x, y ) ;
304 motion_seq = sequence_number ;
305 glutPostRedisplay () ;
309 PassiveMotion(int x, int y)
311 int window = glutGetWindow () ;
312 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
313 ++sequence_number, window, x, y ) ;
314 passivemotion_called = 1 ;
315 passivemotion_x = x ;
316 passivemotion_y = y ;
317 passivemotion_seq = sequence_number ;
318 glutPostRedisplay () ;
324 int window = glutGetWindow () ;
326 printf ( "%6d Window %d Entry Callback: %d\n",
327 ++sequence_number, window, state ) ;
328 glutPostRedisplay () ;
334 int window = glutGetWindow () ;
336 printf ( "%6d Window %d Close Callback\n",
337 ++sequence_number, window ) ;
343 int window = glutGetWindow () ;
344 overlaydisplay_called = 1 ;
345 printf ( "%6d Window %d OverlayDisplay Callback\n",
346 ++sequence_number, window ) ;
347 glutPostRedisplay () ;
351 WindowStatus(int state)
353 int window = glutGetWindow () ;
354 windowstatus_called = 1 ;
355 printf ( "%6d Window %d WindowStatus Callback: %d\n",
356 ++sequence_number, window, state ) ;
357 glutPostRedisplay () ;
361 SpaceMotion(int x, int y, int z)
363 int window = glutGetWindow () ;
364 spacemotion_called = 1 ;
365 printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n",
366 ++sequence_number, window, x, y, z ) ;
367 glutPostRedisplay () ;
371 SpaceRotation(int x, int y, int z)
373 int window = glutGetWindow () ;
374 spacerotation_called = 1 ;
375 printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n",
376 ++sequence_number, window, x, y, z ) ;
377 glutPostRedisplay () ;
381 SpaceButton(int button, int updown)
383 int window = glutGetWindow () ;
384 spacebutton_called = 1 ;
385 printf ( "%6d Window %d SpaceButton Callback: %d %d\n",
386 ++sequence_number, window, button, updown ) ;
387 glutPostRedisplay () ;
393 int window = glutGetWindow () ;
395 printf ( "%6d Window %d Dials Callback: %d %d\n",
396 ++sequence_number, window, x, y ) ;
397 glutPostRedisplay () ;
401 ButtonBox(int button, int updown)
403 int window = glutGetWindow () ;
404 buttonbox_called = 1 ;
405 printf ( "%6d Window %d ButtonBox Callback: %d %d\n",
406 ++sequence_number, window, button, updown ) ;
407 glutPostRedisplay () ;
411 TabletMotion(int x, int y)
413 int window = glutGetWindow () ;
414 tabletmotion_called = 1 ;
415 printf ( "%6d Window %d TabletMotion Callback: %d %d\n",
416 ++sequence_number, window, x, y ) ;
417 glutPostRedisplay () ;
421 TabletButton(int button, int updown, int x, int y)
423 int window = glutGetWindow () ;
424 tabletbutton_called = 1 ;
425 printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n",
426 ++sequence_number, window, button, updown, x, y ) ;
427 glutPostRedisplay () ;
431 MenuCallback ( int menuID )
433 int window = glutGetWindow () ;
434 printf( "%6d Window %d MenuCallback - menuID is %d\n",
435 ++sequence_number, window, menuID );
441 int window = glutGetWindow () ;
442 menudestroy_called = 1 ;
443 printf ( "%6d Window %d MenuDestroy Callback\n",
444 ++sequence_number, window ) ;
446 if (window) /* When destroyed when shutting down, not always a window defined... */
447 glutPostRedisplay () ;
451 MenuStatus( int status, int x, int y )
453 int window = glutGetWindow () ;
454 menudestroy_called = 1 ;
455 printf ( "%6d Window %d MenuStatus Callback: %d %d %d\n",
456 ++sequence_number, window, status, x, y ) ;
457 glutPostRedisplay () ;
460 static void Idle ( void )
466 main(int argc, char *argv[])
468 int freeglut_window, aux_window ;
469 char dummy_string[STRING_LENGTH];
471 int menuID, subMenuA, subMenuB;
473 glutInitWarningFunc(Warning);
474 glutInitErrorFunc(Error);
475 glutInitWindowSize(500, 250);
476 glutInitWindowPosition ( 140, 140 );
477 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
478 glutInit(&argc, argv);
479 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
481 freeglut_window = glutCreateWindow( "Callback Demo" );
482 printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
484 glClearColor(1.0, 1.0, 1.0, 1.0);
486 glutDisplayFunc( Display );
487 glutReshapeFunc( Reshape );
488 glutKeyboardFunc( Key );
489 glutSpecialFunc( Special );
490 glutVisibilityFunc( Visibility );
491 glutKeyboardUpFunc( KeyUp );
492 glutSpecialUpFunc( SpecialUp );
493 glutJoystickFunc( Joystick, 100 );
494 glutMouseFunc ( Mouse ) ;
495 glutMouseWheelFunc ( MouseWheel ) ;
496 glutMotionFunc ( Motion ) ;
497 glutPassiveMotionFunc ( PassiveMotion ) ;
498 glutEntryFunc ( Entry ) ;
499 glutCloseFunc ( Close ) ;
500 glutOverlayDisplayFunc ( OverlayDisplay ) ;
501 glutWindowStatusFunc ( WindowStatus ) ;
502 glutSpaceballMotionFunc ( SpaceMotion ) ;
503 glutSpaceballRotateFunc ( SpaceRotation ) ;
504 glutSpaceballButtonFunc ( SpaceButton ) ;
505 glutButtonBoxFunc ( ButtonBox ) ;
506 glutDialsFunc ( Dials ) ;
507 glutTabletMotionFunc ( TabletMotion ) ;
508 glutTabletButtonFunc ( TabletButton ) ;
509 glutMenuStatusFunc ( MenuStatus );
510 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
512 subMenuA = glutCreateMenu( MenuCallback );
513 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
514 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
515 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
516 glutMenuDestroyFunc ( MenuDestroy );
518 subMenuB = glutCreateMenu( MenuCallback );
519 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
520 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
521 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
522 glutAddSubMenu( "Going to sub menu A", subMenuA );
523 glutMenuDestroyFunc ( MenuDestroy );
525 menuID = glutCreateMenu( MenuCallback );
526 glutAddMenuEntry( "Entry one", 1 );
527 glutAddMenuEntry( "Entry two", 2 );
528 glutAddMenuEntry( "Entry three", 3 );
529 glutAddMenuEntry( "Entry four", 4 );
530 glutAddMenuEntry( "Entry five", 5 );
531 glutAddSubMenu( "Enter sub menu A", subMenuA );
532 glutAddSubMenu( "Enter sub menu B", subMenuB );
533 glutMenuDestroyFunc ( MenuDestroy );
535 glutAttachMenu( GLUT_LEFT_BUTTON );
537 aux_window = glutCreateWindow( "Second Window" );
538 printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
540 glClearColor(1.0, 1.0, 1.0, 1.0);
542 glutDisplayFunc( Display );
543 glutReshapeFunc( Reshape );
544 glutKeyboardFunc( Key );
545 glutSpecialFunc( Special );
546 glutVisibilityFunc( Visibility );
547 glutKeyboardUpFunc( KeyUp );
548 glutSpecialUpFunc( SpecialUp );
549 /* glutJoystickFunc( Joystick, 100 ); */
550 glutMouseFunc ( Mouse ) ;
551 glutMouseWheelFunc ( MouseWheel ) ;
552 glutMotionFunc ( Motion ) ;
553 glutPassiveMotionFunc ( PassiveMotion ) ;
554 glutEntryFunc ( Entry ) ;
555 glutCloseFunc ( Close ) ;
556 glutOverlayDisplayFunc ( OverlayDisplay ) ;
557 glutWindowStatusFunc ( WindowStatus ) ;
558 glutSpaceballMotionFunc ( SpaceMotion ) ;
559 glutSpaceballRotateFunc ( SpaceRotation ) ;
560 glutSpaceballButtonFunc ( SpaceButton ) ;
561 glutButtonBoxFunc ( ButtonBox ) ;
562 glutDialsFunc ( Dials ) ;
563 glutTabletMotionFunc ( TabletMotion ) ;
564 glutTabletButtonFunc ( TabletButton ) ;
565 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
567 glutIdleFunc ( Idle );
569 printf ( "Please enter something to continue: " );
570 fgets ( dummy_string, STRING_LENGTH, stdin );
574 printf ( "Back from the 'freeglut' main loop\n" ) ;
576 return 0; /* ANSI C requires main to return int. */