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