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