added entry callback to one demo
[freeglut] / progs / demos / One / one.c
1 /*
2  * one.c
3  *
4  * Hey! This was the original file where freeglut development started. Just
5  * note what I have written here at the time. And see the creation date :)
6  *
7  * : This is a wrapper. I still have to figure out
8  * : how to build shared libraries under *nix :)
9  *
10  * Copyright (c) 1999 by Pawel W. Olszta
11  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
12  * Creation date: czw gru  2 11:58:41 CET 1999
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <GL/freeglut.h>
23
24 int g_LeaveGameMode = 0;
25 int g_InGameMode = 0;
26 int g_mainwin, g_sw1;
27
28 /*
29  * Call this function to have some text drawn at given coordinates
30  */
31 void PrintText( int nX, int nY, char* pszText )
32 {
33     int lines;
34     char *p;
35
36     /*
37      * Prepare the OpenGL state
38      */
39     glDisable( GL_LIGHTING );
40     glDisable( GL_DEPTH_TEST );
41     glMatrixMode( GL_PROJECTION );
42     glPushMatrix();
43     glLoadIdentity();
44
45     /*
46      * Have an orthogonal projection matrix set
47      */
48     glOrtho( 0, glutGet( GLUT_WINDOW_WIDTH ),
49              0, glutGet( GLUT_WINDOW_HEIGHT ),
50              -1, +1
51     );
52
53     /*
54      * Now the matrix mode
55      */
56     glMatrixMode( GL_MODELVIEW );
57     glPushMatrix();
58     glLoadIdentity();
59
60     /*
61      * Now the main text
62      */
63     glColor3ub( 0, 0, 0 );
64     glRasterPos2i( nX, nY );
65
66     for( p=pszText, lines=0; *p; p++ )
67     {
68         if( *p == '\n' )
69         {
70             lines++;
71             glRasterPos2i( nX, nY-(lines*18) );
72         }
73
74         glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18,  *p );
75     }
76
77     /*
78      * Revert to the old matrix modes
79      */
80     glMatrixMode( GL_PROJECTION );
81     glPopMatrix();
82
83     glMatrixMode( GL_MODELVIEW );
84     glPopMatrix();
85
86     /*
87      * Restore the old OpenGL states
88      */
89     glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
90     glEnable( GL_DEPTH_TEST );
91     glEnable( GL_LIGHTING );
92 }
93
94 /*
95  * This is the display routine for our sample FreeGLUT windows
96  */
97 static float g_fTime = 0.0f;
98
99 void SampleDisplay( void )
100 {
101     int win = glutGetWindow();
102
103     if (win==g_sw1)
104     {
105         /*
106          * Clear the screen
107          */
108         glClearColor(0.7f,0.7f,0.7f,1);
109         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
110         glutPostWindowRedisplay(g_mainwin);
111     }
112     else
113     {
114         /*
115          * Clear the screen
116          */
117         glClearColor( 0, 0.5, 1, 1 );
118         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
119
120         /*
121          * Have the cube rotated
122          */
123         glMatrixMode( GL_MODELVIEW );
124         glPushMatrix();
125
126         glRotatef( g_fTime, 0, 0, 1 );
127         glRotatef( g_fTime, 0, 1, 0 );
128         glRotatef( g_fTime, 1, 0, 0 );
129
130         /*
131          * And then drawn...
132          */
133         glColor3f( 1, 1, 0 );
134         /* glutWireCube( 20.0 ); */
135         glutWireTeapot( 20.0 );
136         /* glutWireSpher( 15.0, 15, 15 ); */
137         /* glColor3f( 0, 1, 0 ); */
138         /* glutWireCube( 30.0 ); */
139         /* glutSolidCone( 10, 20, 10, 2 ); */
140
141         /*
142          * Don't forget about the model-view matrix
143          */
144         glPopMatrix( );
145
146         /*
147          * Draw a silly text
148          */
149         if( g_InGameMode == 0 )
150             PrintText( 20, 20, "Hello there cruel world!" );
151         else
152             PrintText( 20, 20, "Press ESC to leave the game mode!" );
153     }
154
155     /*
156      * And swap this context's buffers
157      */
158     glutSwapBuffers( );
159     glutPostWindowRedisplay(win);
160 }
161
162 /*
163  * This is a sample idle function
164  */
165 void SampleIdle( void )
166 {
167     g_fTime += 0.5f;
168
169     if( g_LeaveGameMode == 1 )
170     {
171         glutLeaveGameMode( );
172         g_LeaveGameMode = 0;
173         g_InGameMode = 0;
174     }
175 }
176
177 void SampleEntry(int state)
178 {
179     int window = glutGetWindow () ;
180     printf ( "Window %d Entry Callback: %d\n", window, state ) ;
181     glutPostRedisplay () ;
182 }
183
184 /*
185  * The reshape function
186  */
187 void SampleReshape( int nWidth, int nHeight )
188 {
189     GLfloat fAspect = (GLfloat) nHeight / (GLfloat) nWidth;
190     GLfloat fPos[ 4 ] = { 0.0f, 0.0f, 10.0f, 0.0f };
191     GLfloat fCol[ 4 ] = { 0.5f, 1.0f,  0.0f, 1.0f };
192
193     /*
194      * Update the viewport first
195      */
196     glViewport( 0, 0, nWidth, nHeight );
197
198     /*
199      * Then the projection matrix
200      */
201     glMatrixMode( GL_PROJECTION );
202     glLoadIdentity();
203     glFrustum( -1.0, 1.0, -fAspect, fAspect, 1.0, 80.0 );
204
205     /*
206      * Move back the camera a bit
207      */
208     glMatrixMode( GL_MODELVIEW );
209     glLoadIdentity( );
210     glTranslatef( 0.0, 0.0, -40.0f );
211
212     /*
213      * Enable some features...
214      */
215     glEnable( GL_CULL_FACE );
216     glEnable( GL_DEPTH_TEST );
217     glEnable( GL_NORMALIZE );
218
219     /*
220      * Set up some lighting
221      */
222     glLightfv( GL_LIGHT0, GL_POSITION, fPos );
223     glEnable( GL_LIGHTING );
224     glEnable( GL_LIGHT0 );
225
226     /*
227      * Set up a sample material
228      */
229     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, fCol );
230 }
231
232 /*
233  * A sample keyboard callback
234  */
235 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
236 {
237     printf( "SampleKeyboard(): keypress '%c' at (%i,%i)\n",
238             cChar, nMouseX, nMouseY );
239 }
240
241 /*
242  * A sample keyboard callback (for game mode window)
243  */
244 void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
245 {
246     if( cChar == 27 )
247         g_LeaveGameMode = 1;
248 }
249
250
251 /*
252  * A sample special callback
253  */
254 void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
255 {
256     printf( "SampleSpecial(): special keypress %i at (%i,%i)\n",
257             nSpecial, nMouseX, nMouseY );
258 }
259
260 /*
261  * A sample menu callback
262  */
263 void SampleMenu( int menuID )
264 {
265     /*
266      * Just print something funny
267      */
268     printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
269 }
270
271 /*
272  * The sample's entry point
273  */
274 int main( int argc, char** argv )
275 {
276     int menuID, subMenuA, subMenuB;
277
278     glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
279     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
280     glutInitWindowPosition( 100, 100 );
281
282     glutInit( &argc, argv );
283
284     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
285
286     subMenuA = glutCreateMenu( SampleMenu );
287     glutAddMenuEntry( "Sub menu A1 (01)", 1 );
288     glutAddMenuEntry( "Sub menu A2 (02)", 2 );
289     glutAddMenuEntry( "Sub menu A3 (03)", 3 );
290
291     subMenuB = glutCreateMenu( SampleMenu );
292     glutAddMenuEntry( "Sub menu B1 (04)", 4 );
293     glutAddMenuEntry( "Sub menu B2 (05)", 5 );
294     glutAddMenuEntry( "Sub menu B3 (06)", 6 );
295     glutAddSubMenu( "Going to sub menu A", subMenuA );
296
297     menuID = glutCreateMenu( SampleMenu );
298     glutAddMenuEntry( "Entry one",   1 );
299     glutAddMenuEntry( "Entry two",   2 );
300     glutAddMenuEntry( "Entry three", 3 );
301     glutAddMenuEntry( "Entry four",  4 );
302     glutAddMenuEntry( "Entry five",  5 );
303     glutAddSubMenu( "Enter sub menu A", subMenuA );
304     glutAddSubMenu( "Enter sub menu B", subMenuB );
305
306     glutCreateWindow( "Hello world!" );
307     glutDisplayFunc( SampleDisplay );
308     glutReshapeFunc( SampleReshape );
309     glutKeyboardFunc( SampleKeyboard );
310     glutSpecialFunc( SampleSpecial );
311     glutIdleFunc( SampleIdle );
312     glutEntryFunc( SampleEntry );
313     glutAttachMenu( GLUT_LEFT_BUTTON );
314
315     glutInitWindowPosition( 200, 200 );
316     g_mainwin = glutCreateWindow( "I am not Jan B." );
317     glutDisplayFunc( SampleDisplay );
318     glutReshapeFunc( SampleReshape );
319     glutKeyboardFunc( SampleKeyboard );
320     glutSpecialFunc( SampleSpecial );
321     glutIdleFunc( SampleIdle );
322     glutAttachMenu( GLUT_LEFT_BUTTON );
323     glutSetMenu(subMenuA);
324     glutAttachMenu( GLUT_RIGHT_BUTTON);
325
326     g_sw1=glutCreateSubWindow(g_mainwin,200,0,100,100);
327     glutDisplayFunc( SampleDisplay );
328     glutSetMenu(subMenuB);
329     glutAttachMenu( GLUT_LEFT_BUTTON);
330
331     printf( "Testing game mode string parsing, don't panic!\n" );
332     glutGameModeString( "320x240:32@100" );
333     glutGameModeString( "640x480:16@72" );
334     glutGameModeString( "1024x768" );
335     glutGameModeString( ":32@120" );
336     glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
337     
338     glutGameModeString( "640x480:37@300" );    /* this one should fail */
339     glutEnterGameMode();
340
341     glutGameModeString( "800x600" );    /* this one is likely to succeed */
342     glutEnterGameMode();
343
344     if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
345         g_InGameMode = 1;
346     glutDisplayFunc( SampleDisplay );
347     glutReshapeFunc( SampleReshape );
348     glutKeyboardFunc( SampleGameModeKeyboard );
349     glutIdleFunc( SampleIdle );
350
351     printf( "current window is %ix%i at (%i,%i)\n",
352         glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
353         glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
354     );
355
356     /*
357      * Enter the main FreeGLUT processing loop
358      */
359     glutMainLoop();
360
361     printf( "glutMainLoop() termination works fine!\n" );
362
363     /*
364      * This is never reached in FreeGLUT. Is that good?
365      */
366     return EXIT_SUCCESS;
367 }
368
369 /*** END OF FILE ***/