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_0V(name) int name##_seq[2] = {-1}; CALLBACK_CALLED_VAR(name);
19 #define CALLBACK_1V(name,field) int name##_##field[2] = {-1}; CALLBACK_0V(name);
20 #define CALLBACK_2V(name,field1,field2) int name##_##field2[2] = {-1}; CALLBACK_1V(name,field1);
21 #define CALLBACK_3V(name,field1,field2,field3) int name##_##field3[2] = {-1}; CALLBACK_2V(name,field1,field2);
22 #define CALLBACK_4V(name,field1,field2,field3,field4) int name##_##field4[2] = {-1}; CALLBACK_3V(name,field1,field2,field3);
23 CALLBACK_2V(reshape,width,height);
24 CALLBACK_2V(position,top,left);
25 CALLBACK_3V(key,key,x,y);
26 CALLBACK_3V(keyup,key,x,y);
27 CALLBACK_3V(special,key,x,y);
28 CALLBACK_3V(specialup,key,x,y);
29 CALLBACK_1V(visibility,vis);
30 CALLBACK_4V(joystick,a,b,c,d);
31 CALLBACK_4V(mouse,button,updown,x,y);
32 CALLBACK_4V(mousewheel,number,direction,x,y);
33 CALLBACK_2V(motion,x,y);
34 CALLBACK_2V(passivemotion,x,y);
37 /* menudestroy is registered on each menu, not a window */
38 int menudestroy_called = 0 ;
39 /* menustatus and menustate are global callbacks, set for all menus at the same time */
40 int menustatus_called = 0;
41 int menustate_called = 0;
43 #define STRING_LENGTH 10
46 bitmapPrintf (const char *fmt, ...)
52 #if defined(WIN32) && !defined(__CYGWIN__)
53 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
55 (void) vsnprintf (buf, sizeof(buf), fmt, args);
58 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
62 getWindowAndIdx(int *winIdx)
64 int window = glutGetWindow();
67 (*winIdx) = window==windows[0]?0:1;
76 int window = getWindowAndIdx(&winIdx);
77 glClear ( GL_COLOR_BUFFER_BIT );
79 glDisable ( GL_DEPTH_TEST );
80 glMatrixMode ( GL_PROJECTION );
83 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
84 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
85 glMatrixMode ( GL_MODELVIEW );
88 glColor3ub ( 0, 0, 0 );
89 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 20 ); /* 10pt margin above 10pt letters */
91 if ( reshape_called[winIdx] )
93 bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq[winIdx], reshape_width[winIdx], reshape_height[winIdx] );
96 if ( position_called[winIdx] )
98 bitmapPrintf ( "Position %d: %d %d\n", position_seq[winIdx], position_left[winIdx], position_top[winIdx] );
101 if ( key_called[winIdx] )
103 bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq[winIdx], key_key[winIdx], key_key[winIdx], key_x[winIdx], key_y[winIdx] );
106 if ( special_called[winIdx] )
108 bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq[winIdx], special_key[winIdx], special_key[winIdx], special_x[winIdx], special_y[winIdx] );
111 if ( visibility_called[winIdx] )
113 bitmapPrintf ( "Visibility %d: %d\n", visibility_seq[winIdx], visibility_vis[winIdx] );
116 if ( keyup_called[winIdx] )
118 bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq[winIdx], keyup_key[winIdx], keyup_key[winIdx], keyup_x[winIdx], keyup_y[winIdx] );
121 if ( specialup_called[winIdx] )
123 bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq[winIdx], specialup_key[winIdx], specialup_key[winIdx], specialup_x[winIdx], specialup_y[winIdx] );
126 if ( joystick_called[winIdx] )
128 bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq[winIdx], joystick_a[winIdx], joystick_b[winIdx], joystick_c[winIdx], joystick_d[winIdx] );
131 if ( mouse_called[winIdx] )
133 bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq[winIdx], mouse_button[winIdx], mouse_updown[winIdx], mouse_x[winIdx], mouse_y[winIdx] );
136 if ( mousewheel_called[winIdx] )
138 bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq[winIdx], mousewheel_number[winIdx], mousewheel_direction[winIdx], mousewheel_x[winIdx], mousewheel_y[winIdx] );
141 if ( motion_called[winIdx] )
143 bitmapPrintf ( "Motion %d: %d %d\n", motion_seq[winIdx], motion_x[winIdx], motion_y[winIdx] );
146 if ( passivemotion_called[winIdx] )
148 bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq[winIdx], passivemotion_x[winIdx], passivemotion_y[winIdx] );
151 glMatrixMode ( GL_PROJECTION );
153 glMatrixMode ( GL_MODELVIEW );
155 glEnable ( GL_DEPTH_TEST );
157 printf ( "%6d Window %d Display Callback\n",
158 ++sequence_number, window ) ;
163 Warning(const char *fmt, va_list ap)
165 printf("%6d Warning callback:\n");
167 /* print warning message */
172 Error(const char *fmt, va_list ap)
174 char dummy_string[STRING_LENGTH];
175 printf("%6d Error callback:\n");
177 /* print warning message */
180 /* terminate program, after pause for input so user can see */
181 printf ( "Please enter something to exit: " );
182 fgets ( dummy_string, STRING_LENGTH, stdin );
184 /* Call exit directly as freeglut is messed
185 * up internally when an error is called.
191 Reshape(int width, int height)
194 int window = getWindowAndIdx(&winIdx);
195 printf ( "%6d Window %d Reshape Callback: %d %d\n",
196 ++sequence_number, window, width, height ) ;
197 reshape_called[winIdx] = 1 ;
198 reshape_width[winIdx] = width ;
199 reshape_height[winIdx] = height ;
200 reshape_seq[winIdx] = sequence_number ;
201 glViewport(0,0,width,height);
202 glutPostRedisplay () ;
206 Position(int left, int top)
209 int window = getWindowAndIdx(&winIdx);
210 printf ( "%6d Window %d Reshape Callback: %d %d\n",
211 ++sequence_number, window, left, top ) ;
212 position_called[winIdx] = 1 ;
213 position_left[winIdx] = left ;
214 position_top[winIdx] = top ;
215 position_seq[winIdx] = sequence_number ;
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 glutPositionFunc( Position );
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 subMenuA = glutCreateMenu( MenuCallback );
567 glutAddMenuEntry( "Sub menu A1 (01)", 11 );
568 glutAddMenuEntry( "Sub menu A2 (02)", 12 );
569 glutAddMenuEntry( "Sub menu A3 (03)", 13 );
570 glutMenuDestroyFunc ( MenuDestroy ); /* callback specific to this menu */
572 subMenuB = glutCreateMenu( MenuCallback );
573 glutAddMenuEntry( "Sub menu B1 (04)", 14 );
574 glutAddMenuEntry( "Sub menu B2 (05)", 15 );
575 glutAddMenuEntry( "Sub menu B3 (06)", 16 );
576 glutAddSubMenu( "Going to sub menu A", subMenuA );
577 glutMenuDestroyFunc ( MenuDestroy ); /* callback specific to this menu */
579 menuID = glutCreateMenu( MenuCallback );
580 glutAddMenuEntry( "Entry one", 21 );
581 glutAddMenuEntry( "Entry two", 22 );
582 glutAddMenuEntry( "Entry three", 23 );
583 glutAddMenuEntry( "Entry four", 24 );
584 glutAddMenuEntry( "Entry five", 25 );
585 glutAddSubMenu( "Enter sub menu A", subMenuA );
586 glutAddSubMenu( "Enter sub menu B", subMenuB );
587 glutMenuDestroyFunc ( MenuDestroy ); /* callback specific to this menu */
589 glutAttachMenu( GLUT_LEFT_BUTTON );
591 glutInitWindowPosition ( 140+500+2*glutGet(GLUT_WINDOW_BORDER_WIDTH), 140 );
592 /* Position second window right next to the first */
593 windows[1] = glutCreateWindow( "Second Window" );
594 printf ( "Creating window %d as 'Second Window'\n", windows[1] ) ;
596 glClearColor(1.0, 1.0, 1.0, 1.0);
598 /* callbacks and settings specific to this window */
599 glutDisplayFunc( Display );
600 glutReshapeFunc( Reshape );
601 glutPositionFunc( Position );
602 glutKeyboardFunc( Key );
603 glutSpecialFunc( Special );
604 glutVisibilityFunc( Visibility );
605 glutKeyboardUpFunc( KeyUp );
606 glutSpecialUpFunc( SpecialUp );
607 /* glutJoystickFunc( Joystick, 100 ); */
608 glutMouseFunc ( Mouse ) ;
609 glutMouseWheelFunc ( MouseWheel ) ;
610 glutMotionFunc ( Motion ) ;
611 glutPassiveMotionFunc ( PassiveMotion ) ;
612 glutEntryFunc ( Entry ) ;
613 glutCloseFunc ( Close ) ;
614 glutOverlayDisplayFunc ( OverlayDisplay ) ;
615 glutWindowStatusFunc ( WindowStatus ) ;
616 glutSpaceballMotionFunc ( SpaceMotion ) ;
617 glutSpaceballRotateFunc ( SpaceRotation ) ;
618 glutSpaceballButtonFunc ( SpaceButton ) ;
619 glutButtonBoxFunc ( ButtonBox ) ;
620 glutDialsFunc ( Dials ) ;
621 glutTabletMotionFunc ( TabletMotion ) ;
622 glutTabletButtonFunc ( TabletButton ) ;
623 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
625 glutSetMenu(subMenuB);
626 glutAttachMenu( GLUT_RIGHT_BUTTON );
629 printf ( "Please enter something to continue: " );
630 fgets ( dummy_string, STRING_LENGTH, stdin );
634 printf ( "Back from the 'freeglut' main loop\n" ) ;