added one more child window (child-of-child) to one demo for testing
[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, g_sw2;
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 if (win==g_sw2)
113     {
114         /*
115         * Clear the screen
116         */
117         glClearColor(0.3f,0.3f,0.3f,1);
118         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
119         glutPostWindowRedisplay(g_mainwin);
120     }
121     else
122     {
123         /*
124          * Clear the screen
125          */
126         glClearColor( 0, 0.5, 1, 1 );
127         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
128
129         /*
130          * Have the cube rotated
131          */
132         glMatrixMode( GL_MODELVIEW );
133         glPushMatrix();
134
135         glRotatef( g_fTime, 0, 0, 1 );
136         glRotatef( g_fTime, 0, 1, 0 );
137         glRotatef( g_fTime, 1, 0, 0 );
138
139         /*
140          * And then drawn...
141          */
142         glColor3f( 1, 1, 0 );
143         /* glutWireCube( 20.0 ); */
144         glutWireTeapot( 20.0 );
145         /* glutWireSpher( 15.0, 15, 15 ); */
146         /* glColor3f( 0, 1, 0 ); */
147         /* glutWireCube( 30.0 ); */
148         /* glutSolidCone( 10, 20, 10, 2 ); */
149
150         /*
151          * Don't forget about the model-view matrix
152          */
153         glPopMatrix( );
154
155         /*
156          * Draw a silly text
157          */
158         if( g_InGameMode == 0 )
159             PrintText( 20, 20, "Hello there cruel world!" );
160         else
161             PrintText( 20, 20, "Press ESC to leave the game mode!" );
162     }
163
164     /*
165      * And swap this context's buffers
166      */
167     glutSwapBuffers( );
168     glutPostWindowRedisplay(win);
169 }
170
171 /*
172  * This is a sample idle function
173  */
174 void SampleIdle( void )
175 {
176     g_fTime += 0.5f;
177
178     if( g_LeaveGameMode == 1 )
179     {
180         glutLeaveGameMode( );
181         g_LeaveGameMode = 0;
182         g_InGameMode = 0;
183     }
184 }
185
186 void SampleEntry(int state)
187 {
188     int window = glutGetWindow () ;
189     printf ( "Window %d Entry Callback: %d\n", window, state ) ;
190     glutPostRedisplay () ;
191 }
192
193 /*
194  * The reshape function
195  */
196 void SampleReshape( int nWidth, int nHeight )
197 {
198     GLfloat fAspect = (GLfloat) nHeight / (GLfloat) nWidth;
199     GLfloat fPos[ 4 ] = { 0.0f, 0.0f, 10.0f, 0.0f };
200     GLfloat fCol[ 4 ] = { 0.5f, 1.0f,  0.0f, 1.0f };
201
202     /*
203      * Update the viewport first
204      */
205     glViewport( 0, 0, nWidth, nHeight );
206
207     /*
208      * Then the projection matrix
209      */
210     glMatrixMode( GL_PROJECTION );
211     glLoadIdentity();
212     glFrustum( -1.0, 1.0, -fAspect, fAspect, 1.0, 80.0 );
213
214     /*
215      * Move back the camera a bit
216      */
217     glMatrixMode( GL_MODELVIEW );
218     glLoadIdentity( );
219     glTranslatef( 0.0, 0.0, -40.0f );
220
221     /*
222      * Enable some features...
223      */
224     glEnable( GL_CULL_FACE );
225     glEnable( GL_DEPTH_TEST );
226     glEnable( GL_NORMALIZE );
227
228     /*
229      * Set up some lighting
230      */
231     glLightfv( GL_LIGHT0, GL_POSITION, fPos );
232     glEnable( GL_LIGHTING );
233     glEnable( GL_LIGHT0 );
234
235     /*
236      * Set up a sample material
237      */
238     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, fCol );
239 }
240
241 /*
242  * A sample keyboard callback
243  */
244 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
245 {
246     printf( "SampleKeyboard(): keypress '%c' at (%i,%i)\n",
247             cChar, nMouseX, nMouseY );
248 }
249
250 /*
251  * A sample keyboard callback (for game mode window)
252  */
253 void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
254 {
255     if( cChar == 27 )
256         g_LeaveGameMode = 1;
257 }
258
259
260 /*
261  * A sample special callback
262  */
263 void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
264 {
265     printf( "SampleSpecial(): special keypress %i at (%i,%i)\n",
266             nSpecial, nMouseX, nMouseY );
267 }
268
269 /*
270  * A sample menu callback
271  */
272 void SampleMenu( int menuID )
273 {
274     /*
275      * Just print something funny
276      */
277     printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
278 }
279
280 /*
281  * The sample's entry point
282  */
283 int main( int argc, char** argv )
284 {
285     int menuID, subMenuA, subMenuB;
286
287     glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
288     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
289     glutInitWindowPosition( 100, 100 );
290
291     glutInit( &argc, argv );
292
293     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
294
295     subMenuA = glutCreateMenu( SampleMenu );
296     glutAddMenuEntry( "Sub menu A1 (01)", 1 );
297     glutAddMenuEntry( "Sub menu A2 (02)", 2 );
298     glutAddMenuEntry( "Sub menu A3 (03)", 3 );
299
300     subMenuB = glutCreateMenu( SampleMenu );
301     glutAddMenuEntry( "Sub menu B1 (04)", 4 );
302     glutAddMenuEntry( "Sub menu B2 (05)", 5 );
303     glutAddMenuEntry( "Sub menu B3 (06)", 6 );
304     glutAddSubMenu( "Going to sub menu A", subMenuA );
305
306     menuID = glutCreateMenu( SampleMenu );
307     glutAddMenuEntry( "Entry one",   1 );
308     glutAddMenuEntry( "Entry two",   2 );
309     glutAddMenuEntry( "Entry three", 3 );
310     glutAddMenuEntry( "Entry four",  4 );
311     glutAddMenuEntry( "Entry five",  5 );
312     glutAddSubMenu( "Enter sub menu A", subMenuA );
313     glutAddSubMenu( "Enter sub menu B", subMenuB );
314
315     glutCreateWindow( "Hello world!" );
316     glutDisplayFunc( SampleDisplay );
317     glutReshapeFunc( SampleReshape );
318     glutKeyboardFunc( SampleKeyboard );
319     glutSpecialFunc( SampleSpecial );
320     glutIdleFunc( SampleIdle );
321     glutEntryFunc( SampleEntry );
322     glutAttachMenu( GLUT_LEFT_BUTTON );
323
324     glutInitWindowPosition( 200, 200 );
325     g_mainwin = glutCreateWindow( "I am not Jan B." );
326     glutDisplayFunc( SampleDisplay );
327     glutReshapeFunc( SampleReshape );
328     glutKeyboardFunc( SampleKeyboard );
329     glutSpecialFunc( SampleSpecial );
330     glutIdleFunc( SampleIdle );
331     glutAttachMenu( GLUT_LEFT_BUTTON );
332     glutSetMenu(subMenuA);
333     glutAttachMenu( GLUT_RIGHT_BUTTON );
334
335     g_sw1=glutCreateSubWindow(g_mainwin,200,0,100,100);
336     glutDisplayFunc( SampleDisplay );
337     glutSetMenu(subMenuB);
338     glutAttachMenu( GLUT_LEFT_BUTTON);
339
340     g_sw2=glutCreateSubWindow(g_sw1,50,0,50,50);
341     glutDisplayFunc( SampleDisplay );
342     glutSetMenu(menuID);
343     glutAttachMenu( GLUT_RIGHT_BUTTON );
344
345     printf( "Testing game mode string parsing, don't panic!\n" );
346     glutGameModeString( "320x240:32@100" );
347     glutGameModeString( "640x480:16@72" );
348     glutGameModeString( "1024x768" );
349     glutGameModeString( ":32@120" );
350     glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
351     
352     glutGameModeString( "640x480:37@300" );    /* this one should fail */
353     glutEnterGameMode();
354
355     glutGameModeString( "800x600" );    /* this one is likely to succeed */
356     glutEnterGameMode();
357
358     if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
359         g_InGameMode = 1;
360     glutDisplayFunc( SampleDisplay );
361     glutReshapeFunc( SampleReshape );
362     glutKeyboardFunc( SampleGameModeKeyboard );
363     glutIdleFunc( SampleIdle );
364
365     printf( "current window is %ix%i at (%i,%i)\n",
366         glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
367         glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
368     );
369
370     /*
371      * Enter the main FreeGLUT processing loop
372      */
373     glutMainLoop();
374
375     printf( "glutMainLoop() termination works fine!\n" );
376
377     /*
378      * This is never reached in FreeGLUT. Is that good?
379      */
380     return EXIT_SUCCESS;
381 }
382
383 /*** END OF FILE ***/