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 (void) vsnprintf (buf, sizeof(buf), fmt, args);
42 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
49 int window = glutGetWindow () ;
50 glClear ( GL_COLOR_BUFFER_BIT );
52 glDisable ( GL_DEPTH_TEST );
53 glMatrixMode ( GL_PROJECTION );
56 glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ),
57 0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
58 glMatrixMode ( GL_MODELVIEW );
61 glColor3ub ( 0, 0, 0 );
62 glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
66 bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height );
71 bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
76 bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
79 if ( visibility_called )
81 bitmapPrintf ( "Visibility %d: %d\n", visibility_seq, visibility_vis );
86 bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
89 if ( specialup_called )
91 bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
94 if ( joystick_called )
96 bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
101 bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
104 if ( mousewheel_called )
106 bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
111 bitmapPrintf ( "Motion %d: %d %d\n", motion_seq, motion_x, motion_y );
114 if ( passivemotion_called )
116 bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
119 glMatrixMode ( GL_PROJECTION );
121 glMatrixMode ( GL_MODELVIEW );
123 glEnable ( GL_DEPTH_TEST );
125 printf ( "%6d Window %d Display Callback\n",
126 ++sequence_number, window ) ;
131 Reshape(int width, int height)
133 int window = glutGetWindow () ;
134 printf ( "%6d Window %d Reshape Callback: %d %d\n",
135 ++sequence_number, window, width, height ) ;
137 reshape_width = width ;
138 reshape_height = height ;
139 reshape_seq = sequence_number ;
140 glutPostRedisplay () ;
144 Key(unsigned char key, int x, int y)
146 int window = glutGetWindow () ;
147 printf ( "%6d Window %d Keyboard Callback: %d %d %d\n",
148 ++sequence_number, window, key, x, y ) ;
153 key_seq = sequence_number ;
154 glutPostRedisplay () ;
158 Special(int key, int x, int y)
160 int window = glutGetWindow () ;
161 printf ( "%6d Window %d Special Key Callback: %d %d %d\n",
162 ++sequence_number, window, key, x, y ) ;
167 special_seq = sequence_number ;
168 glutPostRedisplay () ;
174 int window = glutGetWindow () ;
175 printf ( "%6d Window %d Visibility Callback: %d\n",
176 ++sequence_number, window, vis ) ;
177 visibility_called = 1 ;
178 visibility_vis = vis ;
179 visibility_seq = sequence_number ;
180 glutPostRedisplay () ;
184 KeyUp(unsigned char key, int x, int y)
186 int window = glutGetWindow () ;
187 printf ( "%6d Window %d Key Release Callback: %d %d %d\n",
188 ++sequence_number, window, key, x, y ) ;
193 keyup_seq = sequence_number ;
194 glutPostRedisplay () ;
198 SpecialUp(int key, int x, int y)
200 int window = glutGetWindow () ;
201 printf ( "%6d Window %d Special Key Release Callback: %d %d %d\n",
202 ++sequence_number, window, key, x, y ) ;
203 specialup_called = 1 ;
204 specialup_key = key ;
207 specialup_seq = sequence_number ;
208 glutPostRedisplay () ;
212 Joystick( unsigned int a, int b, int c, int d) /* Need meaningful names */
214 int window = glutGetWindow () ;
215 printf ( "%6d Window %d Joystick Callback: %d %d %d %d\n",
216 ++sequence_number, window, a, b, c, d ) ;
217 joystick_called = 1 ;
222 joystick_seq = sequence_number ;
223 glutPostRedisplay () ;
227 Mouse(int button, int updown, int x, int y)
229 int window = glutGetWindow () ;
230 printf ( "%6d Window %d Mouse Click Callback: %d %d %d %d\n",
231 ++sequence_number, window, button, updown, x, y ) ;
233 mouse_button = button ;
234 mouse_updown = updown ;
237 mouse_seq = sequence_number ;
238 glutPostRedisplay () ;
242 MouseWheel(int wheel_number, int direction, int x, int y)
244 int window = glutGetWindow () ;
245 printf ( "%6d Window %d Mouse Wheel Callback: %d %d %d %d\n",
246 ++sequence_number, window, wheel_number, direction, x, y ) ;
247 mousewheel_called = 1 ;
248 mousewheel_number = wheel_number ;
249 mousewheel_direction = direction ;
252 mousewheel_seq = sequence_number ;
253 glutPostRedisplay () ;
259 int window = glutGetWindow () ;
260 printf ( "%6d Window %d Mouse Motion Callback: %d %d\n",
261 ++sequence_number, window, x, y ) ;
265 motion_seq = sequence_number ;
266 glutPostRedisplay () ;
270 PassiveMotion(int x, int y)
272 int window = glutGetWindow () ;
273 printf ( "%6d Window %d Mouse Passive Motion Callback: %d %d\n",
274 ++sequence_number, window, x, y ) ;
275 passivemotion_called = 1 ;
276 passivemotion_x = x ;
277 passivemotion_y = y ;
278 passivemotion_seq = sequence_number ;
279 glutPostRedisplay () ;
285 int window = glutGetWindow () ;
287 printf ( "%6d Window %d Entry Callback: %d\n",
288 ++sequence_number, window, state ) ;
289 glutPostRedisplay () ;
295 int window = glutGetWindow () ;
297 printf ( "%6d Window %d Close Callback\n",
298 ++sequence_number, window ) ;
304 int window = glutGetWindow () ;
305 overlaydisplay_called = 1 ;
306 printf ( "%6d Window %d OverlayDisplay Callback\n",
307 ++sequence_number, window ) ;
308 glutPostRedisplay () ;
312 WindowStatus(int state)
314 int window = glutGetWindow () ;
315 windowstatus_called = 1 ;
316 printf ( "%6d Window %d WindowStatus Callback: %d\n",
317 ++sequence_number, window, state ) ;
318 glutPostRedisplay () ;
322 SpaceMotion(int x, int y, int z)
324 int window = glutGetWindow () ;
325 spacemotion_called = 1 ;
326 printf ( "%6d Window %d SpaceMotion Callback: %d %d %d\n",
327 ++sequence_number, window, x, y, z ) ;
328 glutPostRedisplay () ;
332 SpaceRotation(int x, int y, int z)
334 int window = glutGetWindow () ;
335 spacerotation_called = 1 ;
336 printf ( "%6d Window %d SpaceRotation Callback: %d %d %d\n",
337 ++sequence_number, window, x, y, z ) ;
338 glutPostRedisplay () ;
342 SpaceButton(int button, int updown)
344 int window = glutGetWindow () ;
345 spacebutton_called = 1 ;
346 printf ( "%6d Window %d SpaceButton Callback: %d %d\n",
347 ++sequence_number, window, button, updown ) ;
348 glutPostRedisplay () ;
354 int window = glutGetWindow () ;
356 printf ( "%6d Window %d Dials Callback: %d %d\n",
357 ++sequence_number, window, x, y ) ;
358 glutPostRedisplay () ;
362 ButtonBox(int button, int updown)
364 int window = glutGetWindow () ;
365 buttonbox_called = 1 ;
366 printf ( "%6d Window %d ButtonBox Callback: %d %d\n",
367 ++sequence_number, window, button, updown ) ;
368 glutPostRedisplay () ;
372 TabletMotion(int x, int y)
374 int window = glutGetWindow () ;
375 tabletmotion_called = 1 ;
376 printf ( "%6d Window %d TabletMotion Callback: %d %d\n",
377 ++sequence_number, window, x, y ) ;
378 glutPostRedisplay () ;
382 TabletButton(int button, int updown, int x, int y)
384 int window = glutGetWindow () ;
385 tabletbutton_called = 1 ;
386 printf ( "%6d Window %d TabletButton Callback: %d %d %d %d\n",
387 ++sequence_number, window, button, updown, x, y ) ;
388 glutPostRedisplay () ;
392 MenuCallback ( int menuID )
394 int window = glutGetWindow () ;
395 printf( "%6d Window %d MenuCallback - menuID is %d\n",
396 ++sequence_number, window, menuID );
402 int window = glutGetWindow () ;
403 menudestroy_called = 1 ;
404 printf ( "%6d Window %d MenuDestroy Callback\n",
405 ++sequence_number, window ) ;
406 glutPostRedisplay () ;
410 MenuStatus( int status, int x, int y )
412 int window = glutGetWindow () ;
413 menudestroy_called = 1 ;
414 printf ( "%6d Window %d MenuStatus Callback: %d %d %d\n",
415 ++sequence_number, window, status, x, y ) ;
416 glutPostRedisplay () ;
419 static void Idle ( void )
425 main(int argc, char *argv[])
427 #define STRING_LENGTH 10
428 int freeglut_window, aux_window ;
429 char dummy_string[STRING_LENGTH];
431 int menuID, subMenuA, subMenuB;
433 glutInitWindowSize(500, 250);
434 glutInitWindowPosition ( 140, 140 );
435 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
436 glutInit(&argc, argv);
438 freeglut_window = glutCreateWindow( "Callback Demo" );
439 printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
441 glClearColor(1.0, 1.0, 1.0, 1.0);
443 glutDisplayFunc( Display );
444 glutReshapeFunc( Reshape );
445 glutKeyboardFunc( Key );
446 glutSpecialFunc( Special );
447 glutVisibilityFunc( Visibility );
448 glutKeyboardUpFunc( KeyUp );
449 glutSpecialUpFunc( SpecialUp );
450 glutJoystickFunc( Joystick, 100 );
451 glutMouseFunc ( Mouse ) ;
452 glutMouseWheelFunc ( MouseWheel ) ;
453 glutMotionFunc ( Motion ) ;
454 glutPassiveMotionFunc ( PassiveMotion ) ;
455 glutEntryFunc ( Entry ) ;
456 glutCloseFunc ( Close ) ;
457 glutOverlayDisplayFunc ( OverlayDisplay ) ;
458 glutWindowStatusFunc ( WindowStatus ) ;
459 glutSpaceballMotionFunc ( SpaceMotion ) ;
460 glutSpaceballRotateFunc ( SpaceRotation ) ;
461 glutSpaceballButtonFunc ( SpaceButton ) ;
462 glutButtonBoxFunc ( ButtonBox ) ;
463 glutDialsFunc ( Dials ) ;
464 glutTabletMotionFunc ( TabletMotion ) ;
465 glutTabletButtonFunc ( TabletButton ) ;
466 glutMenuDestroyFunc ( MenuDestroy );
467 glutMenuStatusFunc ( MenuStatus );
468 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
470 subMenuA = glutCreateMenu( MenuCallback );
471 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
472 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
473 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
475 subMenuB = glutCreateMenu( MenuCallback );
476 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
477 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
478 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
479 glutAddSubMenu( "Going to sub menu A", subMenuA );
481 menuID = glutCreateMenu( MenuCallback );
482 glutAddMenuEntry( "Entry one", 1 );
483 glutAddMenuEntry( "Entry two", 2 );
484 glutAddMenuEntry( "Entry three", 3 );
485 glutAddMenuEntry( "Entry four", 4 );
486 glutAddMenuEntry( "Entry five", 5 );
487 glutAddSubMenu( "Enter sub menu A", subMenuA );
488 glutAddSubMenu( "Enter sub menu B", subMenuB );
490 glutAttachMenu( GLUT_LEFT_BUTTON );
492 aux_window = glutCreateWindow( "Second Window" );
493 printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
495 glClearColor(1.0, 1.0, 1.0, 1.0);
497 glutDisplayFunc( Display );
498 glutReshapeFunc( Reshape );
499 glutKeyboardFunc( Key );
500 glutSpecialFunc( Special );
501 glutVisibilityFunc( Visibility );
502 glutKeyboardUpFunc( KeyUp );
503 glutSpecialUpFunc( SpecialUp );
504 /* glutJoystickFunc( Joystick, 100 ); */
505 glutMouseFunc ( Mouse ) ;
506 glutMouseWheelFunc ( MouseWheel ) ;
507 glutMotionFunc ( Motion ) ;
508 glutPassiveMotionFunc ( PassiveMotion ) ;
509 glutEntryFunc ( Entry ) ;
510 glutCloseFunc ( Close ) ;
511 glutOverlayDisplayFunc ( OverlayDisplay ) ;
512 glutWindowStatusFunc ( WindowStatus ) ;
513 glutSpaceballMotionFunc ( SpaceMotion ) ;
514 glutSpaceballRotateFunc ( SpaceRotation ) ;
515 glutSpaceballButtonFunc ( SpaceButton ) ;
516 glutButtonBoxFunc ( ButtonBox ) ;
517 glutDialsFunc ( Dials ) ;
518 glutTabletMotionFunc ( TabletMotion ) ;
519 glutTabletButtonFunc ( TabletButton ) ;
520 glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
522 glutIdleFunc ( Idle );
524 printf ( "Please enter something to continue: " );
525 fgets ( dummy_string, STRING_LENGTH, stdin );
529 printf ( "Back from the 'freeglut' main loop\n" ) ;
531 return 0; /* ANSI C requires main to return int. */