little bit of cleanup and extra comments
[freeglut] / progs / demos / CallbackMaker / CallbackMaker.c
1 /* CallbackMaker.c */
2 /*
3  * Program to invoke all the callbacks that "freeglut" supports
4  */
5
6
7 #include <GL/freeglut.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdarg.h>
11
12 static int sequence_number = 0 ;
13
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 ;
32
33 #define STRING_LENGTH   10
34
35 static void
36 bitmapPrintf (const char *fmt, ...)
37 {
38     static char buf[256];
39     va_list args;
40
41     va_start(args, fmt);
42 #if defined(WIN32) && !defined(__CYGWIN__)
43     (void) _vsnprintf (buf, sizeof(buf), fmt, args);
44 #else
45     (void) vsnprintf (buf, sizeof(buf), fmt, args);
46 #endif
47     va_end(args);
48     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
49 }
50
51
52 static void 
53 Display(void)
54 {
55   int window = glutGetWindow () ;
56   glClear ( GL_COLOR_BUFFER_BIT );
57
58   glDisable ( GL_DEPTH_TEST );
59   glMatrixMode ( GL_PROJECTION );
60   glPushMatrix();
61   glLoadIdentity();
62   glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ), 
63           0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
64   glMatrixMode ( GL_MODELVIEW );
65   glPushMatrix ();
66   glLoadIdentity ();
67   glColor3ub ( 0, 0, 0 );
68   glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
69
70   if ( reshape_called )
71   {
72     bitmapPrintf ( "Reshape %d:  %d %d\n", reshape_seq, reshape_width, reshape_height );
73   }
74
75   if ( key_called )
76   {
77     bitmapPrintf ( "Key %d:  %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
78   }
79
80   if ( special_called )
81   {
82     bitmapPrintf ( "Special %d:  %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
83   }
84
85   if ( visibility_called )
86   {
87     bitmapPrintf ( "Visibility %d:  %d\n", visibility_seq, visibility_vis );
88   }
89
90   if ( keyup_called )
91   {
92     bitmapPrintf ( "Key Up %d:  %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
93   }
94
95   if ( specialup_called )
96   {
97     bitmapPrintf ( "Special Up %d:  %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
98   }
99
100   if ( joystick_called )
101   {
102     bitmapPrintf ( "Joystick %d:  %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
103   }
104
105   if ( mouse_called )
106   {
107     bitmapPrintf ( "Mouse %d:  %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
108   }
109
110   if ( mousewheel_called )
111   {
112     bitmapPrintf ( "Mouse Wheel %d:  %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
113   }
114
115   if ( motion_called )
116   {
117     bitmapPrintf ( "Motion %d:  %d %d\n", motion_seq, motion_x, motion_y );
118   }
119
120   if ( passivemotion_called )
121   {
122     bitmapPrintf ( "Passive Motion %d:  %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
123   }
124
125   glMatrixMode ( GL_PROJECTION );
126   glPopMatrix ();
127   glMatrixMode ( GL_MODELVIEW );
128   glPopMatrix ();
129   glEnable ( GL_DEPTH_TEST );
130
131   printf ( "%6d Window %d Display Callback\n",
132             ++sequence_number, window ) ;
133   glutSwapBuffers();
134 }
135
136 static void
137 Warning(const char *fmt, va_list ap)
138 {
139     printf("%6d Warning callback:\n");
140
141     /* print warning message */
142     vprintf(fmt, ap);
143 }
144
145 static void
146 Error(const char *fmt, va_list ap)
147 {
148     char dummy_string[STRING_LENGTH];
149     printf("%6d Error callback:\n");
150
151     /* print warning message */
152     vprintf(fmt, ap);
153
154     /* deInitialize the freeglut state */
155     /* This all goes haywire of course when the error occurs during deinitialize,
156      * one might want to call exit directly as its possible freeglut is messed
157      * up internally when error is called. 
158      */
159     printf("Error callback: calling glutExit() to prepare for clean exit\n");
160     glutExit();
161
162     /* terminate program */
163     printf ( "Please enter something to exit: " );
164     fgets ( dummy_string, STRING_LENGTH, stdin );
165
166     exit(1);
167 }
168
169 static void 
170 Reshape(int width, int height)
171 {
172   int window = glutGetWindow () ;
173   printf ( "%6d Window %d Reshape Callback:  %d %d\n",
174             ++sequence_number, window, width, height ) ;
175   reshape_called = 1 ;
176   reshape_width = width ;
177   reshape_height = height ;
178   reshape_seq = sequence_number ;
179   glutPostRedisplay () ;
180 }
181
182 static void 
183 Key(unsigned char key, int x, int y)
184 {
185   int window = glutGetWindow () ;
186   printf ( "%6d Window %d Keyboard Callback:  %d %d %d\n",
187             ++sequence_number, window, key, x, y ) ;
188   key_called = 1 ;
189   key_key = key ;
190   key_x = x ;
191   key_y = y ;
192   key_seq = sequence_number ;
193   glutPostRedisplay () ;
194 }
195
196 static void 
197 Special(int key, int x, int y)
198 {
199   int window = glutGetWindow () ;
200   printf ( "%6d Window %d Special Key Callback:  %d %d %d\n",
201             ++sequence_number, window, key, x, y ) ;
202   special_called = 1 ;
203   special_key = key ;
204   special_x = x ;
205   special_y = y ;
206   special_seq = sequence_number ;
207   glutPostRedisplay () ;
208 }
209
210 static void 
211 Visibility(int vis)
212 {
213   int window = glutGetWindow () ;
214   printf ( "%6d Window %d Visibility Callback:  %d\n",
215             ++sequence_number, window, vis ) ;
216   visibility_called = 1 ;
217   visibility_vis = vis ;
218   visibility_seq = sequence_number ;
219   glutPostRedisplay () ;
220 }
221
222 static void 
223 KeyUp(unsigned char key, int x, int y)
224 {
225   int window = glutGetWindow () ;
226   printf ( "%6d Window %d Key Release Callback:  %d %d %d\n",
227             ++sequence_number, window, key, x, y ) ;
228   keyup_called = 1 ;
229   keyup_key = key ;
230   keyup_x = x ;
231   keyup_y = y ;
232   keyup_seq = sequence_number ;
233   glutPostRedisplay () ;
234 }
235
236 static void 
237 SpecialUp(int key, int x, int y)
238 {
239   int window = glutGetWindow () ;
240   printf ( "%6d Window %d Special Key Release Callback:  %d %d %d\n",
241             ++sequence_number, window, key, x, y ) ;
242   specialup_called = 1 ;
243   specialup_key = key ;
244   specialup_x = x ;
245   specialup_y = y ;
246   specialup_seq = sequence_number ;
247   glutPostRedisplay () ;
248 }
249
250 static void 
251 Joystick( unsigned int a, int b, int c, int d)  /* Need meaningful names */
252 {
253   int window = glutGetWindow () ;
254   printf ( "%6d Window %d Joystick Callback:  %d %d %d %d\n",
255             ++sequence_number, window, a, b, c, d ) ;
256   joystick_called = 1 ;
257   joystick_a = a ;
258   joystick_b = b ;
259   joystick_c = c ;
260   joystick_d = d ;
261   joystick_seq = sequence_number ;
262   glutPostRedisplay () ;
263 }
264
265 static void 
266 Mouse(int button, int updown, int x, int y)
267 {
268   int window = glutGetWindow () ;
269   printf ( "%6d Window %d Mouse Click Callback:  %d %d %d %d\n",
270             ++sequence_number, window, button, updown, x, y ) ;
271   mouse_called = 1 ;
272   mouse_button = button ;
273   mouse_updown = updown ;
274   mouse_x = x ;
275   mouse_y = y ;
276   mouse_seq = sequence_number ;
277   glutPostRedisplay () ;
278 }
279
280 static void 
281 MouseWheel(int wheel_number, int direction, int x, int y)
282 {
283   int window = glutGetWindow () ;
284   printf ( "%6d Window %d Mouse Wheel Callback:  %d %d %d %d\n",
285             ++sequence_number, window, wheel_number, direction, x, y ) ;
286   mousewheel_called = 1 ;
287   mousewheel_number = wheel_number ;
288   mousewheel_direction = direction ;
289   mousewheel_x = x ;
290   mousewheel_y = y ;
291   mousewheel_seq = sequence_number ;
292   glutPostRedisplay () ;
293 }
294
295 static void 
296 Motion(int x, int y)
297 {
298   int window = glutGetWindow () ;
299   printf ( "%6d Window %d Mouse Motion Callback:  %d %d\n",
300             ++sequence_number, window, x, y ) ;
301   motion_called = 1 ;
302   motion_x = x ;
303   motion_y = y ;
304   motion_seq = sequence_number ;
305   glutPostRedisplay () ;
306 }
307
308 static void 
309 PassiveMotion(int x, int y)
310 {
311   int window = glutGetWindow () ;
312   printf ( "%6d Window %d Mouse Passive Motion Callback:  %d %d\n",
313             ++sequence_number, window, x, y ) ;
314   passivemotion_called = 1 ;
315   passivemotion_x = x ;
316   passivemotion_y = y ;
317   passivemotion_seq = sequence_number ;
318   glutPostRedisplay () ;
319 }
320
321 static void 
322 Entry(int state)
323 {
324   int window = glutGetWindow () ;
325   entry_called = 1 ;
326   printf ( "%6d Window %d Entry Callback:  %d\n",
327             ++sequence_number, window, state ) ;
328   glutPostRedisplay () ;
329 }
330
331 static void 
332 Close(void)
333 {
334   int window = glutGetWindow () ;
335   close_called = 1 ;
336   printf ( "%6d Window %d Close Callback\n",
337             ++sequence_number, window ) ;
338 }
339
340 static void 
341 OverlayDisplay(void)
342 {
343   int window = glutGetWindow () ;
344   overlaydisplay_called = 1 ;
345   printf ( "%6d Window %d OverlayDisplay Callback\n",
346             ++sequence_number, window ) ;
347   glutPostRedisplay () ;
348 }
349
350 static void 
351 WindowStatus(int state)
352 {
353   int window = glutGetWindow () ;
354   windowstatus_called = 1 ;
355   printf ( "%6d Window %d WindowStatus Callback:  %d\n",
356             ++sequence_number, window, state ) ;
357   glutPostRedisplay () ;
358 }
359
360 static void 
361 SpaceMotion(int x, int y, int z)
362 {
363   int window = glutGetWindow () ;
364   spacemotion_called = 1 ;
365   printf ( "%6d Window %d SpaceMotion Callback:  %d %d %d\n",
366             ++sequence_number, window, x, y, z ) ;
367   glutPostRedisplay () ;
368 }
369
370 static void 
371 SpaceRotation(int x, int y, int z)
372 {
373   int window = glutGetWindow () ;
374   spacerotation_called = 1 ;
375   printf ( "%6d Window %d SpaceRotation Callback:  %d %d %d\n",
376             ++sequence_number, window, x, y, z ) ;
377   glutPostRedisplay () ;
378 }
379
380 static void 
381 SpaceButton(int button, int updown)
382 {
383   int window = glutGetWindow () ;
384   spacebutton_called = 1 ;
385   printf ( "%6d Window %d SpaceButton Callback:  %d %d\n",
386             ++sequence_number, window, button, updown ) ;
387   glutPostRedisplay () ;
388 }
389
390 static void 
391 Dials(int x, int y)
392 {
393   int window = glutGetWindow () ;
394   dials_called = 1 ;
395   printf ( "%6d Window %d Dials Callback:  %d %d\n",
396             ++sequence_number, window, x, y ) ;
397   glutPostRedisplay () ;
398 }
399
400 static void 
401 ButtonBox(int button, int updown)
402 {
403   int window = glutGetWindow () ;
404   buttonbox_called = 1 ;
405   printf ( "%6d Window %d ButtonBox Callback:  %d %d\n",
406             ++sequence_number, window, button, updown ) ;
407   glutPostRedisplay () ;
408 }
409
410 static void 
411 TabletMotion(int x, int y)
412 {
413   int window = glutGetWindow () ;
414   tabletmotion_called = 1 ;
415   printf ( "%6d Window %d TabletMotion Callback:  %d %d\n",
416             ++sequence_number, window, x, y ) ;
417   glutPostRedisplay () ;
418 }
419
420 static void 
421 TabletButton(int button, int updown, int x, int y)
422 {
423   int window = glutGetWindow () ;
424   tabletbutton_called = 1 ;
425   printf ( "%6d Window %d TabletButton Callback:  %d %d %d %d\n",
426             ++sequence_number, window, button, updown, x, y ) ;
427   glutPostRedisplay () ;
428 }
429
430 static void
431 MenuCallback ( int menuID )
432 {
433   int window = glutGetWindow () ;
434   printf( "%6d Window %d MenuCallback - menuID is %d\n",
435           ++sequence_number, window, menuID );
436 }
437
438 static void 
439 MenuDestroy( void )
440 {
441   int window = glutGetWindow () ;
442   menudestroy_called = 1 ;
443   printf ( "%6d Window %d MenuDestroy Callback\n",
444             ++sequence_number, window ) ;
445
446   if (window)   /* When destroyed when shutting down, not always a window defined... */
447     glutPostRedisplay () ;
448 }
449
450 static void 
451 MenuStatus( int status, int x, int y )
452 {
453   int window = glutGetWindow () ;
454   menudestroy_called = 1 ;
455   printf ( "%6d Window %d MenuStatus Callback:  %d %d %d\n",
456             ++sequence_number, window, status, x, y ) ;
457   glutPostRedisplay () ;
458 }
459
460 static void Idle ( void )
461 {
462   ++sequence_number ;
463 }
464
465 int 
466 main(int argc, char *argv[])
467 {
468   int freeglut_window, aux_window ;
469   char dummy_string[STRING_LENGTH];
470
471   int menuID, subMenuA, subMenuB;
472
473   glutInitWarningFunc(Warning);
474   glutInitErrorFunc(Error);
475   glutInitWindowSize(500, 250);
476   glutInitWindowPosition ( 140, 140 );
477   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
478   glutInit(&argc, argv);
479   glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
480
481   freeglut_window = glutCreateWindow( "Callback Demo" );
482   printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
483
484   glClearColor(1.0, 1.0, 1.0, 1.0);
485
486   glutDisplayFunc( Display );
487   glutReshapeFunc( Reshape );
488   glutKeyboardFunc( Key );
489   glutSpecialFunc( Special );
490   glutVisibilityFunc( Visibility );
491   glutKeyboardUpFunc( KeyUp );
492   glutSpecialUpFunc( SpecialUp );
493   glutJoystickFunc( Joystick, 100 );
494   glutMouseFunc ( Mouse ) ;
495   glutMouseWheelFunc ( MouseWheel ) ;
496   glutMotionFunc ( Motion ) ;
497   glutPassiveMotionFunc ( PassiveMotion ) ;
498   glutEntryFunc ( Entry ) ;
499   glutCloseFunc ( Close ) ;
500   glutOverlayDisplayFunc ( OverlayDisplay ) ;
501   glutWindowStatusFunc ( WindowStatus ) ;
502   glutSpaceballMotionFunc ( SpaceMotion ) ;
503   glutSpaceballRotateFunc ( SpaceRotation ) ;
504   glutSpaceballButtonFunc ( SpaceButton ) ;
505   glutButtonBoxFunc ( ButtonBox ) ;
506   glutDialsFunc ( Dials ) ;
507   glutTabletMotionFunc ( TabletMotion ) ;
508   glutTabletButtonFunc ( TabletButton ) ;
509   glutMenuStatusFunc ( MenuStatus );
510   glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
511
512   subMenuA = glutCreateMenu( MenuCallback );
513   glutAddMenuEntry( "Sub menu A1 (01)", 1 );
514   glutAddMenuEntry( "Sub menu A2 (02)", 2 );
515   glutAddMenuEntry( "Sub menu A3 (03)", 3 );
516   glutMenuDestroyFunc ( MenuDestroy );
517
518   subMenuB = glutCreateMenu( MenuCallback );
519   glutAddMenuEntry( "Sub menu B1 (04)", 4 );
520   glutAddMenuEntry( "Sub menu B2 (05)", 5 );
521   glutAddMenuEntry( "Sub menu B3 (06)", 6 );
522   glutAddSubMenu( "Going to sub menu A", subMenuA );
523   glutMenuDestroyFunc ( MenuDestroy );
524
525   menuID = glutCreateMenu( MenuCallback );
526   glutAddMenuEntry( "Entry one",   1 );
527   glutAddMenuEntry( "Entry two",   2 );
528   glutAddMenuEntry( "Entry three", 3 );
529   glutAddMenuEntry( "Entry four",  4 );
530   glutAddMenuEntry( "Entry five",  5 );
531   glutAddSubMenu( "Enter sub menu A", subMenuA );
532   glutAddSubMenu( "Enter sub menu B", subMenuB );
533   glutMenuDestroyFunc ( MenuDestroy );
534
535   glutAttachMenu( GLUT_LEFT_BUTTON );
536
537   aux_window = glutCreateWindow( "Second Window" );
538   printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
539
540   glClearColor(1.0, 1.0, 1.0, 1.0);
541
542   glutDisplayFunc( Display );
543   glutReshapeFunc( Reshape );
544   glutKeyboardFunc( Key );
545   glutSpecialFunc( Special );
546   glutVisibilityFunc( Visibility );
547   glutKeyboardUpFunc( KeyUp );
548   glutSpecialUpFunc( SpecialUp );
549   /*  glutJoystickFunc( Joystick, 100 ); */
550   glutMouseFunc ( Mouse ) ;
551   glutMouseWheelFunc ( MouseWheel ) ;
552   glutMotionFunc ( Motion ) ;
553   glutPassiveMotionFunc ( PassiveMotion ) ;
554   glutEntryFunc ( Entry ) ;
555   glutCloseFunc ( Close ) ;
556   glutOverlayDisplayFunc ( OverlayDisplay ) ;
557   glutWindowStatusFunc ( WindowStatus ) ;
558   glutSpaceballMotionFunc ( SpaceMotion ) ;
559   glutSpaceballRotateFunc ( SpaceRotation ) ;
560   glutSpaceballButtonFunc ( SpaceButton ) ;
561   glutButtonBoxFunc ( ButtonBox ) ;
562   glutDialsFunc ( Dials ) ;
563   glutTabletMotionFunc ( TabletMotion ) ;
564   glutTabletButtonFunc ( TabletButton ) ;
565   glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
566
567   glutIdleFunc ( Idle );
568
569   printf ( "Please enter something to continue: " );
570   fgets ( dummy_string, STRING_LENGTH, stdin );
571
572   glutMainLoop();
573
574   printf ( "Back from the 'freeglut' main loop\n" ) ;
575
576   return 0;             /* ANSI C requires main to return int. */
577 }