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