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 /* terminate program, after pause for input so user can see */
155 printf ( "Please enter something to exit: " );
156 fgets ( dummy_string, STRING_LENGTH, stdin );
158 /* Call exit directly as freeglut is messed
159 * up internally when an error is called.
165 Reshape(int width, int height)
167 int window = glutGetWindow () ;
168 printf ( "%6d Window %d Reshape Callback: %d %d\n",
169 ++sequence_number, window, width, height ) ;
171 reshape_width = width ;
172 reshape_height = height ;
173 reshape_seq = sequence_number ;
174 glutPostRedisplay () ;
178 Key(unsigned char key, int x, int y)
180 int window = glutGetWindow () ;
181 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
182 ++sequence_number, window, key, x, y ) ;
187 key_seq = sequence_number ;
188 glutPostRedisplay () ;
192 Special(int key, int x, int y)
194 int window = glutGetWindow () ;
195 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
196 ++sequence_number, window, key, x, y ) ;
201 special_seq = sequence_number ;
202 glutPostRedisplay () ;
208 int window = glutGetWindow () ;
209 printf ( "%6d Window %d Visibility Callback: %d\n",
210 ++sequence_number, window, vis ) ;
211 visibility_called = 1 ;
212 visibility_vis = vis ;
213 visibility_seq = sequence_number ;
214 glutPostRedisplay () ;
218 KeyUp(unsigned char key, int x, int y)
220 int window = glutGetWindow () ;
221 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
222 ++sequence_number, window, key, x, y ) ;
227 keyup_seq = sequence_number ;
228 glutPostRedisplay () ;
232 SpecialUp(int key, int x, int y)
234 int window = glutGetWindow () ;
235 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
236 ++sequence_number, window, key, x, y ) ;
237 specialup_called = 1 ;
238 specialup_key = key ;
241 specialup_seq = sequence_number ;
242 glutPostRedisplay () ;
246 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
248 int window = glutGetWindow () ;
249 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
250 ++sequence_number, window, a, b, c, d ) ;
251 joystick_called = 1 ;
256 joystick_seq = sequence_number ;
257 glutPostRedisplay () ;
261 Mouse(int button, int updown, int x, int y)
263 int window = glutGetWindow () ;
264 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
265 ++sequence_number, window, button, updown, x, y ) ;
267 mouse_button = button ;
268 mouse_updown = updown ;
271 mouse_seq = sequence_number ;
272 glutPostRedisplay () ;
276 MouseWheel(int wheel_number, int direction, int x, int y)
278 int window = glutGetWindow () ;
279 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
280 ++sequence_number, window, wheel_number, direction, x, y ) ;
281 mousewheel_called = 1 ;
282 mousewheel_number = wheel_number ;
283 mousewheel_direction = direction ;
286 mousewheel_seq = sequence_number ;
287 glutPostRedisplay () ;
293 int window = glutGetWindow () ;
294 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
295 ++sequence_number, window, x, y ) ;
299 motion_seq = sequence_number ;
300 glutPostRedisplay () ;
304 PassiveMotion(int x, int y)
306 int window = glutGetWindow () ;
307 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
308 ++sequence_number, window, x, y ) ;
309 passivemotion_called = 1 ;
310 passivemotion_x = x ;
311 passivemotion_y = y ;
312 passivemotion_seq = sequence_number ;
313 glutPostRedisplay () ;
319 int window = glutGetWindow () ;
321 printf ( "%6d Window %d Entry Callback: %d\n",
322 ++sequence_number, window, state ) ;
323 glutPostRedisplay () ;
329 int window = glutGetWindow () ;
331 printf ( "%6d Window %d Close Callback\n",
332 ++sequence_number, window ) ;
338 int window = glutGetWindow () ;
339 overlaydisplay_called = 1 ;
340 printf ( "%6d Window %d OverlayDisplay Callback\n",
341 ++sequence_number, window ) ;
342 glutPostRedisplay () ;
346 WindowStatus(int state)
348 int window = glutGetWindow () ;
349 windowstatus_called = 1 ;
350 printf ( "%6d Window %d WindowStatus Callback: %d\n",
351 ++sequence_number, window, state ) ;
352 glutPostRedisplay () ;
356 SpaceMotion(int x, int y, int z)
358 int window = glutGetWindow () ;
359 spacemotion_called = 1 ;
360 printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n",
361 ++sequence_number, window, x, y, z ) ;
362 glutPostRedisplay () ;
366 SpaceRotation(int x, int y, int z)
368 int window = glutGetWindow () ;
369 spacerotation_called = 1 ;
370 printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n",
371 ++sequence_number, window, x, y, z ) ;
372 glutPostRedisplay () ;
376 SpaceButton(int button, int updown)
378 int window = glutGetWindow () ;
379 spacebutton_called = 1 ;
380 printf ( "%6d Window %d SpaceButton Callback: %d %d\n",
381 ++sequence_number, window, button, updown ) ;
382 glutPostRedisplay () ;
388 int window = glutGetWindow () ;
390 printf ( "%6d Window %d Dials Callback: %d %d\n",
391 ++sequence_number, window, x, y ) ;
392 glutPostRedisplay () ;
396 ButtonBox(int button, int updown)
398 int window = glutGetWindow () ;
399 buttonbox_called = 1 ;
400 printf ( "%6d Window %d ButtonBox Callback: %d %d\n",
401 ++sequence_number, window, button, updown ) ;
402 glutPostRedisplay () ;
406 TabletMotion(int x, int y)
408 int window = glutGetWindow () ;
409 tabletmotion_called = 1 ;
410 printf ( "%6d Window %d TabletMotion Callback: %d %d\n",
411 ++sequence_number, window, x, y ) ;
412 glutPostRedisplay () ;
416 TabletButton(int button, int updown, int x, int y)
418 int window = glutGetWindow () ;
419 tabletbutton_called = 1 ;
420 printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n",
421 ++sequence_number, window, button, updown, x, y ) ;
422 glutPostRedisplay () ;
426 MenuCallback ( int menuID )
428 int window = glutGetWindow () ;
429 printf( "%6d Window %d MenuCallback - menuID is %d\n",
430 ++sequence_number, window, menuID );
436 int window = glutGetWindow () ;
437 menudestroy_called = 1 ;
438 printf ( "%6d Window %d MenuDestroy Callback\n",
439 ++sequence_number, window ) ;
441 if (window) /* When destroyed when shutting down, not always a window defined... */
442 glutPostRedisplay () ;
446 MenuStatus( int status, int x, int y )
448 int window = glutGetWindow () ;
449 menudestroy_called = 1 ;
450 printf ( "%6d Window %d MenuStatus Callback: %d %d %d\n",
451 ++sequence_number, window, status, x, y ) ;
452 glutPostRedisplay () ;
455 static void Idle ( void )
461 main(int argc, char *argv[])
463 int freeglut_window, aux_window ;
464 char dummy_string[STRING_LENGTH];
466 int menuID, subMenuA, subMenuB;
468 glutInitWarningFunc(Warning);
469 glutInitErrorFunc(Error);
470 glutInitWindowSize(500, 250);
471 glutInitWindowPosition ( 140, 140 );
472 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
473 glutInit(&argc, argv);
474 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
476 freeglut_window = glutCreateWindow( "Callback Demo" );
477 printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
479 glClearColor(1.0, 1.0, 1.0, 1.0);
481 glutDisplayFunc( Display );
482 glutReshapeFunc( Reshape );
483 glutKeyboardFunc( Key );
484 glutSpecialFunc( Special );
485 glutVisibilityFunc( Visibility );
486 glutKeyboardUpFunc( KeyUp );
487 glutSpecialUpFunc( SpecialUp );
488 glutJoystickFunc( Joystick, 100 );
489 glutMouseFunc ( Mouse ) ;
490 glutMouseWheelFunc ( MouseWheel ) ;
491 glutMotionFunc ( Motion ) ;
492 glutPassiveMotionFunc ( PassiveMotion ) ;
493 glutEntryFunc ( Entry ) ;
494 glutCloseFunc ( Close ) ;
495 glutOverlayDisplayFunc ( OverlayDisplay ) ;
496 glutWindowStatusFunc ( WindowStatus ) ;
497 glutSpaceballMotionFunc ( SpaceMotion ) ;
498 glutSpaceballRotateFunc ( SpaceRotation ) ;
499 glutSpaceballButtonFunc ( SpaceButton ) ;
500 glutButtonBoxFunc ( ButtonBox ) ;
501 glutDialsFunc ( Dials ) ;
502 glutTabletMotionFunc ( TabletMotion ) ;
503 glutTabletButtonFunc ( TabletButton ) ;
504 glutMenuStatusFunc ( MenuStatus );
505 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
507 subMenuA = glutCreateMenu( MenuCallback );
508 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
509 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
510 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
511 glutMenuDestroyFunc ( MenuDestroy );
513 subMenuB = glutCreateMenu( MenuCallback );
514 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
515 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
516 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
517 glutAddSubMenu( "Going to sub menu A", subMenuA );
518 glutMenuDestroyFunc ( MenuDestroy );
520 menuID = glutCreateMenu( MenuCallback );
521 glutAddMenuEntry( "Entry one", 1 );
522 glutAddMenuEntry( "Entry two", 2 );
523 glutAddMenuEntry( "Entry three", 3 );
524 glutAddMenuEntry( "Entry four", 4 );
525 glutAddMenuEntry( "Entry five", 5 );
526 glutAddSubMenu( "Enter sub menu A", subMenuA );
527 glutAddSubMenu( "Enter sub menu B", subMenuB );
528 glutMenuDestroyFunc ( MenuDestroy );
530 glutAttachMenu( GLUT_LEFT_BUTTON );
532 aux_window = glutCreateWindow( "Second Window" );
533 printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
535 glClearColor(1.0, 1.0, 1.0, 1.0);
537 glutDisplayFunc( Display );
538 glutReshapeFunc( Reshape );
539 glutKeyboardFunc( Key );
540 glutSpecialFunc( Special );
541 glutVisibilityFunc( Visibility );
542 glutKeyboardUpFunc( KeyUp );
543 glutSpecialUpFunc( SpecialUp );
544 /* glutJoystickFunc( Joystick, 100 ); */
545 glutMouseFunc ( Mouse ) ;
546 glutMouseWheelFunc ( MouseWheel ) ;
547 glutMotionFunc ( Motion ) ;
548 glutPassiveMotionFunc ( PassiveMotion ) ;
549 glutEntryFunc ( Entry ) ;
550 glutCloseFunc ( Close ) ;
551 glutOverlayDisplayFunc ( OverlayDisplay ) ;
552 glutWindowStatusFunc ( WindowStatus ) ;
553 glutSpaceballMotionFunc ( SpaceMotion ) ;
554 glutSpaceballRotateFunc ( SpaceRotation ) ;
555 glutSpaceballButtonFunc ( SpaceButton ) ;
556 glutButtonBoxFunc ( ButtonBox ) ;
557 glutDialsFunc ( Dials ) ;
558 glutTabletMotionFunc ( TabletMotion ) ;
559 glutTabletButtonFunc ( TabletButton ) ;
560 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
562 glutIdleFunc ( Idle );
564 printf ( "Please enter something to continue: " );
565 fgets ( dummy_string, STRING_LENGTH, stdin );
569 printf ( "Back from the 'freeglut' main loop\n" ) ;
571 return 0; /* ANSI C requires main to return int. */