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 ;
34 bitmapPrintf (const char *fmt, ...)
40 #if defined(WIN32) && !defined(__CYGWIN__)
41 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
43 (void) vsnprintf (buf, sizeof(buf), fmt, args);
46 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
53 int window = glutGetWindow () ;
54 glClear ( GL_COLOR_BUFFER_BIT );
56 glDisable ( GL_DEPTH_TEST );
57 glMatrixMode ( GL_PROJECTION );
60 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
61 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
62 glMatrixMode ( GL_MODELVIEW );
65 glColor3ub ( 0, 0, 0 );
66 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
70 bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height );
75 bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
80 bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
83 if ( visibility_called )
85 bitmapPrintf ( "Visibility %d: %d\n", visibility_seq, visibility_vis );
90 bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
93 if ( specialup_called )
95 bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
98 if ( joystick_called )
100 bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
105 bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
108 if ( mousewheel_called )
110 bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
115 bitmapPrintf ( "Motion %d: %d %d\n", motion_seq, motion_x, motion_y );
118 if ( passivemotion_called )
120 bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
123 glMatrixMode ( GL_PROJECTION );
125 glMatrixMode ( GL_MODELVIEW );
127 glEnable ( GL_DEPTH_TEST );
129 printf ( "%6d Window %d Display Callback\n",
130 ++sequence_number, window ) ;
135 Warning(const char *fmt, va_list ap)
137 printf("%6d Warning callback:\n");
139 /* print warning message */
144 Error(const char *fmt, va_list ap)
146 #define STRING_LENGTH 10
147 char dummy_string[STRING_LENGTH];
148 printf("%6d Error callback:\n");
150 /* print warning message */
153 /* deInitialize the freeglut state */
154 /* This all goes haywire of course when the error occurs during deinitialize,
155 * one might want to call exit directly as its possible freeglut is messed
156 * up internally when error is called.
158 printf("Error callback: calling glutExit() to prepare for clean exit\n");
161 /* terminate program */
162 printf ( "Please enter something to exit: " );
163 fgets ( dummy_string, STRING_LENGTH, stdin );
169 Reshape(int width, int height)
171 int window = glutGetWindow () ;
172 printf ( "%6d Window %d Reshape Callback: %d %d\n",
173 ++sequence_number, window, width, height ) ;
175 reshape_width = width ;
176 reshape_height = height ;
177 reshape_seq = sequence_number ;
178 glutPostRedisplay () ;
182 Key(unsigned char key, int x, int y)
184 int window = glutGetWindow () ;
185 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
186 ++sequence_number, window, key, x, y ) ;
191 key_seq = sequence_number ;
192 glutPostRedisplay () ;
196 Special(int key, int x, int y)
198 int window = glutGetWindow () ;
199 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
200 ++sequence_number, window, key, x, y ) ;
205 special_seq = sequence_number ;
206 glutPostRedisplay () ;
212 int window = glutGetWindow () ;
213 printf ( "%6d Window %d Visibility Callback: %d\n",
214 ++sequence_number, window, vis ) ;
215 visibility_called = 1 ;
216 visibility_vis = vis ;
217 visibility_seq = sequence_number ;
218 glutPostRedisplay () ;
222 KeyUp(unsigned char key, int x, int y)
224 int window = glutGetWindow () ;
225 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
226 ++sequence_number, window, key, x, y ) ;
231 keyup_seq = sequence_number ;
232 glutPostRedisplay () ;
236 SpecialUp(int key, int x, int y)
238 int window = glutGetWindow () ;
239 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
240 ++sequence_number, window, key, x, y ) ;
241 specialup_called = 1 ;
242 specialup_key = key ;
245 specialup_seq = sequence_number ;
246 glutPostRedisplay () ;
250 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
252 int window = glutGetWindow () ;
253 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
254 ++sequence_number, window, a, b, c, d ) ;
255 joystick_called = 1 ;
260 joystick_seq = sequence_number ;
261 glutPostRedisplay () ;
265 Mouse(int button, int updown, int x, int y)
267 int window = glutGetWindow () ;
268 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
269 ++sequence_number, window, button, updown, x, y ) ;
271 mouse_button = button ;
272 mouse_updown = updown ;
275 mouse_seq = sequence_number ;
276 glutPostRedisplay () ;
280 MouseWheel(int wheel_number, int direction, int x, int y)
282 int window = glutGetWindow () ;
283 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
284 ++sequence_number, window, wheel_number, direction, x, y ) ;
285 mousewheel_called = 1 ;
286 mousewheel_number = wheel_number ;
287 mousewheel_direction = direction ;
290 mousewheel_seq = sequence_number ;
291 glutPostRedisplay () ;
297 int window = glutGetWindow () ;
298 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
299 ++sequence_number, window, x, y ) ;
303 motion_seq = sequence_number ;
304 glutPostRedisplay () ;
308 PassiveMotion(int x, int y)
310 int window = glutGetWindow () ;
311 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
312 ++sequence_number, window, x, y ) ;
313 passivemotion_called = 1 ;
314 passivemotion_x = x ;
315 passivemotion_y = y ;
316 passivemotion_seq = sequence_number ;
317 glutPostRedisplay () ;
323 int window = glutGetWindow () ;
325 printf ( "%6d Window %d Entry Callback: %d\n",
326 ++sequence_number, window, state ) ;
327 glutPostRedisplay () ;
333 int window = glutGetWindow () ;
335 printf ( "%6d Window %d Close Callback\n",
336 ++sequence_number, window ) ;
342 int window = glutGetWindow () ;
343 overlaydisplay_called = 1 ;
344 printf ( "%6d Window %d OverlayDisplay Callback\n",
345 ++sequence_number, window ) ;
346 glutPostRedisplay () ;
350 WindowStatus(int state)
352 int window = glutGetWindow () ;
353 windowstatus_called = 1 ;
354 printf ( "%6d Window %d WindowStatus Callback: %d\n",
355 ++sequence_number, window, state ) ;
356 glutPostRedisplay () ;
360 SpaceMotion(int x, int y, int z)
362 int window = glutGetWindow () ;
363 spacemotion_called = 1 ;
364 printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n",
365 ++sequence_number, window, x, y, z ) ;
366 glutPostRedisplay () ;
370 SpaceRotation(int x, int y, int z)
372 int window = glutGetWindow () ;
373 spacerotation_called = 1 ;
374 printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n",
375 ++sequence_number, window, x, y, z ) ;
376 glutPostRedisplay () ;
380 SpaceButton(int button, int updown)
382 int window = glutGetWindow () ;
383 spacebutton_called = 1 ;
384 printf ( "%6d Window %d SpaceButton Callback: %d %d\n",
385 ++sequence_number, window, button, updown ) ;
386 glutPostRedisplay () ;
392 int window = glutGetWindow () ;
394 printf ( "%6d Window %d Dials Callback: %d %d\n",
395 ++sequence_number, window, x, y ) ;
396 glutPostRedisplay () ;
400 ButtonBox(int button, int updown)
402 int window = glutGetWindow () ;
403 buttonbox_called = 1 ;
404 printf ( "%6d Window %d ButtonBox Callback: %d %d\n",
405 ++sequence_number, window, button, updown ) ;
406 glutPostRedisplay () ;
410 TabletMotion(int x, int y)
412 int window = glutGetWindow () ;
413 tabletmotion_called = 1 ;
414 printf ( "%6d Window %d TabletMotion Callback: %d %d\n",
415 ++sequence_number, window, x, y ) ;
416 glutPostRedisplay () ;
420 TabletButton(int button, int updown, int x, int y)
422 int window = glutGetWindow () ;
423 tabletbutton_called = 1 ;
424 printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n",
425 ++sequence_number, window, button, updown, x, y ) ;
426 glutPostRedisplay () ;
430 MenuCallback ( int menuID )
432 int window = glutGetWindow () ;
433 printf( "%6d Window %d MenuCallback - menuID is %d\n",
434 ++sequence_number, window, menuID );
440 int window = glutGetWindow () ;
441 menudestroy_called = 1 ;
442 printf ( "%6d Window %d MenuDestroy Callback\n",
443 ++sequence_number, window ) ;
445 if (window) /* When destroyed when shutting down, not always a window defined... */
446 glutPostRedisplay () ;
450 MenuStatus( int status, int x, int y )
452 int window = glutGetWindow () ;
453 menudestroy_called = 1 ;
454 printf ( "%6d Window %d MenuStatus Callback: %d %d %d\n",
455 ++sequence_number, window, status, x, y ) ;
456 glutPostRedisplay () ;
459 static void Idle ( void )
465 main(int argc, char *argv[])
467 #define STRING_LENGTH 10
468 int freeglut_window, aux_window ;
469 char dummy_string[STRING_LENGTH];
471 int menuID, subMenuA, subMenuB;
473 glutInitWarningFunc(Warning);
474 glutInitErrorFunc(Error);
475 glutInitWindowSize(500, 250);
476 glutInitWindowPosition ( 140, 140 );
477 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
478 glutInit(&argc, argv);
480 freeglut_window = glutCreateWindow( "Callback Demo" );
481 printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
483 glClearColor(1.0, 1.0, 1.0, 1.0);
485 glutDisplayFunc( Display );
486 glutReshapeFunc( Reshape );
487 glutKeyboardFunc( Key );
488 glutSpecialFunc( Special );
489 glutVisibilityFunc( Visibility );
490 glutKeyboardUpFunc( KeyUp );
491 glutSpecialUpFunc( SpecialUp );
492 glutJoystickFunc( Joystick, 100 );
493 glutMouseFunc ( Mouse ) ;
494 glutMouseWheelFunc ( MouseWheel ) ;
495 glutMotionFunc ( Motion ) ;
496 glutPassiveMotionFunc ( PassiveMotion ) ;
497 glutEntryFunc ( Entry ) ;
498 glutCloseFunc ( Close ) ;
499 glutOverlayDisplayFunc ( OverlayDisplay ) ;
500 glutWindowStatusFunc ( WindowStatus ) ;
501 glutSpaceballMotionFunc ( SpaceMotion ) ;
502 glutSpaceballRotateFunc ( SpaceRotation ) ;
503 glutSpaceballButtonFunc ( SpaceButton ) ;
504 glutButtonBoxFunc ( ButtonBox ) ;
505 glutDialsFunc ( Dials ) ;
506 glutTabletMotionFunc ( TabletMotion ) ;
507 glutTabletButtonFunc ( TabletButton ) ;
508 glutMenuStatusFunc ( MenuStatus );
509 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
511 subMenuA = glutCreateMenu( MenuCallback );
512 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
513 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
514 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
515 glutMenuDestroyFunc ( MenuDestroy );
517 subMenuB = glutCreateMenu( MenuCallback );
518 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
519 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
520 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
521 glutAddSubMenu( "Going to sub menu A", subMenuA );
522 glutMenuDestroyFunc ( MenuDestroy );
524 menuID = glutCreateMenu( MenuCallback );
525 glutAddMenuEntry( "Entry one", 1 );
526 glutAddMenuEntry( "Entry two", 2 );
527 glutAddMenuEntry( "Entry three", 3 );
528 glutAddMenuEntry( "Entry four", 4 );
529 glutAddMenuEntry( "Entry five", 5 );
530 glutAddSubMenu( "Enter sub menu A", subMenuA );
531 glutAddSubMenu( "Enter sub menu B", subMenuB );
532 glutMenuDestroyFunc ( MenuDestroy );
534 glutAttachMenu( GLUT_LEFT_BUTTON );
536 aux_window = glutCreateWindow( "Second Window" );
537 printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
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. */