2d310623902c7c04bd227d5fd759ac83f666db69
[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     /* terminate program, after pause for input so user can see */
155     printf ( "Please enter something to exit: " );
156     fgets ( dummy_string, STRING_LENGTH, stdin );
157
158     /* Call exit directly as freeglut is messed
159      * up internally when an error is called. 
160      */
161     exit(1);
162 }
163
164 static void 
165 Reshape(int width, int height)
166 {
167   int window = glutGetWindow () ;
168   printf ( "%6d Window %d Reshape Callback:  %d %d\n",
169             ++sequence_number, window, width, height ) ;
170   reshape_called = 1 ;
171   reshape_width = width ;
172   reshape_height = height ;
173   reshape_seq = sequence_number ;
174   glutPostRedisplay () ;
175 }
176
177 static void 
178 Key(unsigned char key, int x, int y)
179 {
180   int window = glutGetWindow () ;
181   printf ( "%6d Window %d Keyboard Callback:  %d %d %d\n",
182             ++sequence_number, window, key, x, y ) ;
183   key_called = 1 ;
184   key_key = key ;
185   key_x = x ;
186   key_y = y ;
187   key_seq = sequence_number ;
188   glutPostRedisplay () ;
189 }
190
191 static void 
192 Special(int key, int x, int y)
193 {
194   int window = glutGetWindow () ;
195   printf ( "%6d Window %d Special Key Callback:  %d %d %d\n",
196             ++sequence_number, window, key, x, y ) ;
197   special_called = 1 ;
198   special_key = key ;
199   special_x = x ;
200   special_y = y ;
201   special_seq = sequence_number ;
202   glutPostRedisplay () ;
203 }
204
205 static void 
206 Visibility(int vis)
207 {
208   int window = glutGetWindow () ;
209   printf ( "%6d Window %d Visibility Callback:  %d\n",
210             ++sequence_number, window, vis ) ;
211   visibility_called = 1 ;
212   visibility_vis = vis ;
213   visibility_seq = sequence_number ;
214   glutPostRedisplay () ;
215 }
216
217 static void 
218 KeyUp(unsigned char key, int x, int y)
219 {
220   int window = glutGetWindow () ;
221   printf ( "%6d Window %d Key Release Callback:  %d %d %d\n",
222             ++sequence_number, window, key, x, y ) ;
223   keyup_called = 1 ;
224   keyup_key = key ;
225   keyup_x = x ;
226   keyup_y = y ;
227   keyup_seq = sequence_number ;
228   glutPostRedisplay () ;
229 }
230
231 static void 
232 SpecialUp(int key, int x, int y)
233 {
234   int window = glutGetWindow () ;
235   printf ( "%6d Window %d Special Key Release Callback:  %d %d %d\n",
236             ++sequence_number, window, key, x, y ) ;
237   specialup_called = 1 ;
238   specialup_key = key ;
239   specialup_x = x ;
240   specialup_y = y ;
241   specialup_seq = sequence_number ;
242   glutPostRedisplay () ;
243 }
244
245 static void 
246 Joystick( unsigned int a, int b, int c, int d)  /* Need meaningful names */
247 {
248   int window = glutGetWindow () ;
249   printf ( "%6d Window %d Joystick Callback:  %d %d %d %d\n",
250             ++sequence_number, window, a, b, c, d ) ;
251   joystick_called = 1 ;
252   joystick_a = a ;
253   joystick_b = b ;
254   joystick_c = c ;
255   joystick_d = d ;
256   joystick_seq = sequence_number ;
257   glutPostRedisplay () ;
258 }
259
260 static void 
261 Mouse(int button, int updown, int x, int y)
262 {
263   int window = glutGetWindow () ;
264   printf ( "%6d Window %d Mouse Click Callback:  %d %d %d %d\n",
265             ++sequence_number, window, button, updown, x, y ) ;
266   mouse_called = 1 ;
267   mouse_button = button ;
268   mouse_updown = updown ;
269   mouse_x = x ;
270   mouse_y = y ;
271   mouse_seq = sequence_number ;
272   glutPostRedisplay () ;
273 }
274
275 static void 
276 MouseWheel(int wheel_number, int direction, int x, int y)
277 {
278   int window = glutGetWindow () ;
279   printf ( "%6d Window %d Mouse Wheel Callback:  %d %d %d %d\n",
280             ++sequence_number, window, wheel_number, direction, x, y ) ;
281   mousewheel_called = 1 ;
282   mousewheel_number = wheel_number ;
283   mousewheel_direction = direction ;
284   mousewheel_x = x ;
285   mousewheel_y = y ;
286   mousewheel_seq = sequence_number ;
287   glutPostRedisplay () ;
288 }
289
290 static void 
291 Motion(int x, int y)
292 {
293   int window = glutGetWindow () ;
294   printf ( "%6d Window %d Mouse Motion Callback:  %d %d\n",
295             ++sequence_number, window, x, y ) ;
296   motion_called = 1 ;
297   motion_x = x ;
298   motion_y = y ;
299   motion_seq = sequence_number ;
300   glutPostRedisplay () ;
301 }
302
303 static void 
304 PassiveMotion(int x, int y)
305 {
306   int window = glutGetWindow () ;
307   printf ( "%6d Window %d Mouse Passive Motion Callback:  %d %d\n",
308             ++sequence_number, window, x, y ) ;
309   passivemotion_called = 1 ;
310   passivemotion_x = x ;
311   passivemotion_y = y ;
312   passivemotion_seq = sequence_number ;
313   glutPostRedisplay () ;
314 }
315
316 static void 
317 Entry(int state)
318 {
319   int window = glutGetWindow () ;
320   entry_called = 1 ;
321   printf ( "%6d Window %d Entry Callback:  %d\n",
322             ++sequence_number, window, state ) ;
323   glutPostRedisplay () ;
324 }
325
326 static void 
327 Close(void)
328 {
329   int window = glutGetWindow () ;
330   close_called = 1 ;
331   printf ( "%6d Window %d Close Callback\n",
332             ++sequence_number, window ) ;
333 }
334
335 static void 
336 OverlayDisplay(void)
337 {
338   int window = glutGetWindow () ;
339   overlaydisplay_called = 1 ;
340   printf ( "%6d Window %d OverlayDisplay Callback\n",
341             ++sequence_number, window ) ;
342   glutPostRedisplay () ;
343 }
344
345 static void 
346 WindowStatus(int state)
347 {
348   int window = glutGetWindow () ;
349   windowstatus_called = 1 ;
350   printf ( "%6d Window %d WindowStatus Callback:  %d\n",
351             ++sequence_number, window, state ) ;
352   glutPostRedisplay () ;
353 }
354
355 static void 
356 SpaceMotion(int x, int y, int z)
357 {
358   int window = glutGetWindow () ;
359   spacemotion_called = 1 ;
360   printf ( "%6d Window %d SpaceMotion Callback:  %d %d %d\n",
361             ++sequence_number, window, x, y, z ) ;
362   glutPostRedisplay () ;
363 }
364
365 static void 
366 SpaceRotation(int x, int y, int z)
367 {
368   int window = glutGetWindow () ;
369   spacerotation_called = 1 ;
370   printf ( "%6d Window %d SpaceRotation Callback:  %d %d %d\n",
371             ++sequence_number, window, x, y, z ) ;
372   glutPostRedisplay () ;
373 }
374
375 static void 
376 SpaceButton(int button, int updown)
377 {
378   int window = glutGetWindow () ;
379   spacebutton_called = 1 ;
380   printf ( "%6d Window %d SpaceButton Callback:  %d %d\n",
381             ++sequence_number, window, button, updown ) ;
382   glutPostRedisplay () ;
383 }
384
385 static void 
386 Dials(int x, int y)
387 {
388   int window = glutGetWindow () ;
389   dials_called = 1 ;
390   printf ( "%6d Window %d Dials Callback:  %d %d\n",
391             ++sequence_number, window, x, y ) ;
392   glutPostRedisplay () ;
393 }
394
395 static void 
396 ButtonBox(int button, int updown)
397 {
398   int window = glutGetWindow () ;
399   buttonbox_called = 1 ;
400   printf ( "%6d Window %d ButtonBox Callback:  %d %d\n",
401             ++sequence_number, window, button, updown ) ;
402   glutPostRedisplay () ;
403 }
404
405 static void 
406 TabletMotion(int x, int y)
407 {
408   int window = glutGetWindow () ;
409   tabletmotion_called = 1 ;
410   printf ( "%6d Window %d TabletMotion Callback:  %d %d\n",
411             ++sequence_number, window, x, y ) ;
412   glutPostRedisplay () ;
413 }
414
415 static void 
416 TabletButton(int button, int updown, int x, int y)
417 {
418   int window = glutGetWindow () ;
419   tabletbutton_called = 1 ;
420   printf ( "%6d Window %d TabletButton Callback:  %d %d %d %d\n",
421             ++sequence_number, window, button, updown, x, y ) ;
422   glutPostRedisplay () ;
423 }
424
425 static void
426 MenuCallback ( int menuID )
427 {
428   int window = glutGetWindow () ;
429   printf( "%6d Window %d MenuCallback - menuID is %d\n",
430           ++sequence_number, window, menuID );
431 }
432
433 static void 
434 MenuDestroy( void )
435 {
436   int window = glutGetWindow () ;
437   menudestroy_called = 1 ;
438   printf ( "%6d Window %d MenuDestroy Callback\n",
439             ++sequence_number, window ) ;
440
441   if (window)   /* When destroyed when shutting down, not always a window defined... */
442     glutPostRedisplay () ;
443 }
444
445 static void 
446 MenuStatus( int status, int x, int y )
447 {
448   int window = glutGetWindow () ;
449   menudestroy_called = 1 ;
450   printf ( "%6d Window %d MenuStatus Callback:  %d %d %d\n",
451             ++sequence_number, window, status, x, y ) ;
452   glutPostRedisplay () ;
453 }
454
455 static void Idle ( void )
456 {
457   ++sequence_number ;
458 }
459
460 int 
461 main(int argc, char *argv[])
462 {
463   int freeglut_window, aux_window ;
464   char dummy_string[STRING_LENGTH];
465
466   int menuID, subMenuA, subMenuB;
467
468   glutInitWarningFunc(Warning);
469   glutInitErrorFunc(Error);
470   glutInitWindowSize(500, 250);
471   glutInitWindowPosition ( 140, 140 );
472   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
473   glutInit(&argc, argv);
474   glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
475
476   freeglut_window = glutCreateWindow( "Callback Demo" );
477   printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
478
479   glClearColor(1.0, 1.0, 1.0, 1.0);
480
481   glutDisplayFunc( Display );
482   glutReshapeFunc( Reshape );
483   glutKeyboardFunc( Key );
484   glutSpecialFunc( Special );
485   glutVisibilityFunc( Visibility );
486   glutKeyboardUpFunc( KeyUp );
487   glutSpecialUpFunc( SpecialUp );
488   glutJoystickFunc( Joystick, 100 );
489   glutMouseFunc ( Mouse ) ;
490   glutMouseWheelFunc ( MouseWheel ) ;
491   glutMotionFunc ( Motion ) ;
492   glutPassiveMotionFunc ( PassiveMotion ) ;
493   glutEntryFunc ( Entry ) ;
494   glutCloseFunc ( Close ) ;
495   glutOverlayDisplayFunc ( OverlayDisplay ) ;
496   glutWindowStatusFunc ( WindowStatus ) ;
497   glutSpaceballMotionFunc ( SpaceMotion ) ;
498   glutSpaceballRotateFunc ( SpaceRotation ) ;
499   glutSpaceballButtonFunc ( SpaceButton ) ;
500   glutButtonBoxFunc ( ButtonBox ) ;
501   glutDialsFunc ( Dials ) ;
502   glutTabletMotionFunc ( TabletMotion ) ;
503   glutTabletButtonFunc ( TabletButton ) ;
504   glutMenuStatusFunc ( MenuStatus );
505   glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
506
507   subMenuA = glutCreateMenu( MenuCallback );
508   glutAddMenuEntry( "Sub menu A1 (01)", 1 );
509   glutAddMenuEntry( "Sub menu A2 (02)", 2 );
510   glutAddMenuEntry( "Sub menu A3 (03)", 3 );
511   glutMenuDestroyFunc ( MenuDestroy );
512
513   subMenuB = glutCreateMenu( MenuCallback );
514   glutAddMenuEntry( "Sub menu B1 (04)", 4 );
515   glutAddMenuEntry( "Sub menu B2 (05)", 5 );
516   glutAddMenuEntry( "Sub menu B3 (06)", 6 );
517   glutAddSubMenu( "Going to sub menu A", subMenuA );
518   glutMenuDestroyFunc ( MenuDestroy );
519
520   menuID = glutCreateMenu( MenuCallback );
521   glutAddMenuEntry( "Entry one",   1 );
522   glutAddMenuEntry( "Entry two",   2 );
523   glutAddMenuEntry( "Entry three", 3 );
524   glutAddMenuEntry( "Entry four",  4 );
525   glutAddMenuEntry( "Entry five",  5 );
526   glutAddSubMenu( "Enter sub menu A", subMenuA );
527   glutAddSubMenu( "Enter sub menu B", subMenuB );
528   glutMenuDestroyFunc ( MenuDestroy );
529
530   glutAttachMenu( GLUT_LEFT_BUTTON );
531
532   aux_window = glutCreateWindow( "Second Window" );
533   printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
534
535   glClearColor(1.0, 1.0, 1.0, 1.0);
536
537   glutDisplayFunc( Display );
538   glutReshapeFunc( Reshape );
539   glutKeyboardFunc( Key );
540   glutSpecialFunc( Special );
541   glutVisibilityFunc( Visibility );
542   glutKeyboardUpFunc( KeyUp );
543   glutSpecialUpFunc( SpecialUp );
544   /*  glutJoystickFunc( Joystick, 100 ); */
545   glutMouseFunc ( Mouse ) ;
546   glutMouseWheelFunc ( MouseWheel ) ;
547   glutMotionFunc ( Motion ) ;
548   glutPassiveMotionFunc ( PassiveMotion ) ;
549   glutEntryFunc ( Entry ) ;
550   glutCloseFunc ( Close ) ;
551   glutOverlayDisplayFunc ( OverlayDisplay ) ;
552   glutWindowStatusFunc ( WindowStatus ) ;
553   glutSpaceballMotionFunc ( SpaceMotion ) ;
554   glutSpaceballRotateFunc ( SpaceRotation ) ;
555   glutSpaceballButtonFunc ( SpaceButton ) ;
556   glutButtonBoxFunc ( ButtonBox ) ;
557   glutDialsFunc ( Dials ) ;
558   glutTabletMotionFunc ( TabletMotion ) ;
559   glutTabletButtonFunc ( TabletButton ) ;
560   glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
561
562   glutIdleFunc ( Idle );
563
564   printf ( "Please enter something to continue: " );
565   fgets ( dummy_string, STRING_LENGTH, stdin );
566
567   glutMainLoop();
568
569   printf ( "Back from the 'freeglut' main loop\n" ) ;
570
571   return 0;             /* ANSI C requires main to return int. */
572 }