6509c3b0db112a9ce66354d493a817e8bff770d8
[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_mainwin1, g_mainwin2, 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         /* Don't draw other windows when in gamemode, those aren't visible
103          * anyway. Drawing them continuously nonetheless can cause flicker trouble
104          * on my machine. This only seems to occur only 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_mainwin2);
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_mainwin2);
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         printf("leaving gamemode...\n");
188         glutLeaveGameMode( );
189         g_LeaveGameMode = 0;
190         g_InGameMode = 0;
191         glutPostWindowRedisplay(g_mainwin1);
192         glutPostWindowRedisplay(g_mainwin2);
193         glutPostWindowRedisplay(g_sw1);
194         glutPostWindowRedisplay(g_sw2);
195     }
196 }
197
198 void SampleEntry(int state)
199 {
200     int window = glutGetWindow () ;
201     printf ( "Window %d Entry Callback: %d\n", window, state ) ;
202 }
203
204 /*
205  * The reshape function
206  */
207 void SampleReshape( int nWidth, int nHeight )
208 {
209     GLfloat fAspect = (GLfloat) nHeight / (GLfloat) nWidth;
210     GLfloat fPos[ 4 ] = { 0.0f, 0.0f, 10.0f, 0.0f };
211     GLfloat fCol[ 4 ] = { 0.5f, 1.0f,  0.0f, 1.0f };
212
213     /*
214      * Update the viewport first
215      */
216     glViewport( 0, 0, nWidth, nHeight );
217
218     /*
219      * Then the projection matrix
220      */
221     glMatrixMode( GL_PROJECTION );
222     glLoadIdentity();
223     glFrustum( -1.0, 1.0, -fAspect, fAspect, 1.0, 80.0 );
224
225     /*
226      * Move back the camera a bit
227      */
228     glMatrixMode( GL_MODELVIEW );
229     glLoadIdentity( );
230     glTranslatef( 0.0, 0.0, -40.0f );
231
232     /*
233      * Enable some features...
234      */
235     glEnable( GL_CULL_FACE );
236     glEnable( GL_DEPTH_TEST );
237     glEnable( GL_NORMALIZE );
238
239     /*
240      * Set up some lighting
241      */
242     glLightfv( GL_LIGHT0, GL_POSITION, fPos );
243     glEnable( GL_LIGHTING );
244     glEnable( GL_LIGHT0 );
245
246     /*
247      * Set up a sample material
248      */
249     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, fCol );
250 }
251
252 /*
253  * A sample keyboard callback
254  */
255 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
256 {
257     printf( "SampleKeyboard(): keypress '%c' at (%i,%i)\n",
258             cChar, nMouseX, nMouseY );
259 }
260
261 /*
262  * A sample keyboard callback (for game mode window)
263  */
264 void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
265 {
266     if( cChar == 27 )
267         g_LeaveGameMode = 1;
268 }
269
270
271 /*
272  * A sample special callback
273  */
274 void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
275 {
276     printf( "SampleSpecial(): special keypress %i at (%i,%i)\n",
277             nSpecial, nMouseX, nMouseY );
278 }
279
280 /*
281  * A sample menu callback
282  */
283 void SampleMenu( int menuID )
284 {
285     printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
286 }
287
288 /*
289  * A sample menu status callback
290  */
291 void SampleMenuStatus( int status, int x, int y )
292 {
293     printf ( "SampleMenu() callback executed, MenuStatus is %i at (%i,%i)\n", status, x, y );
294 }
295
296 /*
297  * The sample's entry point
298  */
299 int main( int argc, char** argv )
300 {
301     int menuID, subMenuA, subMenuB;
302
303     glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
304     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
305     glutInitWindowPosition( 100, 100 );
306
307     glutInit( &argc, argv );
308
309     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
310     glutMenuStatusFunc( SampleMenuStatus );
311     glutIdleFunc( SampleIdle );
312
313     subMenuA = glutCreateMenu( SampleMenu );
314     glutAddMenuEntry( "Sub menu A1 (01)", 1 );
315     glutAddMenuEntry( "Sub menu A2 (02)", 2 );
316     glutAddMenuEntry( "Sub menu A3 (03)", 3 );
317
318     subMenuB = glutCreateMenu( SampleMenu );
319     glutAddMenuEntry( "Sub menu B1 (04)", 4 );
320     glutAddMenuEntry( "Sub menu B2 (05)", 5 );
321     glutAddMenuEntry( "Sub menu B3 (06)", 6 );
322     glutAddSubMenu( "Going to sub menu A", subMenuA );
323
324     menuID = glutCreateMenu( SampleMenu );
325     glutAddMenuEntry( "Entry one",   1 );
326     glutAddMenuEntry( "Entry two",   2 );
327     glutAddMenuEntry( "Entry three", 3 );
328     glutAddMenuEntry( "Entry four",  4 );
329     glutAddMenuEntry( "Entry five",  5 );
330     glutAddSubMenu( "Enter sub menu A", subMenuA );
331     glutAddSubMenu( "Enter sub menu B", subMenuB );
332
333     g_mainwin1 = glutCreateWindow( "Hello world!" );
334     glutDisplayFunc( SampleDisplay );
335     glutReshapeFunc( SampleReshape );
336     glutKeyboardFunc( SampleKeyboard );
337     glutSpecialFunc( SampleSpecial );
338     glutEntryFunc( SampleEntry );
339     glutAttachMenu( GLUT_LEFT_BUTTON );
340
341     glutInitWindowPosition( 200, 200 );
342     g_mainwin2 = glutCreateWindow( "I am not Jan B." );
343     glutDisplayFunc( SampleDisplay );
344     glutReshapeFunc( SampleReshape );
345     glutKeyboardFunc( SampleKeyboard );
346     glutSpecialFunc( SampleSpecial );
347     glutEntryFunc( SampleEntry );
348     glutAttachMenu( GLUT_LEFT_BUTTON );
349     glutSetMenu(subMenuA);
350     glutAttachMenu( GLUT_RIGHT_BUTTON );
351
352     g_sw1=glutCreateSubWindow(g_mainwin2,200,0,100,100);
353     glutDisplayFunc( SampleDisplay );
354     glutSetMenu(subMenuB);
355     glutAttachMenu( GLUT_LEFT_BUTTON );
356
357     g_sw2=glutCreateSubWindow(g_sw1,50,0,50,50);
358     glutDisplayFunc( SampleDisplay );
359     glutSetMenu(menuID);
360     glutAttachMenu( GLUT_RIGHT_BUTTON );
361
362     printf( "Testing game mode string parsing, don't panic!\n" );
363     glutGameModeString( "320x240:32@100" );
364     glutGameModeString( "640x480:16@72" );
365     glutGameModeString( "1024x768" );
366     glutGameModeString( ":32@120" );
367     glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
368     
369     glutGameModeString( "640x480:37@300" );    /* this one should fail */
370     glutEnterGameMode();
371
372     glutGameModeString( "800x600" );    /* this one is likely to succeed */
373     g_gamemodewin = glutEnterGameMode();
374
375     if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
376         g_InGameMode = 1;
377     glutDisplayFunc( SampleDisplay );
378     glutReshapeFunc( SampleReshape );
379     glutKeyboardFunc( SampleGameModeKeyboard );
380     glutEntryFunc( SampleEntry );
381     glutSetMenu(menuID);
382     glutAttachMenu( GLUT_LEFT_BUTTON );
383
384     printf( "current window is %ix%i at (%i,%i)\n",
385         glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
386         glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
387     );
388
389     /*
390      * Enter the main FreeGLUT processing loop
391      */
392     glutMainLoop();
393
394     /*
395      * returned from mainloop after window closed
396      * see glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS); above
397      */
398     printf( "glutMainLoop() termination works fine!\n" );
399
400     return EXIT_SUCCESS;
401 }
402
403 /*** END OF FILE ***/