3 * Program to invoke all the callbacks that "freeglut" supports
7 #include <GL/freeglut.h>
12 static int sequence_number = 0 ;
16 int reshape_called = 0, key_called = 0, special_called = 0, visibility_called = 0,
17 keyup_called = 0, specialup_called = 0, joystick_called = 0, mouse_called = 0,
18 mousewheel_called = 0, motion_called = 0, passivemotion_called = 0, entry_called = 0,
19 close_called = 0, overlaydisplay_called = 0, windowstatus_called = 0,
20 spacemotion_called = 0, spacerotation_called = 0, spacebutton_called = 0,
21 buttonbox_called = 0, dials_called = 0, tabletmotion_called = 0, tabletbutton_called = 0,
22 menudestroy_called = 0, menustatus_called = 0 ;
23 int reshape_width = -1, reshape_height = -1, reshape_seq = -1 ;
24 int key_key = -1, key_x = -1, key_y = -1, key_seq = -1 ;
25 int special_key = -1, special_x = -1, special_y = -1, special_seq = -1 ;
26 int visibility_vis = -1, visibility_seq = -1 ;
27 int keyup_key = -1, keyup_x = -1, keyup_y = -1, keyup_seq = -1 ;
28 int specialup_key = -1, specialup_x = -1, specialup_y = -1, specialup_seq = -1 ;
29 int joystick_a = -1, joystick_b = -1, joystick_c = -1, joystick_d = -1, joystick_seq = -1 ; /* Need meaningful names */
30 int mouse_button = -1, mouse_updown = -1, mouse_x = -1, mouse_y = -1, mouse_seq = -1 ;
31 int mousewheel_number = -1, mousewheel_direction = -1, mousewheel_x = -1, mousewheel_y = -1, mousewheel_seq = -1 ;
32 int motion_x = -1, motion_y = -1, motion_seq = -1 ;
33 int passivemotion_x = -1, passivemotion_y = -1, passivemotion_seq = -1 ;
35 #define STRING_LENGTH 10
38 bitmapPrintf (const char *fmt, ...)
44 #if defined(WIN32) && !defined(__CYGWIN__)
45 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
47 (void) vsnprintf (buf, sizeof(buf), fmt, args);
50 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
57 int window = glutGetWindow () ;
58 glClear ( GL_COLOR_BUFFER_BIT );
60 glDisable ( GL_DEPTH_TEST );
61 glMatrixMode ( GL_PROJECTION );
64 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
65 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
66 glMatrixMode ( GL_MODELVIEW );
69 glColor3ub ( 0, 0, 0 );
70 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 20 ); /* 10pt margin above 10pt letters */
74 bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height );
79 bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
84 bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
87 if ( visibility_called )
89 bitmapPrintf ( "Visibility %d: %d\n", visibility_seq, visibility_vis );
94 bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
97 if ( specialup_called )
99 bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
102 if ( joystick_called )
104 bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
109 bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
112 if ( mousewheel_called )
114 bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
119 bitmapPrintf ( "Motion %d: %d %d\n", motion_seq, motion_x, motion_y );
122 if ( passivemotion_called )
124 bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
127 glMatrixMode ( GL_PROJECTION );
129 glMatrixMode ( GL_MODELVIEW );
131 glEnable ( GL_DEPTH_TEST );
133 printf ( "%6d Window %d Display Callback\n",
134 ++sequence_number, window ) ;
139 Warning(const char *fmt, va_list ap)
141 printf("%6d Warning callback:\n");
143 /* print warning message */
148 Error(const char *fmt, va_list ap)
150 char dummy_string[STRING_LENGTH];
151 printf("%6d Error callback:\n");
153 /* print warning message */
156 /* terminate program, after pause for input so user can see */
157 printf ( "Please enter something to exit: " );
158 fgets ( dummy_string, STRING_LENGTH, stdin );
160 /* Call exit directly as freeglut is messed
161 * up internally when an error is called.
167 Reshape(int width, int height)
169 int window = glutGetWindow () ;
170 printf ( "%6d Window %d Reshape Callback: %d %d\n",
171 ++sequence_number, window, width, height ) ;
173 reshape_width = width ;
174 reshape_height = height ;
175 reshape_seq = sequence_number ;
176 glViewport(0,0,width,height);
177 glutPostRedisplay () ;
181 Key(unsigned char key, int x, int y)
183 int window = glutGetWindow () ;
184 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
185 ++sequence_number, window, key, x, y ) ;
190 key_seq = sequence_number ;
191 glutPostRedisplay () ;
195 Special(int key, int x, int y)
197 int window = glutGetWindow () ;
198 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
199 ++sequence_number, window, key, x, y ) ;
204 special_seq = sequence_number ;
205 glutPostRedisplay () ;
211 int window = glutGetWindow () ;
212 printf ( "%6d Window %d Visibility Callback: %d\n",
213 ++sequence_number, window, vis ) ;
214 visibility_called = 1 ;
215 visibility_vis = vis ;
216 visibility_seq = sequence_number ;
217 glutPostRedisplay () ;
221 KeyUp(unsigned char key, int x, int y)
223 int window = glutGetWindow () ;
224 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
225 ++sequence_number, window, key, x, y ) ;
230 keyup_seq = sequence_number ;
231 glutPostRedisplay () ;
235 SpecialUp(int key, int x, int y)
237 int window = glutGetWindow () ;
238 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
239 ++sequence_number, window, key, x, y ) ;
240 specialup_called = 1 ;
241 specialup_key = key ;
244 specialup_seq = sequence_number ;
245 glutPostRedisplay () ;
249 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
251 int window = glutGetWindow () ;
252 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
253 ++sequence_number, window, a, b, c, d ) ;
254 joystick_called = 1 ;
259 joystick_seq = sequence_number ;
260 glutPostRedisplay () ;
264 Mouse(int button, int updown, int x, int y)
266 int window = glutGetWindow () ;
267 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
268 ++sequence_number, window, button, updown, x, y ) ;
270 mouse_button = button ;
271 mouse_updown = updown ;
274 mouse_seq = sequence_number ;
275 glutPostRedisplay () ;
279 MouseWheel(int wheel_number, int direction, int x, int y)
281 int window = glutGetWindow () ;
282 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
283 ++sequence_number, window, wheel_number, direction, x, y ) ;
284 mousewheel_called = 1 ;
285 mousewheel_number = wheel_number ;
286 mousewheel_direction = direction ;
289 mousewheel_seq = sequence_number ;
290 glutPostRedisplay () ;
296 int window = glutGetWindow () ;
297 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
298 ++sequence_number, window, x, y ) ;
302 motion_seq = sequence_number ;
303 glutPostRedisplay () ;
307 PassiveMotion(int x, int y)
309 int window = glutGetWindow () ;
310 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
311 ++sequence_number, window, x, y ) ;
312 passivemotion_called = 1 ;
313 passivemotion_x = x ;
314 passivemotion_y = y ;
315 passivemotion_seq = sequence_number ;
316 glutPostRedisplay () ;
322 int window = glutGetWindow () ;
324 printf ( "%6d Window %d Entry Callback: %d\n",
325 ++sequence_number, window, state ) ;
326 glutPostRedisplay () ;
332 int window = glutGetWindow () ;
334 printf ( "%6d Window %d Close Callback\n",
335 ++sequence_number, window ) ;
341 int window = glutGetWindow () ;
342 overlaydisplay_called = 1 ;
343 printf ( "%6d Window %d OverlayDisplay Callback\n",
344 ++sequence_number, window ) ;
345 glutPostRedisplay () ;
349 WindowStatus(int state)
351 int window = glutGetWindow () ;
352 windowstatus_called = 1 ;
353 printf ( "%6d Window %d WindowStatus Callback: %d\n",
354 ++sequence_number, window, state ) ;
355 glutPostRedisplay () ;
359 SpaceMotion(int x, int y, int z)
361 int window = glutGetWindow () ;
362 spacemotion_called = 1 ;
363 printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n",
364 ++sequence_number, window, x, y, z ) ;
365 glutPostRedisplay () ;
369 SpaceRotation(int x, int y, int z)
371 int window = glutGetWindow () ;
372 spacerotation_called = 1 ;
373 printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n",
374 ++sequence_number, window, x, y, z ) ;
375 glutPostRedisplay () ;
379 SpaceButton(int button, int updown)
381 int window = glutGetWindow () ;
382 spacebutton_called = 1 ;
383 printf ( "%6d Window %d SpaceButton Callback: %d %d\n",
384 ++sequence_number, window, button, updown ) ;
385 glutPostRedisplay () ;
391 int window = glutGetWindow () ;
393 printf ( "%6d Window %d Dials Callback: %d %d\n",
394 ++sequence_number, window, x, y ) ;
395 glutPostRedisplay () ;
399 ButtonBox(int button, int updown)
401 int window = glutGetWindow () ;
402 buttonbox_called = 1 ;
403 printf ( "%6d Window %d ButtonBox Callback: %d %d\n",
404 ++sequence_number, window, button, updown ) ;
405 glutPostRedisplay () ;
409 TabletMotion(int x, int y)
411 int window = glutGetWindow () ;
412 tabletmotion_called = 1 ;
413 printf ( "%6d Window %d TabletMotion Callback: %d %d\n",
414 ++sequence_number, window, x, y ) ;
415 glutPostRedisplay () ;
419 TabletButton(int button, int updown, int x, int y)
421 int window = glutGetWindow () ;
422 tabletbutton_called = 1 ;
423 printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n",
424 ++sequence_number, window, button, updown, x, y ) ;
425 glutPostRedisplay () ;
429 MenuCallback ( int menuID )
431 int window = glutGetWindow () ;
432 printf( "%6d Window %d MenuCallback - menuID is %d\n",
433 ++sequence_number, window, menuID );
439 int window = glutGetWindow () ;
440 menudestroy_called = 1 ;
441 printf ( "%6d Window %d MenuDestroy Callback\n",
442 ++sequence_number, window ) ;
444 if (window) /* When destroyed when shutting down, not always a window defined... */
445 glutPostRedisplay () ;
449 MenuStatus( int status, int x, int y )
451 int window = glutGetWindow () ;
452 menudestroy_called = 1 ;
453 printf ( "%6d Window %d MenuStatus Callback: %d %d %d\n",
454 ++sequence_number, window, status, x, y ) ;
455 glutPostRedisplay () ;
458 static void Idle ( void )
464 main(int argc, char *argv[])
466 char dummy_string[STRING_LENGTH];
468 int menuID, subMenuA, subMenuB;
470 glutInitWarningFunc(Warning);
471 glutInitErrorFunc(Error);
472 glutInitWindowSize(500, 250);
473 glutInitWindowPosition ( 140, 140 );
474 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
475 glutInit(&argc, argv);
476 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
478 windows[0] = glutCreateWindow( "Callback Demo" );
479 printf ( "Creating window %d as 'Callback Demo'\n", windows[0] ) ;
481 glClearColor(1.0, 1.0, 1.0, 1.0);
483 glutDisplayFunc( Display );
484 glutReshapeFunc( Reshape );
485 glutKeyboardFunc( Key );
486 glutSpecialFunc( Special );
487 glutVisibilityFunc( Visibility );
488 glutKeyboardUpFunc( KeyUp );
489 glutSpecialUpFunc( SpecialUp );
490 glutJoystickFunc( Joystick, 100 );
491 glutMouseFunc ( Mouse ) ;
492 glutMouseWheelFunc ( MouseWheel ) ;
493 glutMotionFunc ( Motion ) ;
494 glutPassiveMotionFunc ( PassiveMotion ) ;
495 glutEntryFunc ( Entry ) ;
496 glutCloseFunc ( Close ) ;
497 glutOverlayDisplayFunc ( OverlayDisplay ) ;
498 glutWindowStatusFunc ( WindowStatus ) ;
499 glutSpaceballMotionFunc ( SpaceMotion ) ;
500 glutSpaceballRotateFunc ( SpaceRotation ) ;
501 glutSpaceballButtonFunc ( SpaceButton ) ;
502 glutButtonBoxFunc ( ButtonBox ) ;
503 glutDialsFunc ( Dials ) ;
504 glutTabletMotionFunc ( TabletMotion ) ;
505 glutTabletButtonFunc ( TabletButton ) ;
506 glutMenuStatusFunc ( MenuStatus );
507 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
509 subMenuA = glutCreateMenu( MenuCallback );
510 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
511 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
512 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
513 glutMenuDestroyFunc ( MenuDestroy );
515 subMenuB = glutCreateMenu( MenuCallback );
516 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
517 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
518 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
519 glutAddSubMenu( "Going to sub menu A", subMenuA );
520 glutMenuDestroyFunc ( MenuDestroy );
522 menuID = glutCreateMenu( MenuCallback );
523 glutAddMenuEntry( "Entry one", 1 );
524 glutAddMenuEntry( "Entry two", 2 );
525 glutAddMenuEntry( "Entry three", 3 );
526 glutAddMenuEntry( "Entry four", 4 );
527 glutAddMenuEntry( "Entry five", 5 );
528 glutAddSubMenu( "Enter sub menu A", subMenuA );
529 glutAddSubMenu( "Enter sub menu B", subMenuB );
530 glutMenuDestroyFunc ( MenuDestroy );
532 glutAttachMenu( GLUT_LEFT_BUTTON );
534 glutInitWindowPosition ( 140+500+2*glutGet(GLUT_WINDOW_BORDER_WIDTH), 140 );
535 /* Position second window right next to the first */
536 windows[1] = glutCreateWindow( "Second Window" );
537 printf ( "Creating window %d as 'Second Window'\n", windows[1] ) ;
539 glClearColor(1.0, 1.0, 1.0, 1.0);
541 glutDisplayFunc( Display );
542 glutReshapeFunc( Reshape );
543 glutKeyboardFunc( Key );
544 glutSpecialFunc( Special );
545 glutVisibilityFunc( Visibility );
546 glutKeyboardUpFunc( KeyUp );
547 glutSpecialUpFunc( SpecialUp );
548 /* glutJoystickFunc( Joystick, 100 ); */
549 glutMouseFunc ( Mouse ) ;
550 glutMouseWheelFunc ( MouseWheel ) ;
551 glutMotionFunc ( Motion ) ;
552 glutPassiveMotionFunc ( PassiveMotion ) ;
553 glutEntryFunc ( Entry ) ;
554 glutCloseFunc ( Close ) ;
555 glutOverlayDisplayFunc ( OverlayDisplay ) ;
556 glutWindowStatusFunc ( WindowStatus ) ;
557 glutSpaceballMotionFunc ( SpaceMotion ) ;
558 glutSpaceballRotateFunc ( SpaceRotation ) ;
559 glutSpaceballButtonFunc ( SpaceButton ) ;
560 glutButtonBoxFunc ( ButtonBox ) ;
561 glutDialsFunc ( Dials ) ;
562 glutTabletMotionFunc ( TabletMotion ) ;
563 glutTabletButtonFunc ( TabletButton ) ;
564 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
566 glutIdleFunc ( Idle );
568 printf ( "Please enter something to continue: " );
569 fgets ( dummy_string, STRING_LENGTH, stdin );
573 printf ( "Back from the 'freeglut' main loop\n" ) ;
575 return 0; /* ANSI C requires main to return int. */