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