added warning and error callbacks to demo
[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 static void
34 bitmapPrintf (const char *fmt, ...)
35 {
36     static char buf[256];
37     va_list args;
38
39     va_start(args, fmt);
40 #if defined(WIN32) && !defined(__CYGWIN__)
41     (void) _vsnprintf (buf, sizeof(buf), fmt, args);
42 #else
43     (void) vsnprintf (buf, sizeof(buf), fmt, args);
44 #endif
45     va_end(args);
46     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
47 }
48
49
50 static void 
51 Display(void)
52 {
53   int window = glutGetWindow () ;
54   glClear ( GL_COLOR_BUFFER_BIT );
55
56   glDisable ( GL_DEPTH_TEST );
57   glMatrixMode ( GL_PROJECTION );
58   glPushMatrix();
59   glLoadIdentity();
60   glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ), 
61           0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
62   glMatrixMode ( GL_MODELVIEW );
63   glPushMatrix ();
64   glLoadIdentity ();
65   glColor3ub ( 0, 0, 0 );
66   glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
67
68   if ( reshape_called )
69   {
70     bitmapPrintf ( "Reshape %d:  %d %d\n", reshape_seq, reshape_width, reshape_height );
71   }
72
73   if ( key_called )
74   {
75     bitmapPrintf ( "Key %d:  %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
76   }
77
78   if ( special_called )
79   {
80     bitmapPrintf ( "Special %d:  %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
81   }
82
83   if ( visibility_called )
84   {
85     bitmapPrintf ( "Visibility %d:  %d\n", visibility_seq, visibility_vis );
86   }
87
88   if ( keyup_called )
89   {
90     bitmapPrintf ( "Key Up %d:  %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
91   }
92
93   if ( specialup_called )
94   {
95     bitmapPrintf ( "Special Up %d:  %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
96   }
97
98   if ( joystick_called )
99   {
100     bitmapPrintf ( "Joystick %d:  %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
101   }
102
103   if ( mouse_called )
104   {
105     bitmapPrintf ( "Mouse %d:  %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
106   }
107
108   if ( mousewheel_called )
109   {
110     bitmapPrintf ( "Mouse Wheel %d:  %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
111   }
112
113   if ( motion_called )
114   {
115     bitmapPrintf ( "Motion %d:  %d %d\n", motion_seq, motion_x, motion_y );
116   }
117
118   if ( passivemotion_called )
119   {
120     bitmapPrintf ( "Passive Motion %d:  %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
121   }
122
123   glMatrixMode ( GL_PROJECTION );
124   glPopMatrix ();
125   glMatrixMode ( GL_MODELVIEW );
126   glPopMatrix ();
127   glEnable ( GL_DEPTH_TEST );
128
129   printf ( "%6d Window %d Display Callback\n",
130             ++sequence_number, window ) ;
131   glutSwapBuffers();
132 }
133
134 static void
135 Warning(const char *fmt, va_list ap)
136 {
137     printf("%6d Warning callback:\n");
138
139     /* print warning message */
140     vprintf(fmt, ap);
141 }
142
143 static void
144 Error(const char *fmt, va_list ap)
145 {
146 #define STRING_LENGTH   10
147     char dummy_string[STRING_LENGTH];
148     printf("%6d Error callback:\n");
149
150     /* print warning message */
151     vprintf(fmt, ap);
152
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. 
157      */
158     printf("Error callback: calling glutExit() to prepare for clean exit\n");
159     glutExit();
160
161     /* terminate program */
162     printf ( "Please enter something to exit: " );
163     fgets ( dummy_string, STRING_LENGTH, stdin );
164
165     exit(1);
166 }
167
168 static void 
169 Reshape(int width, int height)
170 {
171   int window = glutGetWindow () ;
172   printf ( "%6d Window %d Reshape Callback:  %d %d\n",
173             ++sequence_number, window, width, height ) ;
174   reshape_called = 1 ;
175   reshape_width = width ;
176   reshape_height = height ;
177   reshape_seq = sequence_number ;
178   glutPostRedisplay () ;
179 }
180
181 static void 
182 Key(unsigned char key, int x, int y)
183 {
184   int window = glutGetWindow () ;
185   printf ( "%6d Window %d Keyboard Callback:  %d %d %d\n",
186             ++sequence_number, window, key, x, y ) ;
187   key_called = 1 ;
188   key_key = key ;
189   key_x = x ;
190   key_y = y ;
191   key_seq = sequence_number ;
192   glutPostRedisplay () ;
193 }
194
195 static void 
196 Special(int key, int x, int y)
197 {
198   int window = glutGetWindow () ;
199   printf ( "%6d Window %d Special Key Callback:  %d %d %d\n",
200             ++sequence_number, window, key, x, y ) ;
201   special_called = 1 ;
202   special_key = key ;
203   special_x = x ;
204   special_y = y ;
205   special_seq = sequence_number ;
206   glutPostRedisplay () ;
207 }
208
209 static void 
210 Visibility(int vis)
211 {
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 () ;
219 }
220
221 static void 
222 KeyUp(unsigned char key, int x, int y)
223 {
224   int window = glutGetWindow () ;
225   printf ( "%6d Window %d Key Release Callback:  %d %d %d\n",
226             ++sequence_number, window, key, x, y ) ;
227   keyup_called = 1 ;
228   keyup_key = key ;
229   keyup_x = x ;
230   keyup_y = y ;
231   keyup_seq = sequence_number ;
232   glutPostRedisplay () ;
233 }
234
235 static void 
236 SpecialUp(int key, int x, int y)
237 {
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 ;
243   specialup_x = x ;
244   specialup_y = y ;
245   specialup_seq = sequence_number ;
246   glutPostRedisplay () ;
247 }
248
249 static void 
250 Joystick( unsigned int a, int b, int c, int d)  /* Need meaningful names */
251 {
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 ;
256   joystick_a = a ;
257   joystick_b = b ;
258   joystick_c = c ;
259   joystick_d = d ;
260   joystick_seq = sequence_number ;
261   glutPostRedisplay () ;
262 }
263
264 static void 
265 Mouse(int button, int updown, int x, int y)
266 {
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 ) ;
270   mouse_called = 1 ;
271   mouse_button = button ;
272   mouse_updown = updown ;
273   mouse_x = x ;
274   mouse_y = y ;
275   mouse_seq = sequence_number ;
276   glutPostRedisplay () ;
277 }
278
279 static void 
280 MouseWheel(int wheel_number, int direction, int x, int y)
281 {
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 ;
288   mousewheel_x = x ;
289   mousewheel_y = y ;
290   mousewheel_seq = sequence_number ;
291   glutPostRedisplay () ;
292 }
293
294 static void 
295 Motion(int x, int y)
296 {
297   int window = glutGetWindow () ;
298   printf ( "%6d Window %d Mouse Motion Callback:  %d %d\n",
299             ++sequence_number, window, x, y ) ;
300   motion_called = 1 ;
301   motion_x = x ;
302   motion_y = y ;
303   motion_seq = sequence_number ;
304   glutPostRedisplay () ;
305 }
306
307 static void 
308 PassiveMotion(int x, int y)
309 {
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 () ;
318 }
319
320 static void 
321 Entry(int state)
322 {
323   int window = glutGetWindow () ;
324   entry_called = 1 ;
325   printf ( "%6d Window %d Entry Callback:  %d\n",
326             ++sequence_number, window, state ) ;
327   glutPostRedisplay () ;
328 }
329
330 static void 
331 Close(void)
332 {
333   int window = glutGetWindow () ;
334   close_called = 1 ;
335   printf ( "%6d Window %d Close Callback\n",
336             ++sequence_number, window ) ;
337 }
338
339 static void 
340 OverlayDisplay(void)
341 {
342   int window = glutGetWindow () ;
343   overlaydisplay_called = 1 ;
344   printf ( "%6d Window %d OverlayDisplay Callback\n",
345             ++sequence_number, window ) ;
346   glutPostRedisplay () ;
347 }
348
349 static void 
350 WindowStatus(int state)
351 {
352   int window = glutGetWindow () ;
353   windowstatus_called = 1 ;
354   printf ( "%6d Window %d WindowStatus Callback:  %d\n",
355             ++sequence_number, window, state ) ;
356   glutPostRedisplay () ;
357 }
358
359 static void 
360 SpaceMotion(int x, int y, int z)
361 {
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 () ;
367 }
368
369 static void 
370 SpaceRotation(int x, int y, int z)
371 {
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 () ;
377 }
378
379 static void 
380 SpaceButton(int button, int updown)
381 {
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 () ;
387 }
388
389 static void 
390 Dials(int x, int y)
391 {
392   int window = glutGetWindow () ;
393   dials_called = 1 ;
394   printf ( "%6d Window %d Dials Callback:  %d %d\n",
395             ++sequence_number, window, x, y ) ;
396   glutPostRedisplay () ;
397 }
398
399 static void 
400 ButtonBox(int button, int updown)
401 {
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 () ;
407 }
408
409 static void 
410 TabletMotion(int x, int y)
411 {
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 () ;
417 }
418
419 static void 
420 TabletButton(int button, int updown, int x, int y)
421 {
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 () ;
427 }
428
429 static void
430 MenuCallback ( int menuID )
431 {
432   int window = glutGetWindow () ;
433   printf( "%6d Window %d MenuCallback - menuID is %d\n",
434           ++sequence_number, window, menuID );
435 }
436
437 static void 
438 MenuDestroy( void )
439 {
440   int window = glutGetWindow () ;
441   menudestroy_called = 1 ;
442   printf ( "%6d Window %d MenuDestroy Callback\n",
443             ++sequence_number, window ) ;
444
445   if (window)   /* When destroyed when shutting down, not always a window defined... */
446     glutPostRedisplay () ;
447 }
448
449 static void 
450 MenuStatus( int status, int x, int y )
451 {
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 () ;
457 }
458
459 static void Idle ( void )
460 {
461   ++sequence_number ;
462 }
463
464 int 
465 main(int argc, char *argv[])
466 {
467 #define STRING_LENGTH   10
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
480   freeglut_window = glutCreateWindow( "Callback Demo" );
481   printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
482
483   glClearColor(1.0, 1.0, 1.0, 1.0);
484
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) ;
510
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 );
516
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 );
523
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 );
533
534   glutAttachMenu( GLUT_LEFT_BUTTON );
535
536   aux_window = glutCreateWindow( "Second Window" );
537   printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
538
539   glClearColor(1.0, 1.0, 1.0, 1.0);
540
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) ;
565
566   glutIdleFunc ( Idle );
567
568   printf ( "Please enter something to continue: " );
569   fgets ( dummy_string, STRING_LENGTH, stdin );
570
571   glutMainLoop();
572
573   printf ( "Back from the 'freeglut' main loop\n" ) ;
574
575   return 0;             /* ANSI C requires main to return int. */
576 }