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