* Removed genfonts/genstrokes code, updated configure.in and Makefile.in
[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 "../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", cChar, nMouseX, nMouseY );
213 }
214
215 /*
216  * A sample keyboard callback (for game mode window)
217  */
218 void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
219 {
220     if( cChar == 27 )
221         g_LeaveGameMode = 1;
222 }
223
224
225 /*
226  * A sample special callback
227  */
228 void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
229 {
230     printf( "SampleSpecial(): special keypress %i at (%i,%i)\n", nSpecial, nMouseX, nMouseY );
231 }
232
233 /*
234  * A sample menu callback
235  */
236 void SampleMenu( int menuID )
237 {
238     /*
239      * Just print something funny
240      */
241     printf( "SampleMenu() callback executed, menuID is %i\n", menuID ); 
242 }
243
244 /*
245  * The sample's entry point
246  */
247 int main( int argc, char** argv )
248 {
249     int menuID, subMenuA, subMenuB;
250
251     glutInit( &argc, argv );
252
253     glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
254     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
255     glutInitWindowPosition( 100, 100 );
256
257     subMenuA = glutCreateMenu( SampleMenu );
258     glutAddMenuEntry( "Sub menu A1 (01)", 1 );
259     glutAddMenuEntry( "Sub menu A2 (02)", 2 );
260     glutAddMenuEntry( "Sub menu A3 (03)", 3 );
261     
262     subMenuB = glutCreateMenu( SampleMenu );
263     glutAddMenuEntry( "Sub menu B1 (04)", 4 );
264     glutAddMenuEntry( "Sub menu B2 (05)", 5 );
265     glutAddMenuEntry( "Sub menu B3 (06)", 6 );
266     glutAddSubMenu( "Going to sub menu A", subMenuA );
267
268     menuID = glutCreateMenu( SampleMenu );
269     glutAddMenuEntry( "Entry one",   1 );
270     glutAddMenuEntry( "Entry two",   2 );
271     glutAddMenuEntry( "Entry three", 3 );
272     glutAddMenuEntry( "Entry four",  4 );
273     glutAddMenuEntry( "Entry five",  5 );
274     glutAddSubMenu( "Enter sub menu A", subMenuA );
275     glutAddSubMenu( "Enter sub menu B", subMenuB );
276
277     glutCreateWindow( "Hello world!" );
278     glutDisplayFunc( SampleDisplay );
279     glutReshapeFunc( SampleReshape );
280     glutKeyboardFunc( SampleKeyboard );
281     glutSpecialFunc( SampleSpecial );
282     glutIdleFunc( SampleIdle );
283     glutAttachMenu( GLUT_LEFT_BUTTON );
284
285     glutInitWindowPosition( 200, 200 );
286     glutCreateWindow( "I am not Jan B." );
287     glutDisplayFunc( SampleDisplay );
288     glutReshapeFunc( SampleReshape );
289     glutKeyboardFunc( SampleKeyboard );
290     glutSpecialFunc( SampleSpecial );
291     glutIdleFunc( SampleIdle );
292     glutAttachMenu( GLUT_LEFT_BUTTON );
293
294     printf( "Testing game mode string parsing, don't panic!\n" );
295     glutGameModeString( "320x240:32@100" );
296     glutGameModeString( "640x480:16@72" );
297     glutGameModeString( "1024x768" );
298     glutGameModeString( ":32@120" );
299     glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" ); 
300     glutGameModeString( "640x480:16@72" );
301
302     glutEnterGameMode();
303     glutDisplayFunc( SampleDisplay );
304     glutReshapeFunc( SampleReshape );
305     glutKeyboardFunc( SampleGameModeKeyboard );
306     glutIdleFunc( SampleIdle );
307     glutAttachMenu( GLUT_LEFT_BUTTON );
308
309     printf( "current window is %ix%i+%i+%i",
310         glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y ),
311         glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT )
312     );  
313
314     /*
315      * Enter the main FreeGLUT processing loop
316      */
317     glutMainLoop();
318
319     printf( "glutMainLoop() termination works fine!\n" );
320
321     /*
322      * This is never reached in FreeGLUT. Is that good?
323      */
324     return( EXIT_SUCCESS );
325 }
326
327 /*** END OF FILE ***/