5b93044d7be2ab76f34bc2b45adcd7040a0c518a
[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 ;
17 int reshape_width = -1, reshape_height = -1, reshape_seq = -1 ;
18 int key_key = -1, key_x = -1, key_y = -1, key_seq = -1 ;
19 int special_key = -1, special_x = -1, special_y = -1, special_seq = -1 ;
20 int visibility_vis = -1, visibility_seq = -1 ;
21 int keyup_key = -1, keyup_x = -1, keyup_y = -1, keyup_seq = -1 ;
22 int specialup_key = -1, specialup_x = -1, specialup_y = -1, specialup_seq = -1 ;
23 int joystick_a = -1, joystick_b = -1, joystick_c = -1, joystick_d = -1, joystick_seq = -1 ;  /* Need meaningful names */
24 int mouse_button = -1, mouse_updown = -1, mouse_x = -1, mouse_y = -1, mouse_seq = -1 ;
25 int mousewheel_number = -1, mousewheel_direction = -1, mousewheel_x = -1, mousewheel_y = -1, mousewheel_seq = -1 ;
26 int motion_x = -1, motion_y = -1, motion_seq = -1 ;
27 int passivemotion_x = -1, passivemotion_y = -1, passivemotion_seq = -1 ;
28 static void 
29 Display(void)
30 {
31   char line[180] ;
32   int window = glutGetWindow () ;
33   glClear ( GL_COLOR_BUFFER_BIT );
34
35   glDisable ( GL_DEPTH_TEST );
36   glMatrixMode ( GL_PROJECTION );
37   glPushMatrix();
38   glLoadIdentity();
39   glOrtho(0, glutGet ( GLUT_WINDOW_WIDTH ), 
40           0, glutGet ( GLUT_WINDOW_HEIGHT ), -1, 1 );
41   glMatrixMode ( GL_MODELVIEW );
42   glPushMatrix ();
43   glLoadIdentity ();
44   glColor3ub ( 0, 0, 0 );
45   glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
46
47   if ( reshape_called )
48   {
49     sprintf ( line, "Reshape %d:  %d %d\n", reshape_seq, reshape_width, reshape_height );
50     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
51   }
52
53   if ( key_called )
54   {
55     sprintf ( line, "Key %d:  %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
56     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
57   }
58
59   if ( special_called )
60   {
61     sprintf ( line, "Special %d:  %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
62     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
63   }
64
65   if ( visibility_called )
66   {
67     sprintf ( line, "Visibility %d:  %d\n", visibility_seq, visibility_vis );
68     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
69   }
70
71   if ( keyup_called )
72   {
73     sprintf ( line, "Key Up %d:  %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
74     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
75   }
76
77   if ( specialup_called )
78   {
79     sprintf ( line, "Special Up %d:  %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
80     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
81   }
82
83   if ( joystick_called )
84   {
85     sprintf ( line, "Joystick %d:  %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
86     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
87   }
88
89   if ( mouse_called )
90   {
91     sprintf ( line, "Mouse %d:  %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
92     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
93   }
94
95   if ( mousewheel_called )
96   {
97     sprintf ( line, "Mouse Wheel %d:  %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
98     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
99   }
100
101   if ( motion_called )
102   {
103     sprintf ( line, "Motion %d:  %d %d\n", motion_seq, motion_x, motion_y );
104     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
105   }
106
107   if ( passivemotion_called )
108   {
109     sprintf ( line, "Passive Motion %d:  %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
110     glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
111   }
112
113   glMatrixMode ( GL_PROJECTION );
114   glPopMatrix ();
115   glMatrixMode ( GL_MODELVIEW );
116   glPopMatrix ();
117   glEnable ( GL_DEPTH_TEST );
118
119   printf ( "%6d Window %d Display Callback\n",
120             ++sequence_number, window ) ;
121   glutSwapBuffers();
122 }
123
124 static void 
125 Reshape(int width, int height)
126 {
127   int window = glutGetWindow () ;
128   printf ( "%6d Window %d Reshape Callback:  %d %d\n",
129             ++sequence_number, window, width, height ) ;
130   reshape_called = 1 ;
131   reshape_width = width ;
132   reshape_height = height ;
133   reshape_seq = sequence_number ;
134   glutPostRedisplay () ;
135 }
136
137 static void 
138 Key(unsigned char key, int x, int y)
139 {
140   int window = glutGetWindow () ;
141   printf ( "%6d Window %d Keyboard Callback:  %d %d %d\n",
142             ++sequence_number, window, key, x, y ) ;
143   key_called = 1 ;
144   key_key = key ;
145   key_x = x ;
146   key_y = y ;
147   key_seq = sequence_number ;
148   glutPostRedisplay () ;
149 }
150
151 static void 
152 Special(int key, int x, int y)
153 {
154   int window = glutGetWindow () ;
155   printf ( "%6d Window %d Special Key Callback:  %d %d %d\n",
156             ++sequence_number, window, key, x, y ) ;
157   special_called = 1 ;
158   special_key = key ;
159   special_x = x ;
160   special_y = y ;
161   special_seq = sequence_number ;
162   glutPostRedisplay () ;
163 }
164
165 static void 
166 Visibility(int vis)
167 {
168   int window = glutGetWindow () ;
169   printf ( "%6d Window %d Visibility Callback:  %d\n",
170             ++sequence_number, window, vis ) ;
171   visibility_called = 1 ;
172   visibility_vis = vis ;
173   visibility_seq = sequence_number ;
174   glutPostRedisplay () ;
175 }
176
177 static void 
178 KeyUp(unsigned char key, int x, int y)
179 {
180   int window = glutGetWindow () ;
181   printf ( "%6d Window %d Key Release Callback:  %d %d %d\n",
182             ++sequence_number, window, key, x, y ) ;
183   keyup_called = 1 ;
184   keyup_key = key ;
185   keyup_x = x ;
186   keyup_y = y ;
187   keyup_seq = sequence_number ;
188   glutPostRedisplay () ;
189 }
190
191 static void 
192 SpecialUp(int key, int x, int y)
193 {
194   int window = glutGetWindow () ;
195   printf ( "%6d Window %d Special Key Release Callback:  %d %d %d\n",
196             ++sequence_number, window, key, x, y ) ;
197   specialup_called = 1 ;
198   specialup_key = key ;
199   specialup_x = x ;
200   specialup_y = y ;
201   specialup_seq = sequence_number ;
202   glutPostRedisplay () ;
203 }
204
205 static void 
206 Joystick( unsigned int a, int b, int c, int d)  /* Need meaningful names */
207 {
208   int window = glutGetWindow () ;
209   printf ( "%6d Window %d Joystick Callback:  %d %d %d %d\n",
210             ++sequence_number, window, a, b, c, d ) ;
211   joystick_called = 1 ;
212   joystick_a = a ;
213   joystick_b = b ;
214   joystick_c = c ;
215   joystick_d = d ;
216   joystick_seq = sequence_number ;
217   glutPostRedisplay () ;
218 }
219
220 static void 
221 Mouse(int button, int updown, int x, int y)
222 {
223   int window = glutGetWindow () ;
224   printf ( "%6d Window %d Mouse Click Callback:  %d %d %d %d\n",
225             ++sequence_number, window, button, updown, x, y ) ;
226   mouse_called = 1 ;
227   mouse_button = button ;
228   mouse_updown = updown ;
229   mouse_x = x ;
230   mouse_y = y ;
231   mouse_seq = sequence_number ;
232   glutPostRedisplay () ;
233 }
234
235 static void 
236 MouseWheel(int wheel_number, int direction, int x, int y)
237 {
238   int window = glutGetWindow () ;
239   printf ( "%6d Window %d Mouse Wheel Callback:  %d %d %d %d\n",
240             ++sequence_number, window, wheel_number, direction, x, y ) ;
241   mousewheel_called = 1 ;
242   mousewheel_number = wheel_number ;
243   mousewheel_direction = direction ;
244   mousewheel_x = x ;
245   mousewheel_y = y ;
246   mousewheel_seq = sequence_number ;
247   glutPostRedisplay () ;
248 }
249
250 static void 
251 Motion(int x, int y)
252 {
253   int window = glutGetWindow () ;
254   printf ( "%6d Window %d Mouse Motion Callback:  %d %d\n",
255             ++sequence_number, window, x, y ) ;
256   motion_called = 1 ;
257   motion_x = x ;
258   motion_y = y ;
259   motion_seq = sequence_number ;
260   glutPostRedisplay () ;
261 }
262
263 static void 
264 PassiveMotion(int x, int y)
265 {
266   int window = glutGetWindow () ;
267   printf ( "%6d Window %d Mouse Passive Motion Callback:  %d %d\n",
268             ++sequence_number, window, x, y ) ;
269   passivemotion_called = 1 ;
270   passivemotion_x = x ;
271   passivemotion_y = y ;
272   passivemotion_seq = sequence_number ;
273   glutPostRedisplay () ;
274 }
275
276 static void 
277 Entry(int state)
278 {
279   int window = glutGetWindow () ;
280   entry_called = 1 ;
281   printf ( "%6d Window %d Entry Callback:  %d\n",
282             ++sequence_number, window, state ) ;
283   glutPostRedisplay () ;
284 }
285
286 static void 
287 Close(void)
288 {
289   int window = glutGetWindow () ;
290   close_called = 1 ;
291   printf ( "%6d Window %d Close Callback\n",
292             ++sequence_number, window ) ;
293 }
294
295
296
297 int 
298 main(int argc, char *argv[])
299 {
300   int freeglut_window, aux_window ;
301
302   glutInitWindowSize(500, 250);
303   glutInitWindowPosition ( 140, 140 );
304   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
305   glutInit(&argc, argv);
306
307   freeglut_window = glutCreateWindow( "Callback Demo" );
308   printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
309
310   glClearColor(1.0, 1.0, 1.0, 1.0);
311
312   glutDisplayFunc( Display );
313   glutReshapeFunc( Reshape );
314   glutKeyboardFunc( Key );
315   glutSpecialFunc( Special );
316   glutVisibilityFunc( Visibility );
317   glutKeyboardUpFunc( KeyUp );
318   glutSpecialUpFunc( SpecialUp );
319   glutJoystickFunc( Joystick, 100 );
320   glutMouseFunc ( Mouse ) ;
321   glutMouseWheelFunc ( MouseWheel ) ;
322   glutMotionFunc ( Motion ) ;
323   glutPassiveMotionFunc ( PassiveMotion ) ;
324   glutEntryFunc ( Entry ) ;
325   glutCloseFunc ( Close ) ;
326
327   aux_window = glutCreateWindow( "Second Window" );
328   printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
329
330   glClearColor(1.0, 1.0, 1.0, 1.0);
331
332   glutDisplayFunc( Display );
333   glutReshapeFunc( Reshape );
334   glutKeyboardFunc( Key );
335   glutSpecialFunc( Special );
336   glutVisibilityFunc( Visibility );
337   glutKeyboardUpFunc( KeyUp );
338   glutSpecialUpFunc( SpecialUp );
339   /*  glutJoystickFunc( Joystick, 100 ); */
340   glutMouseFunc ( Mouse ) ;
341   glutMouseWheelFunc ( MouseWheel ) ;
342   glutMotionFunc ( Motion ) ;
343   glutPassiveMotionFunc ( PassiveMotion ) ;
344   glutEntryFunc ( Entry ) ;
345   glutCloseFunc ( Close ) ;
346
347   glutMainLoop();
348
349   printf ( "Back from the 'freeglut' main loop\n" ) ;
350
351   return 0;             /* ANSI C requires main to return int. */
352 }