3 * Program to invoke all the callbacks that "freeglut" supports
7 #include <GL/freeglut.h>
12 static int sequence_number = 0 ;
16 /* define status vars showing whether given callback has been called for given window */
17 #define CALLBACK_CALLED_VAR(name) int name##_called[2] = {0};
18 #define CALLBACK_1_VAR(name,field) int name##_##field[2], name##_seq[2] = {-1};
19 #define CALLBACK_2_VARS(name,field1,field2) int name##_##field1[2], name##_##field2[2], name##_seq[2] = {-1};
20 #define CALLBACK_3_VARS(name,field1,field2,field3) int name##_##field1[2], name##_##field2[2], name##_##field3[2], name##_seq[2] = {-1};
21 #define CALLBACK_4_VARS(name,field1,field2,field3,field4) int name##_##field1[2], name##_##field2[2], name##_##field3[2], name##_##field4[2], name##_seq[2] = {-1};
22 CALLBACK_CALLED_VAR(reshape);
23 CALLBACK_2_VARS(reshape,width,height);
24 CALLBACK_CALLED_VAR(key);
25 CALLBACK_3_VARS(key,key,x,y);
26 CALLBACK_CALLED_VAR(keyup);
27 CALLBACK_3_VARS(keyup,key,x,y);
28 CALLBACK_CALLED_VAR(special);
29 CALLBACK_3_VARS(special,key,x,y);
30 CALLBACK_CALLED_VAR(specialup);
31 CALLBACK_3_VARS(specialup,key,x,y);
32 CALLBACK_CALLED_VAR(visibility);
33 CALLBACK_1_VAR(visibility,vis);
34 CALLBACK_CALLED_VAR(joystick);
35 CALLBACK_4_VARS(joystick,a,b,c,d);
36 CALLBACK_CALLED_VAR(mouse);
37 CALLBACK_4_VARS(mouse,button,updown,x,y);
38 CALLBACK_CALLED_VAR(mousewheel);
39 CALLBACK_4_VARS(mousewheel,number,direction,x,y);
40 CALLBACK_CALLED_VAR(motion);
41 CALLBACK_2_VARS(motion,x,y);
42 CALLBACK_CALLED_VAR(passivemotion);
43 CALLBACK_2_VARS(passivemotion,x,y);
44 CALLBACK_CALLED_VAR(entry);
45 CALLBACK_CALLED_VAR(close);
46 /*CALLBACK_CALLED_VAR(overlaydisplay);
47 CALLBACK_CALLED_VAR(windowstatus);
48 CALLBACK_CALLED_VAR(spacemotion);
49 CALLBACK_CALLED_VAR(spacerotation);
50 CALLBACK_CALLED_VAR(spacebutton);
51 CALLBACK_CALLED_VAR(buttonbox);
52 CALLBACK_CALLED_VAR(dials);
53 CALLBACK_CALLED_VAR(tabletmotion);
54 CALLBACK_CALLED_VAR(tabletbutton);
55 CALLBACK_CALLED_VAR(menustatus);*/
56 /* menudestroy is registered on each menu, not a window */
57 int menudestroy_called = 0 ;
58 /* menustatus and menustate are global callbacks, set for all menus at the same time */
59 int menustatus_called = 0;
60 int menustate_called = 0;
62 #define STRING_LENGTH 10
65 bitmapPrintf (const char *fmt, ...)
71 #if defined(WIN32) && !defined(__CYGWIN__)
72 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
74 (void) vsnprintf (buf, sizeof(buf), fmt, args);
77 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
81 getWindowAndIdx(int *winIdx)
83 int window = glutGetWindow();
86 (*winIdx) = window==windows[0]?0:1;
95 int window = getWindowAndIdx(&winIdx);
96 glClear ( GL_COLOR_BUFFER_BIT );
98 glDisable ( GL_DEPTH_TEST );
99 glMatrixMode ( GL_PROJECTION );
102 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
103 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
104 glMatrixMode ( GL_MODELVIEW );
107 glColor3ub ( 0, 0, 0 );
108 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 20 ); /* 10pt margin above 10pt letters */
110 if ( reshape_called[winIdx] )
112 bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq[winIdx], reshape_width[winIdx], reshape_height[winIdx] );
115 if ( key_called[winIdx] )
117 bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq[winIdx], key_key[winIdx], key_key[winIdx], key_x[winIdx], key_y[winIdx] );
120 if ( special_called[winIdx] )
122 bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq[winIdx], special_key[winIdx], special_key[winIdx], special_x[winIdx], special_y[winIdx] );
125 if ( visibility_called[winIdx] )
127 bitmapPrintf ( "Visibility %d: %d\n", visibility_seq[winIdx], visibility_vis[winIdx] );
130 if ( keyup_called[winIdx] )
132 bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq[winIdx], keyup_key[winIdx], keyup_key[winIdx], keyup_x[winIdx], keyup_y[winIdx] );
135 if ( specialup_called[winIdx] )
137 bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq[winIdx], specialup_key[winIdx], specialup_key[winIdx], specialup_x[winIdx], specialup_y[winIdx] );
140 if ( joystick_called[winIdx] )
142 bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq[winIdx], joystick_a[winIdx], joystick_b[winIdx], joystick_c[winIdx], joystick_d[winIdx] );
145 if ( mouse_called[winIdx] )
147 bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq[winIdx], mouse_button[winIdx], mouse_updown[winIdx], mouse_x[winIdx], mouse_y[winIdx] );
150 if ( mousewheel_called[winIdx] )
152 bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq[winIdx], mousewheel_number[winIdx], mousewheel_direction[winIdx], mousewheel_x[winIdx], mousewheel_y[winIdx] );
155 if ( motion_called[winIdx] )
157 bitmapPrintf ( "Motion %d: %d %d\n", motion_seq[winIdx], motion_x[winIdx], motion_y[winIdx] );
160 if ( passivemotion_called[winIdx] )
162 bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq[winIdx], passivemotion_x[winIdx], passivemotion_y[winIdx] );
165 glMatrixMode ( GL_PROJECTION );
167 glMatrixMode ( GL_MODELVIEW );
169 glEnable ( GL_DEPTH_TEST );
171 printf ( "%6d Window %d Display Callback\n",
172 ++sequence_number, window ) ;
177 Warning(const char *fmt, va_list ap)
179 printf("%6d Warning callback:\n");
181 /* print warning message */
186 Error(const char *fmt, va_list ap)
188 char dummy_string[STRING_LENGTH];
189 printf("%6d Error callback:\n");
191 /* print warning message */
194 /* terminate program, after pause for input so user can see */
195 printf ( "Please enter something to exit: " );
196 fgets ( dummy_string, STRING_LENGTH, stdin );
198 /* Call exit directly as freeglut is messed
199 * up internally when an error is called.
205 Reshape(int width, int height)
208 int window = getWindowAndIdx(&winIdx);
209 printf ( "%6d Window %d Reshape Callback: %d %d\n",
210 ++sequence_number, window, width, height ) ;
211 reshape_called[winIdx] = 1 ;
212 reshape_width[winIdx] = width ;
213 reshape_height[winIdx] = height ;
214 reshape_seq[winIdx] = sequence_number ;
215 glViewport(0,0,width,height);
216 glutPostRedisplay () ;
220 Key(unsigned char key, int x, int y)
223 int window = getWindowAndIdx(&winIdx);
224 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
225 ++sequence_number, window, key, x, y ) ;
226 key_called[winIdx] = 1 ;
227 key_key[winIdx] = key ;
230 key_seq[winIdx] = sequence_number ;
231 glutPostRedisplay () ;
235 Special(int key, int x, int y)
238 int window = getWindowAndIdx(&winIdx);
239 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
240 ++sequence_number, window, key, x, y ) ;
241 special_called[winIdx] = 1 ;
242 special_key[winIdx] = key ;
243 special_x[winIdx] = x ;
244 special_y[winIdx] = y ;
245 special_seq[winIdx] = sequence_number ;
246 glutPostRedisplay () ;
253 int window = getWindowAndIdx(&winIdx);
254 printf ( "%6d Window %d Visibility Callback: %d\n",
255 ++sequence_number, window, vis ) ;
256 visibility_called[winIdx] = 1 ;
257 visibility_vis[winIdx] = vis ;
258 visibility_seq[winIdx] = sequence_number ;
259 glutPostRedisplay () ;
263 KeyUp(unsigned char key, int x, int y)
266 int window = getWindowAndIdx(&winIdx);
267 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
268 ++sequence_number, window, key, x, y ) ;
269 keyup_called[winIdx] = 1 ;
270 keyup_key[winIdx] = key ;
271 keyup_x[winIdx] = x ;
272 keyup_y[winIdx] = y ;
273 keyup_seq[winIdx] = sequence_number ;
274 glutPostRedisplay () ;
278 SpecialUp(int key, int x, int y)
281 int window = getWindowAndIdx(&winIdx);
282 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
283 ++sequence_number, window, key, x, y ) ;
284 specialup_called[winIdx] = 1 ;
285 specialup_key[winIdx] = key ;
286 specialup_x[winIdx] = x ;
287 specialup_y[winIdx] = y ;
288 specialup_seq[winIdx] = sequence_number ;
289 glutPostRedisplay () ;
293 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
296 int window = getWindowAndIdx(&winIdx);
297 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
298 ++sequence_number, window, a, b, c, d ) ;
299 joystick_called[winIdx] = 1 ;
300 joystick_a[winIdx] = a ;
301 joystick_b[winIdx] = b ;
302 joystick_c[winIdx] = c ;
303 joystick_d[winIdx] = d ;
304 joystick_seq[winIdx] = sequence_number ;
305 glutPostRedisplay () ;
309 Mouse(int button, int updown, int x, int y)
312 int window = getWindowAndIdx(&winIdx);
313 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
314 ++sequence_number, window, button, updown, x, y ) ;
315 mouse_called[winIdx] = 1 ;
316 mouse_button[winIdx] = button ;
317 mouse_updown[winIdx] = updown ;
318 mouse_x[winIdx] = x ;
319 mouse_y[winIdx] = y ;
320 mouse_seq[winIdx] = sequence_number ;
321 glutPostRedisplay () ;
325 MouseWheel(int wheel_number, int direction, int x, int y)
328 int window = getWindowAndIdx(&winIdx);
329 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
330 ++sequence_number, window, wheel_number, direction, x, y ) ;
331 mousewheel_called[winIdx] = 1 ;
332 mousewheel_number[winIdx] = wheel_number ;
333 mousewheel_direction[winIdx] = direction ;
334 mousewheel_x[winIdx] = x ;
335 mousewheel_y[winIdx] = y ;
336 mousewheel_seq[winIdx] = sequence_number ;
337 glutPostRedisplay () ;
344 int window = getWindowAndIdx(&winIdx);
345 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
346 ++sequence_number, window, x, y ) ;
347 motion_called[winIdx] = 1 ;
348 motion_x[winIdx] = x ;
349 motion_y[winIdx] = y ;
350 motion_seq[winIdx] = sequence_number ;
351 glutPostRedisplay () ;
355 PassiveMotion(int x, int y)
358 int window = getWindowAndIdx(&winIdx);
359 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
360 ++sequence_number, window, x, y ) ;
361 passivemotion_called[winIdx] = 1 ;
362 passivemotion_x[winIdx] = x ;
363 passivemotion_y[winIdx] = y ;
364 passivemotion_seq[winIdx] = sequence_number ;
365 glutPostRedisplay () ;
371 int window = getWindowAndIdx(NULL);
372 printf ( "%6d Window %d Entry Callback: %d\n",
373 ++sequence_number, window, state ) ;
374 glutPostRedisplay () ;
380 int window = getWindowAndIdx(NULL);
381 printf ( "%6d Window %d Close Callback\n",
382 ++sequence_number, window ) ;
388 int window = getWindowAndIdx(NULL);
389 printf ( "%6d Window %d OverlayDisplay Callback\n",
390 ++sequence_number, window ) ;
391 glutPostRedisplay () ;
395 WindowStatus(int state)
397 int window = getWindowAndIdx(NULL);
398 printf ( "%6d Window %d WindowStatus Callback: %d\n",
399 ++sequence_number, window, state ) ;
400 glutPostRedisplay () ;
404 SpaceMotion(int x, int y, int z)
406 int window = getWindowAndIdx(NULL);
407 printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n",
408 ++sequence_number, window, x, y, z ) ;
409 glutPostRedisplay () ;
413 SpaceRotation(int x, int y, int z)
415 int window = getWindowAndIdx(NULL);
416 printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n",
417 ++sequence_number, window, x, y, z ) ;
418 glutPostRedisplay () ;
422 SpaceButton(int button, int updown)
424 int window = getWindowAndIdx(NULL);
425 printf ( "%6d Window %d SpaceButton Callback: %d %d\n",
426 ++sequence_number, window, button, updown ) ;
427 glutPostRedisplay () ;
433 int window = getWindowAndIdx(NULL);
434 printf ( "%6d Window %d Dials Callback: %d %d\n",
435 ++sequence_number, window, x, y ) ;
436 glutPostRedisplay () ;
440 ButtonBox(int button, int updown)
442 int window = getWindowAndIdx(NULL);
443 printf ( "%6d Window %d ButtonBox Callback: %d %d\n",
444 ++sequence_number, window, button, updown ) ;
445 glutPostRedisplay () ;
449 TabletMotion(int x, int y)
451 int window = getWindowAndIdx(NULL);
452 printf ( "%6d Window %d TabletMotion Callback: %d %d\n",
453 ++sequence_number, window, x, y ) ;
454 glutPostRedisplay () ;
458 TabletButton(int button, int updown, int x, int y)
460 int window = getWindowAndIdx(NULL);
461 printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n",
462 ++sequence_number, window, button, updown, x, y ) ;
463 glutPostRedisplay () ;
467 MenuCallback ( int value )
469 int menu = glutGetMenu();
470 int window = getWindowAndIdx(NULL);
471 printf( "%6d Menu %d MenuCallback for menu opened in Window %d - value is %d\n",
472 ++sequence_number, menu, window, value );
478 int menu = glutGetMenu();
479 menudestroy_called = 1 ;
480 printf ( "%6d Menu %d MenuDestroy Callback\n",
481 ++sequence_number, menu ) ;
485 MenuStatus( int status, int x, int y )
487 /* Menu and window for which this event is triggered are current when the callback is called */
488 int menu = glutGetMenu();
489 int window = getWindowAndIdx(NULL);
490 menustatus_called = 1 ;
491 printf ( "%6d Menu %d MenuStatus Callback in Window %d: %d %d %d\n",
492 ++sequence_number, menu, window, status, x, y ) ;
493 glutPostRedisplay () ;
498 MenuState( int status )
500 /* Menu and window for which this event is triggered are current when the callback is called */
501 int menu = glutGetMenu();
502 int window = getWindowAndIdx(NULL);
503 menustate_called = 1 ;
504 printf ( "%6d Menu %d MenuState Callback in Window %d: %d\n",
505 ++sequence_number, menu, window, status) ;
506 glutPostRedisplay () ;
509 static void Idle ( void )
515 main(int argc, char *argv[])
517 char dummy_string[STRING_LENGTH];
519 int menuID, subMenuA, subMenuB;
521 glutInitWarningFunc(Warning);
522 glutInitErrorFunc(Error);
523 glutInitWindowSize(500, 250);
524 glutInitWindowPosition ( 140, 140 );
525 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
526 glutInit(&argc, argv);
527 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
529 /* Set other global callback (global as in not associated with any specific menu or window) */
530 glutIdleFunc ( Idle );
531 glutMenuStatusFunc ( MenuStatus );
532 glutMenuStateFunc ( MenuState ); /* Note that glutMenuStatusFunc is an enhanced version of the deprecated glutMenuStateFunc. */
534 windows[0] = glutCreateWindow( "Callback Demo" );
535 printf ( "Creating window %d as 'Callback Demo'\n", windows[0] ) ;
537 glClearColor(1.0, 1.0, 1.0, 1.0);
539 /* callbacks and settings specific to this window */
540 glutDisplayFunc( Display );
541 glutReshapeFunc( Reshape );
542 glutKeyboardFunc( Key );
543 glutSpecialFunc( Special );
544 glutVisibilityFunc( Visibility );
545 glutKeyboardUpFunc( KeyUp );
546 glutSpecialUpFunc( SpecialUp );
547 glutJoystickFunc( Joystick, 100 );
548 glutMouseFunc ( Mouse ) ;
549 glutMouseWheelFunc ( MouseWheel ) ;
550 glutMotionFunc ( Motion ) ;
551 glutPassiveMotionFunc ( PassiveMotion ) ;
552 glutEntryFunc ( Entry ) ;
553 glutCloseFunc ( Close ) ;
554 glutOverlayDisplayFunc ( OverlayDisplay ) ;
555 glutWindowStatusFunc ( WindowStatus ) ;
556 glutSpaceballMotionFunc ( SpaceMotion ) ;
557 glutSpaceballRotateFunc ( SpaceRotation ) ;
558 glutSpaceballButtonFunc ( SpaceButton ) ;
559 glutButtonBoxFunc ( ButtonBox ) ;
560 glutDialsFunc ( Dials ) ;
561 glutTabletMotionFunc ( TabletMotion ) ;
562 glutTabletButtonFunc ( TabletButton ) ;
563 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
565 subMenuA = glutCreateMenu( MenuCallback );
566 glutAddMenuEntry( "Sub menu A1 (01)", 11 );
567 glutAddMenuEntry( "Sub menu A2 (02)", 12 );
568 glutAddMenuEntry( "Sub menu A3 (03)", 13 );
569 glutMenuDestroyFunc ( MenuDestroy ); /* callback specific to this menu */
571 subMenuB = glutCreateMenu( MenuCallback );
572 glutAddMenuEntry( "Sub menu B1 (04)", 14 );
573 glutAddMenuEntry( "Sub menu B2 (05)", 15 );
574 glutAddMenuEntry( "Sub menu B3 (06)", 16 );
575 glutAddSubMenu( "Going to sub menu A", subMenuA );
576 glutMenuDestroyFunc ( MenuDestroy ); /* callback specific to this menu */
578 menuID = glutCreateMenu( MenuCallback );
579 glutAddMenuEntry( "Entry one", 21 );
580 glutAddMenuEntry( "Entry two", 22 );
581 glutAddMenuEntry( "Entry three", 23 );
582 glutAddMenuEntry( "Entry four", 24 );
583 glutAddMenuEntry( "Entry five", 25 );
584 glutAddSubMenu( "Enter sub menu A", subMenuA );
585 glutAddSubMenu( "Enter sub menu B", subMenuB );
586 glutMenuDestroyFunc ( MenuDestroy ); /* callback specific to this menu */
588 glutAttachMenu( GLUT_LEFT_BUTTON );
590 glutInitWindowPosition ( 140+500+2*glutGet(GLUT_WINDOW_BORDER_WIDTH), 140 );
591 /* Position second window right next to the first */
592 windows[1] = glutCreateWindow( "Second Window" );
593 printf ( "Creating window %d as 'Second Window'\n", windows[1] ) ;
595 glClearColor(1.0, 1.0, 1.0, 1.0);
597 /* callbacks and settings specific to this window */
598 glutDisplayFunc( Display );
599 glutReshapeFunc( Reshape );
600 glutKeyboardFunc( Key );
601 glutSpecialFunc( Special );
602 glutVisibilityFunc( Visibility );
603 glutKeyboardUpFunc( KeyUp );
604 glutSpecialUpFunc( SpecialUp );
605 /* glutJoystickFunc( Joystick, 100 ); */
606 glutMouseFunc ( Mouse ) ;
607 glutMouseWheelFunc ( MouseWheel ) ;
608 glutMotionFunc ( Motion ) ;
609 glutPassiveMotionFunc ( PassiveMotion ) ;
610 glutEntryFunc ( Entry ) ;
611 glutCloseFunc ( Close ) ;
612 glutOverlayDisplayFunc ( OverlayDisplay ) ;
613 glutWindowStatusFunc ( WindowStatus ) ;
614 glutSpaceballMotionFunc ( SpaceMotion ) ;
615 glutSpaceballRotateFunc ( SpaceRotation ) ;
616 glutSpaceballButtonFunc ( SpaceButton ) ;
617 glutButtonBoxFunc ( ButtonBox ) ;
618 glutDialsFunc ( Dials ) ;
619 glutTabletMotionFunc ( TabletMotion ) ;
620 glutTabletButtonFunc ( TabletButton ) ;
621 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
623 glutSetMenu(subMenuB);
624 glutAttachMenu( GLUT_RIGHT_BUTTON );
627 printf ( "Please enter something to continue: " );
628 fgets ( dummy_string, STRING_LENGTH, stdin );
632 printf ( "Back from the 'freeglut' main loop\n" ) ;