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