added a child window to one demo, added different menus attached to
[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 /*
178  * The reshape function
179  */
180 void SampleReshape( int nWidth, int nHeight )
181 {
182     GLfloat fAspect = (GLfloat) nHeight / (GLfloat) nWidth;
183     GLfloat fPos[ 4 ] = { 0.0f, 0.0f, 10.0f, 0.0f };
184     GLfloat fCol[ 4 ] = { 0.5f, 1.0f,  0.0f, 1.0f };
185
186     /*
187      * Update the viewport first
188      */
189     glViewport( 0, 0, nWidth, nHeight );
190
191     /*
192      * Then the projection matrix
193      */
194     glMatrixMode( GL_PROJECTION );
195     glLoadIdentity();
196     glFrustum( -1.0, 1.0, -fAspect, fAspect, 1.0, 80.0 );
197
198     /*
199      * Move back the camera a bit
200      */
201     glMatrixMode( GL_MODELVIEW );
202     glLoadIdentity( );
203     glTranslatef( 0.0, 0.0, -40.0f );
204
205     /*
206      * Enable some features...
207      */
208     glEnable( GL_CULL_FACE );
209     glEnable( GL_DEPTH_TEST );
210     glEnable( GL_NORMALIZE );
211
212     /*
213      * Set up some lighting
214      */
215     glLightfv( GL_LIGHT0, GL_POSITION, fPos );
216     glEnable( GL_LIGHTING );
217     glEnable( GL_LIGHT0 );
218
219     /*
220      * Set up a sample material
221      */
222     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, fCol );
223 }
224
225 /*
226  * A sample keyboard callback
227  */
228 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
229 {
230     printf( "SampleKeyboard(): keypress '%c' at (%i,%i)\n",
231             cChar, nMouseX, nMouseY );
232 }
233
234 /*
235  * A sample keyboard callback (for game mode window)
236  */
237 void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
238 {
239     if( cChar == 27 )
240         g_LeaveGameMode = 1;
241 }
242
243
244 /*
245  * A sample special callback
246  */
247 void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
248 {
249     printf( "SampleSpecial(): special keypress %i at (%i,%i)\n",
250             nSpecial, nMouseX, nMouseY );
251 }
252
253 /*
254  * A sample menu callback
255  */
256 void SampleMenu( int menuID )
257 {
258     /*
259      * Just print something funny
260      */
261     printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
262 }
263
264 /*
265  * The sample's entry point
266  */
267 int main( int argc, char** argv )
268 {
269     int menuID, subMenuA, subMenuB;
270
271     glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
272     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
273     glutInitWindowPosition( 100, 100 );
274
275     glutInit( &argc, argv );
276
277     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
278
279     subMenuA = glutCreateMenu( SampleMenu );
280     glutAddMenuEntry( "Sub menu A1 (01)", 1 );
281     glutAddMenuEntry( "Sub menu A2 (02)", 2 );
282     glutAddMenuEntry( "Sub menu A3 (03)", 3 );
283
284     subMenuB = glutCreateMenu( SampleMenu );
285     glutAddMenuEntry( "Sub menu B1 (04)", 4 );
286     glutAddMenuEntry( "Sub menu B2 (05)", 5 );
287     glutAddMenuEntry( "Sub menu B3 (06)", 6 );
288     glutAddSubMenu( "Going to sub menu A", subMenuA );
289
290     menuID = glutCreateMenu( SampleMenu );
291     glutAddMenuEntry( "Entry one",   1 );
292     glutAddMenuEntry( "Entry two",   2 );
293     glutAddMenuEntry( "Entry three", 3 );
294     glutAddMenuEntry( "Entry four",  4 );
295     glutAddMenuEntry( "Entry five",  5 );
296     glutAddSubMenu( "Enter sub menu A", subMenuA );
297     glutAddSubMenu( "Enter sub menu B", subMenuB );
298
299     glutCreateWindow( "Hello world!" );
300     glutDisplayFunc( SampleDisplay );
301     glutReshapeFunc( SampleReshape );
302     glutKeyboardFunc( SampleKeyboard );
303     glutSpecialFunc( SampleSpecial );
304     glutIdleFunc( SampleIdle );
305     glutAttachMenu( GLUT_LEFT_BUTTON );
306
307     glutInitWindowPosition( 200, 200 );
308     g_mainwin = glutCreateWindow( "I am not Jan B." );
309     glutDisplayFunc( SampleDisplay );
310     glutReshapeFunc( SampleReshape );
311     glutKeyboardFunc( SampleKeyboard );
312     glutSpecialFunc( SampleSpecial );
313     glutIdleFunc( SampleIdle );
314     glutAttachMenu( GLUT_LEFT_BUTTON );
315     glutSetMenu(subMenuA);
316     glutAttachMenu( GLUT_RIGHT_BUTTON);
317
318     g_sw1=glutCreateSubWindow(g_mainwin,200,0,100,100);
319     glutDisplayFunc( SampleDisplay );
320     glutSetMenu(subMenuB);
321     glutAttachMenu( GLUT_LEFT_BUTTON);
322
323     printf( "Testing game mode string parsing, don't panic!\n" );
324     glutGameModeString( "320x240:32@100" );
325     glutGameModeString( "640x480:16@72" );
326     glutGameModeString( "1024x768" );
327     glutGameModeString( ":32@120" );
328     glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
329     
330     glutGameModeString( "640x480:37@300" );    /* this one should fail */
331     glutEnterGameMode();
332
333     glutGameModeString( "800x600" );    /* this one is likely to succeed */
334     glutEnterGameMode();
335
336     if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
337         g_InGameMode = 1;
338     glutDisplayFunc( SampleDisplay );
339     glutReshapeFunc( SampleReshape );
340     glutKeyboardFunc( SampleGameModeKeyboard );
341     glutIdleFunc( SampleIdle );
342
343     printf( "current window is %ix%i at (%i,%i)\n",
344         glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
345         glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
346     );
347
348     /*
349      * Enter the main FreeGLUT processing loop
350      */
351     glutMainLoop();
352
353     printf( "glutMainLoop() termination works fine!\n" );
354
355     /*
356      * This is never reached in FreeGLUT. Is that good?
357      */
358     return EXIT_SUCCESS;
359 }
360
361 /*** END OF FILE ***/