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 int menudestroy_called = 0 ; /* menudestroy is registered on menu, so don't use above */
58 #define STRING_LENGTH 10
61 bitmapPrintf (const char *fmt, ...)
67 #if defined(WIN32) && !defined(__CYGWIN__)
68 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
70 (void) vsnprintf (buf, sizeof(buf), fmt, args);
73 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
77 getWindowAndIdx(int *winIdx)
79 int window = glutGetWindow();
82 (*winIdx) = window==windows[0]?0:1;
91 int window = getWindowAndIdx(&winIdx);
92 glClear ( GL_COLOR_BUFFER_BIT );
94 glDisable ( GL_DEPTH_TEST );
95 glMatrixMode ( GL_PROJECTION );
98 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
99 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
100 glMatrixMode ( GL_MODELVIEW );
103 glColor3ub ( 0, 0, 0 );
104 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 20 ); /* 10pt margin above 10pt letters */
106 if ( reshape_called[winIdx] )
108 bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq[winIdx], reshape_width[winIdx], reshape_height[winIdx] );
111 if ( key_called[winIdx] )
113 bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq[winIdx], key_key[winIdx], key_key[winIdx], key_x[winIdx], key_y[winIdx] );
116 if ( special_called[winIdx] )
118 bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq[winIdx], special_key[winIdx], special_key[winIdx], special_x[winIdx], special_y[winIdx] );
121 if ( visibility_called[winIdx] )
123 bitmapPrintf ( "Visibility %d: %d\n", visibility_seq[winIdx], visibility_vis[winIdx] );
126 if ( keyup_called[winIdx] )
128 bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq[winIdx], keyup_key[winIdx], keyup_key[winIdx], keyup_x[winIdx], keyup_y[winIdx] );
131 if ( specialup_called[winIdx] )
133 bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq[winIdx], specialup_key[winIdx], specialup_key[winIdx], specialup_x[winIdx], specialup_y[winIdx] );
136 if ( joystick_called[winIdx] )
138 bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq[winIdx], joystick_a[winIdx], joystick_b[winIdx], joystick_c[winIdx], joystick_d[winIdx] );
141 if ( mouse_called[winIdx] )
143 bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq[winIdx], mouse_button[winIdx], mouse_updown[winIdx], mouse_x[winIdx], mouse_y[winIdx] );
146 if ( mousewheel_called[winIdx] )
148 bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq[winIdx], mousewheel_number[winIdx], mousewheel_direction[winIdx], mousewheel_x[winIdx], mousewheel_y[winIdx] );
151 if ( motion_called[winIdx] )
153 bitmapPrintf ( "Motion %d: %d %d\n", motion_seq[winIdx], motion_x[winIdx], motion_y[winIdx] );
156 if ( passivemotion_called[winIdx] )
158 bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq[winIdx], passivemotion_x[winIdx], passivemotion_y[winIdx] );
161 glMatrixMode ( GL_PROJECTION );
163 glMatrixMode ( GL_MODELVIEW );
165 glEnable ( GL_DEPTH_TEST );
167 printf ( "%6d Window %d Display Callback\n",
168 ++sequence_number, window ) ;
173 Warning(const char *fmt, va_list ap)
175 printf("%6d Warning callback:\n");
177 /* print warning message */
182 Error(const char *fmt, va_list ap)
184 char dummy_string[STRING_LENGTH];
185 printf("%6d Error callback:\n");
187 /* print warning message */
190 /* terminate program, after pause for input so user can see */
191 printf ( "Please enter something to exit: " );
192 fgets ( dummy_string, STRING_LENGTH, stdin );
194 /* Call exit directly as freeglut is messed
195 * up internally when an error is called.
201 Reshape(int width, int height)
204 int window = getWindowAndIdx(&winIdx);
205 printf ( "%6d Window %d Reshape Callback: %d %d\n",
206 ++sequence_number, window, width, height ) ;
207 reshape_called[winIdx] = 1 ;
208 reshape_width[winIdx] = width ;
209 reshape_height[winIdx] = height ;
210 reshape_seq[winIdx] = sequence_number ;
211 glViewport(0,0,width,height);
212 glutPostRedisplay () ;
216 Key(unsigned char key, int x, int y)
219 int window = getWindowAndIdx(&winIdx);
220 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
221 ++sequence_number, window, key, x, y ) ;
222 key_called[winIdx] = 1 ;
223 key_key[winIdx] = key ;
226 key_seq[winIdx] = sequence_number ;
227 glutPostRedisplay () ;
231 Special(int key, int x, int y)
234 int window = getWindowAndIdx(&winIdx);
235 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
236 ++sequence_number, window, key, x, y ) ;
237 special_called[winIdx] = 1 ;
238 special_key[winIdx] = key ;
239 special_x[winIdx] = x ;
240 special_y[winIdx] = y ;
241 special_seq[winIdx] = sequence_number ;
242 glutPostRedisplay () ;
249 int window = getWindowAndIdx(&winIdx);
250 printf ( "%6d Window %d Visibility Callback: %d\n",
251 ++sequence_number, window, vis ) ;
252 visibility_called[winIdx] = 1 ;
253 visibility_vis[winIdx] = vis ;
254 visibility_seq[winIdx] = sequence_number ;
255 glutPostRedisplay () ;
259 KeyUp(unsigned char key, int x, int y)
262 int window = getWindowAndIdx(&winIdx);
263 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
264 ++sequence_number, window, key, x, y ) ;
265 keyup_called[winIdx] = 1 ;
266 keyup_key[winIdx] = key ;
267 keyup_x[winIdx] = x ;
268 keyup_y[winIdx] = y ;
269 keyup_seq[winIdx] = sequence_number ;
270 glutPostRedisplay () ;
274 SpecialUp(int key, int x, int y)
277 int window = getWindowAndIdx(&winIdx);
278 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
279 ++sequence_number, window, key, x, y ) ;
280 specialup_called[winIdx] = 1 ;
281 specialup_key[winIdx] = key ;
282 specialup_x[winIdx] = x ;
283 specialup_y[winIdx] = y ;
284 specialup_seq[winIdx] = sequence_number ;
285 glutPostRedisplay () ;
289 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
292 int window = getWindowAndIdx(&winIdx);
293 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
294 ++sequence_number, window, a, b, c, d ) ;
295 joystick_called[winIdx] = 1 ;
296 joystick_a[winIdx] = a ;
297 joystick_b[winIdx] = b ;
298 joystick_c[winIdx] = c ;
299 joystick_d[winIdx] = d ;
300 joystick_seq[winIdx] = sequence_number ;
301 glutPostRedisplay () ;
305 Mouse(int button, int updown, int x, int y)
308 int window = getWindowAndIdx(&winIdx);
309 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
310 ++sequence_number, window, button, updown, x, y ) ;
311 mouse_called[winIdx] = 1 ;
312 mouse_button[winIdx] = button ;
313 mouse_updown[winIdx] = updown ;
314 mouse_x[winIdx] = x ;
315 mouse_y[winIdx] = y ;
316 mouse_seq[winIdx] = sequence_number ;
317 glutPostRedisplay () ;
321 MouseWheel(int wheel_number, int direction, int x, int y)
324 int window = getWindowAndIdx(&winIdx);
325 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
326 ++sequence_number, window, wheel_number, direction, x, y ) ;
327 mousewheel_called[winIdx] = 1 ;
328 mousewheel_number[winIdx] = wheel_number ;
329 mousewheel_direction[winIdx] = direction ;
330 mousewheel_x[winIdx] = x ;
331 mousewheel_y[winIdx] = y ;
332 mousewheel_seq[winIdx] = sequence_number ;
333 glutPostRedisplay () ;
340 int window = getWindowAndIdx(&winIdx);
341 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
342 ++sequence_number, window, x, y ) ;
343 motion_called[winIdx] = 1 ;
344 motion_x[winIdx] = x ;
345 motion_y[winIdx] = y ;
346 motion_seq[winIdx] = sequence_number ;
347 glutPostRedisplay () ;
351 PassiveMotion(int x, int y)
354 int window = getWindowAndIdx(&winIdx);
355 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
356 ++sequence_number, window, x, y ) ;
357 passivemotion_called[winIdx] = 1 ;
358 passivemotion_x[winIdx] = x ;
359 passivemotion_y[winIdx] = y ;
360 passivemotion_seq[winIdx] = sequence_number ;
361 glutPostRedisplay () ;
367 int window = getWindowAndIdx(NULL);
368 printf ( "%6d Window %d Entry Callback: %d\n",
369 ++sequence_number, window, state ) ;
370 glutPostRedisplay () ;
376 int window = getWindowAndIdx(NULL);
377 printf ( "%6d Window %d Close Callback\n",
378 ++sequence_number, window ) ;
384 int window = getWindowAndIdx(NULL);
385 printf ( "%6d Window %d OverlayDisplay Callback\n",
386 ++sequence_number, window ) ;
387 glutPostRedisplay () ;
391 WindowStatus(int state)
393 int window = getWindowAndIdx(NULL);
394 printf ( "%6d Window %d WindowStatus Callback: %d\n",
395 ++sequence_number, window, state ) ;
396 glutPostRedisplay () ;
400 SpaceMotion(int x, int y, int z)
402 int window = getWindowAndIdx(NULL);
403 printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n",
404 ++sequence_number, window, x, y, z ) ;
405 glutPostRedisplay () ;
409 SpaceRotation(int x, int y, int z)
411 int window = getWindowAndIdx(NULL);
412 printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n",
413 ++sequence_number, window, x, y, z ) ;
414 glutPostRedisplay () ;
418 SpaceButton(int button, int updown)
420 int window = getWindowAndIdx(NULL);
421 printf ( "%6d Window %d SpaceButton Callback: %d %d\n",
422 ++sequence_number, window, button, updown ) ;
423 glutPostRedisplay () ;
429 int window = getWindowAndIdx(NULL);
430 printf ( "%6d Window %d Dials Callback: %d %d\n",
431 ++sequence_number, window, x, y ) ;
432 glutPostRedisplay () ;
436 ButtonBox(int button, int updown)
438 int window = getWindowAndIdx(NULL);
439 printf ( "%6d Window %d ButtonBox Callback: %d %d\n",
440 ++sequence_number, window, button, updown ) ;
441 glutPostRedisplay () ;
445 TabletMotion(int x, int y)
447 int window = getWindowAndIdx(NULL);
448 printf ( "%6d Window %d TabletMotion Callback: %d %d\n",
449 ++sequence_number, window, x, y ) ;
450 glutPostRedisplay () ;
454 TabletButton(int button, int updown, int x, int y)
456 int window = getWindowAndIdx(NULL);
457 printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n",
458 ++sequence_number, window, button, updown, x, y ) ;
459 glutPostRedisplay () ;
463 MenuCallback ( int menuID )
465 int window = getWindowAndIdx(NULL);
466 printf( "%6d Window %d MenuCallback - menuID is %d\n",
467 ++sequence_number, window, menuID );
473 int window = getWindowAndIdx(NULL);
474 menudestroy_called = 1 ;
475 printf ( "%6d Window %d MenuDestroy Callback\n",
476 ++sequence_number, window ) ;
478 if (window) /* When destroyed when shutting down, not always a window defined... */
479 glutPostRedisplay () ;
483 MenuStatus( int status, int x, int y )
485 int window = getWindowAndIdx(NULL);
486 menudestroy_called = 1 ;
487 printf ( "%6d Window %d MenuStatus Callback: %d %d %d\n",
488 ++sequence_number, window, status, x, y ) ;
489 glutPostRedisplay () ;
492 static void Idle ( void )
498 main(int argc, char *argv[])
500 char dummy_string[STRING_LENGTH];
502 int menuID, subMenuA, subMenuB;
504 glutInitWarningFunc(Warning);
505 glutInitErrorFunc(Error);
506 glutInitWindowSize(500, 250);
507 glutInitWindowPosition ( 140, 140 );
508 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
509 glutInit(&argc, argv);
510 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
512 windows[0] = glutCreateWindow( "Callback Demo" );
513 printf ( "Creating window %d as 'Callback Demo'\n", windows[0] ) ;
515 glClearColor(1.0, 1.0, 1.0, 1.0);
517 glutDisplayFunc( Display );
518 glutReshapeFunc( Reshape );
519 glutKeyboardFunc( Key );
520 glutSpecialFunc( Special );
521 glutVisibilityFunc( Visibility );
522 glutKeyboardUpFunc( KeyUp );
523 glutSpecialUpFunc( SpecialUp );
524 glutJoystickFunc( Joystick, 100 );
525 glutMouseFunc ( Mouse ) ;
526 glutMouseWheelFunc ( MouseWheel ) ;
527 glutMotionFunc ( Motion ) ;
528 glutPassiveMotionFunc ( PassiveMotion ) ;
529 glutEntryFunc ( Entry ) ;
530 glutCloseFunc ( Close ) ;
531 glutOverlayDisplayFunc ( OverlayDisplay ) ;
532 glutWindowStatusFunc ( WindowStatus ) ;
533 glutSpaceballMotionFunc ( SpaceMotion ) ;
534 glutSpaceballRotateFunc ( SpaceRotation ) ;
535 glutSpaceballButtonFunc ( SpaceButton ) ;
536 glutButtonBoxFunc ( ButtonBox ) ;
537 glutDialsFunc ( Dials ) ;
538 glutTabletMotionFunc ( TabletMotion ) ;
539 glutTabletButtonFunc ( TabletButton ) ;
540 glutMenuStatusFunc ( MenuStatus );
541 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
543 subMenuA = glutCreateMenu( MenuCallback );
544 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
545 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
546 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
547 glutMenuDestroyFunc ( MenuDestroy );
549 subMenuB = glutCreateMenu( MenuCallback );
550 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
551 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
552 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
553 glutAddSubMenu( "Going to sub menu A", subMenuA );
554 glutMenuDestroyFunc ( MenuDestroy );
556 menuID = glutCreateMenu( MenuCallback );
557 glutAddMenuEntry( "Entry one", 1 );
558 glutAddMenuEntry( "Entry two", 2 );
559 glutAddMenuEntry( "Entry three", 3 );
560 glutAddMenuEntry( "Entry four", 4 );
561 glutAddMenuEntry( "Entry five", 5 );
562 glutAddSubMenu( "Enter sub menu A", subMenuA );
563 glutAddSubMenu( "Enter sub menu B", subMenuB );
564 glutMenuDestroyFunc ( MenuDestroy );
566 glutAttachMenu( GLUT_LEFT_BUTTON );
568 glutInitWindowPosition ( 140+500+2*glutGet(GLUT_WINDOW_BORDER_WIDTH), 140 );
569 /* Position second window right next to the first */
570 windows[1] = glutCreateWindow( "Second Window" );
571 printf ( "Creating window %d as 'Second Window'\n", windows[1] ) ;
573 glClearColor(1.0, 1.0, 1.0, 1.0);
575 glutDisplayFunc( Display );
576 glutReshapeFunc( Reshape );
577 glutKeyboardFunc( Key );
578 glutSpecialFunc( Special );
579 glutVisibilityFunc( Visibility );
580 glutKeyboardUpFunc( KeyUp );
581 glutSpecialUpFunc( SpecialUp );
582 /* glutJoystickFunc( Joystick, 100 ); */
583 glutMouseFunc ( Mouse ) ;
584 glutMouseWheelFunc ( MouseWheel ) ;
585 glutMotionFunc ( Motion ) ;
586 glutPassiveMotionFunc ( PassiveMotion ) ;
587 glutEntryFunc ( Entry ) ;
588 glutCloseFunc ( Close ) ;
589 glutOverlayDisplayFunc ( OverlayDisplay ) ;
590 glutWindowStatusFunc ( WindowStatus ) ;
591 glutSpaceballMotionFunc ( SpaceMotion ) ;
592 glutSpaceballRotateFunc ( SpaceRotation ) ;
593 glutSpaceballButtonFunc ( SpaceButton ) ;
594 glutButtonBoxFunc ( ButtonBox ) ;
595 glutDialsFunc ( Dials ) ;
596 glutTabletMotionFunc ( TabletMotion ) ;
597 glutTabletButtonFunc ( TabletButton ) ;
598 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
600 glutIdleFunc ( Idle );
602 printf ( "Please enter something to continue: " );
603 fgets ( dummy_string, STRING_LENGTH, stdin );
607 printf ( "Back from the 'freeglut' main loop\n" ) ;
609 return 0; /* ANSI C requires main to return int. */