2001-07-27 07:35:54 +03:00
/*
* one . c
*
2004-03-15 08:08:08 +02:00
* Hey ! This was the original file where freeglut development started . Just
2001-07-27 07:35:54 +03:00
* note what I have written here at the time . And see the creation date : )
*
* : This is a wrapper . I still have to figure out
* : how to build shared libraries under * nix : )
*
* Copyright ( c ) 1999 by Pawel W . Olszta
* Written by Pawel W . Olszta , < olszta @ sourceforge . net >
* Creation date : czw gru 2 11 : 58 : 41 CET 1999
*/
# include <stdio.h>
# include <stdlib.h>
2003-09-13 16:38:04 +03:00
# include <GL/freeglut.h>
2001-07-27 07:35:54 +03:00
int g_LeaveGameMode = 0 ;
2011-03-17 06:22:55 +02:00
int g_InGameMode = 0 ;
2012-11-20 07:25:14 +02:00
int g_mainwin1 , g_mainwin2 , g_sw1 , g_sw2 , g_gamemodewin ;
2001-07-27 07:35:54 +03:00
/*
* Call this function to have some text drawn at given coordinates
*/
void PrintText ( int nX , int nY , char * pszText )
{
int lines ;
char * p ;
2004-03-15 08:08:08 +02:00
2001-07-27 07:35:54 +03:00
/*
* Prepare the OpenGL state
*/
2003-12-11 20:49:38 +02:00
glDisable ( GL_LIGHTING ) ;
2004-03-15 08:08:08 +02:00
glDisable ( GL_DEPTH_TEST ) ;
2001-07-27 07:35:54 +03:00
glMatrixMode ( GL_PROJECTION ) ;
glPushMatrix ( ) ;
glLoadIdentity ( ) ;
2004-03-15 08:08:08 +02:00
2001-07-27 07:35:54 +03:00
/*
* Have an orthogonal projection matrix set
*/
glOrtho ( 0 , glutGet ( GLUT_WINDOW_WIDTH ) ,
2003-11-11 13:18:16 +02:00
0 , glutGet ( GLUT_WINDOW_HEIGHT ) ,
- 1 , + 1
2001-07-27 07:35:54 +03:00
) ;
/*
* Now the matrix mode
*/
glMatrixMode ( GL_MODELVIEW ) ;
glPushMatrix ( ) ;
glLoadIdentity ( ) ;
2004-03-15 08:08:08 +02:00
2001-07-27 07:35:54 +03:00
/*
2004-03-15 08:08:08 +02:00
* Now the main text
2001-07-27 07:35:54 +03:00
*/
2004-03-15 08:08:08 +02:00
glColor3ub ( 0 , 0 , 0 ) ;
glRasterPos2i ( nX , nY ) ;
2001-07-27 07:35:54 +03:00
for ( p = pszText , lines = 0 ; * p ; p + + )
{
2003-11-11 13:52:14 +02:00
if ( * p = = ' \n ' )
2003-11-11 13:18:16 +02:00
{
lines + + ;
glRasterPos2i ( nX , nY - ( lines * 18 ) ) ;
}
2004-03-15 08:08:08 +02:00
2003-11-11 13:18:16 +02:00
glutBitmapCharacter ( GLUT_BITMAP_HELVETICA_18 , * p ) ;
2001-07-27 07:35:54 +03:00
}
2004-03-15 08:08:08 +02:00
2001-07-27 07:35:54 +03:00
/*
* Revert to the old matrix modes
2004-03-15 08:08:08 +02:00
*/
2001-07-27 07:35:54 +03:00
glMatrixMode ( GL_PROJECTION ) ;
glPopMatrix ( ) ;
2004-03-15 08:08:08 +02:00
2001-07-27 07:35:54 +03:00
glMatrixMode ( GL_MODELVIEW ) ;
glPopMatrix ( ) ;
2004-03-15 08:08:08 +02:00
2001-07-27 07:35:54 +03:00
/*
* Restore the old OpenGL states
*/
glColor4f ( 1.0f , 1.0f , 1.0f , 1.0f ) ;
glEnable ( GL_DEPTH_TEST ) ;
2003-12-11 20:49:38 +02:00
glEnable ( GL_LIGHTING ) ;
2004-03-15 08:08:08 +02:00
}
2001-07-27 07:35:54 +03:00
/*
* This is the display routine for our sample FreeGLUT windows
*/
void SampleDisplay ( void )
{
2012-07-21 17:15:39 +03:00
int win = glutGetWindow ( ) ;
2001-07-27 07:35:54 +03:00
2012-07-23 13:37:40 +03:00
if ( g_InGameMode & & win ! = g_gamemodewin )
2012-11-20 07:25:14 +02:00
/* Don't draw other windows when in gamemode, those aren't visible
* anyway . Drawing them continuously nonetheless can cause flicker trouble
* on my machine . This only seems to occur only when there are child windows
2012-07-23 13:37:40 +03:00
* among the non - visible windows
*/
return ;
2012-07-21 17:15:39 +03:00
if ( win = = g_sw1 )
{
/*
* Clear the screen
*/
glClearColor ( 0.7f , 0.7f , 0.7f , 1 ) ;
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
2012-11-20 07:25:14 +02:00
glutPostWindowRedisplay ( g_mainwin2 ) ;
2012-07-21 17:15:39 +03:00
}
2012-07-23 09:55:45 +03:00
else if ( win = = g_sw2 )
{
/*
2012-11-19 15:32:44 +02:00
* Clear the screen
*/
2012-07-23 09:55:45 +03:00
glClearColor ( 0.3f , 0.3f , 0.3f , 1 ) ;
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
2012-11-20 07:25:14 +02:00
glutPostWindowRedisplay ( g_mainwin2 ) ;
2012-07-23 09:55:45 +03:00
}
2001-07-27 07:35:54 +03:00
else
2012-07-21 17:15:39 +03:00
{
2012-07-23 13:37:40 +03:00
const GLfloat time = glutGet ( GLUT_ELAPSED_TIME ) / 1000.f * 40 ;
2012-07-21 17:15:39 +03:00
/*
* Clear the screen
*/
glClearColor ( 0 , 0.5 , 1 , 1 ) ;
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
/*
* Have the cube rotated
*/
glMatrixMode ( GL_MODELVIEW ) ;
glPushMatrix ( ) ;
2012-07-23 13:37:40 +03:00
glRotatef ( time , 0 , 0 , 1 ) ;
glRotatef ( time , 0 , 1 , 0 ) ;
glRotatef ( time , 1 , 0 , 0 ) ;
2012-07-21 17:15:39 +03:00
/*
* And then drawn . . .
*/
glColor3f ( 1 , 1 , 0 ) ;
/* glutWireCube( 20.0 ); */
glutWireTeapot ( 20.0 ) ;
2012-07-23 13:37:40 +03:00
/* glutWireSphere( 15.0, 15, 15 ); */
2012-07-21 17:15:39 +03:00
/* glColor3f( 0, 1, 0 ); */
/* glutWireCube( 30.0 ); */
/* glutSolidCone( 10, 20, 10, 2 ); */
/*
* Don ' t forget about the model - view matrix
*/
glPopMatrix ( ) ;
/*
* Draw a silly text
*/
if ( g_InGameMode = = 0 )
PrintText ( 20 , 20 , " Hello there cruel world! " ) ;
else
PrintText ( 20 , 20 , " Press ESC to leave the game mode! " ) ;
}
2001-07-27 07:35:54 +03:00
/*
* And swap this context ' s buffers
*/
2003-11-11 13:18:16 +02:00
glutSwapBuffers ( ) ;
2012-07-21 17:15:39 +03:00
glutPostWindowRedisplay ( win ) ;
2001-07-27 07:35:54 +03:00
}
/*
* This is a sample idle function
*/
void SampleIdle ( void )
{
if ( g_LeaveGameMode = = 1 )
{
2012-07-23 13:37:40 +03:00
/* One could do all this just as well in SampleGameModeKeyboard... */
2012-11-20 07:25:14 +02:00
printf ( " leaving gamemode... \n " ) ;
2003-11-11 13:18:16 +02:00
glutLeaveGameMode ( ) ;
2001-07-27 07:35:54 +03:00
g_LeaveGameMode = 0 ;
2003-11-11 13:18:16 +02:00
g_InGameMode = 0 ;
2012-11-20 07:25:14 +02:00
glutPostWindowRedisplay ( g_mainwin1 ) ;
glutPostWindowRedisplay ( g_mainwin2 ) ;
glutPostWindowRedisplay ( g_sw1 ) ;
glutPostWindowRedisplay ( g_sw2 ) ;
2001-07-27 07:35:54 +03:00
}
2004-03-15 08:08:08 +02:00
}
2001-07-27 07:35:54 +03:00
2012-07-22 13:03:36 +03:00
void SampleEntry ( int state )
{
int window = glutGetWindow ( ) ;
printf ( " Window %d Entry Callback: %d \n " , window , state ) ;
}
2001-07-27 07:35:54 +03:00
/*
* The reshape function
*/
void SampleReshape ( int nWidth , int nHeight )
{
GLfloat fAspect = ( GLfloat ) nHeight / ( GLfloat ) nWidth ;
GLfloat fPos [ 4 ] = { 0.0f , 0.0f , 10.0f , 0.0f } ;
GLfloat fCol [ 4 ] = { 0.5f , 1.0f , 0.0f , 1.0f } ;
/*
* Update the viewport first
*/
glViewport ( 0 , 0 , nWidth , nHeight ) ;
/*
* Then the projection matrix
*/
glMatrixMode ( GL_PROJECTION ) ;
glLoadIdentity ( ) ;
glFrustum ( - 1.0 , 1.0 , - fAspect , fAspect , 1.0 , 80.0 ) ;
/*
* Move back the camera a bit
*/
glMatrixMode ( GL_MODELVIEW ) ;
2003-11-11 13:18:16 +02:00
glLoadIdentity ( ) ;
2001-07-27 07:35:54 +03:00
glTranslatef ( 0.0 , 0.0 , - 40.0f ) ;
/*
* Enable some features . . .
*/
glEnable ( GL_CULL_FACE ) ;
glEnable ( GL_DEPTH_TEST ) ;
glEnable ( GL_NORMALIZE ) ;
/*
* Set up some lighting
*/
glLightfv ( GL_LIGHT0 , GL_POSITION , fPos ) ;
glEnable ( GL_LIGHTING ) ;
glEnable ( GL_LIGHT0 ) ;
/*
* Set up a sample material
*/
glMaterialfv ( GL_FRONT , GL_AMBIENT_AND_DIFFUSE , fCol ) ;
}
/*
* A sample keyboard callback
*/
void SampleKeyboard ( unsigned char cChar , int nMouseX , int nMouseY )
{
2003-11-11 13:18:16 +02:00
printf ( " SampleKeyboard(): keypress '%c' at (%i,%i) \n " ,
cChar , nMouseX , nMouseY ) ;
2001-07-27 07:35:54 +03:00
}
/*
* A sample keyboard callback ( for game mode window )
*/
void SampleGameModeKeyboard ( unsigned char cChar , int nMouseX , int nMouseY )
{
if ( cChar = = 27 )
2003-11-11 13:18:16 +02:00
g_LeaveGameMode = 1 ;
2001-07-27 07:35:54 +03:00
}
/*
* A sample special callback
*/
void SampleSpecial ( int nSpecial , int nMouseX , int nMouseY )
{
2003-11-11 13:18:16 +02:00
printf ( " SampleSpecial(): special keypress %i at (%i,%i) \n " ,
nSpecial , nMouseX , nMouseY ) ;
2001-07-27 07:35:54 +03:00
}
/*
* A sample menu callback
*/
void SampleMenu ( int menuID )
{
2004-03-15 08:08:08 +02:00
printf ( " SampleMenu() callback executed, menuID is %i \n " , menuID ) ;
2001-07-27 07:35:54 +03:00
}
2012-11-17 03:02:30 +02:00
/*
* A sample menu status callback
*/
void SampleMenuStatus ( int status , int x , int y )
{
printf ( " SampleMenu() callback executed, MenuStatus is %i at (%i,%i) \n " , status , x , y ) ;
}
2001-07-27 07:35:54 +03:00
/*
* The sample ' s entry point
*/
int main ( int argc , char * * argv )
{
int menuID , subMenuA , subMenuB ;
glutInitDisplayString ( " stencil~2 rgb double depth>=16 samples " ) ;
glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ;
glutInitWindowPosition ( 100 , 100 ) ;
2003-11-11 13:20:01 +02:00
glutInit ( & argc , argv ) ;
2011-09-04 23:30:48 +03:00
glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE , GLUT_ACTION_GLUTMAINLOOP_RETURNS ) ;
2012-11-22 07:49:53 +02:00
glutMenuStatusFunc ( SampleMenuStatus ) ;
glutIdleFunc ( SampleIdle ) ;
2011-09-04 23:30:48 +03:00
2001-07-27 07:35:54 +03:00
subMenuA = glutCreateMenu ( SampleMenu ) ;
glutAddMenuEntry ( " Sub menu A1 (01) " , 1 ) ;
glutAddMenuEntry ( " Sub menu A2 (02) " , 2 ) ;
glutAddMenuEntry ( " Sub menu A3 (03) " , 3 ) ;
2004-03-15 08:08:08 +02:00
2001-07-27 07:35:54 +03:00
subMenuB = glutCreateMenu ( SampleMenu ) ;
glutAddMenuEntry ( " Sub menu B1 (04) " , 4 ) ;
glutAddMenuEntry ( " Sub menu B2 (05) " , 5 ) ;
glutAddMenuEntry ( " Sub menu B3 (06) " , 6 ) ;
glutAddSubMenu ( " Going to sub menu A " , subMenuA ) ;
menuID = glutCreateMenu ( SampleMenu ) ;
glutAddMenuEntry ( " Entry one " , 1 ) ;
glutAddMenuEntry ( " Entry two " , 2 ) ;
glutAddMenuEntry ( " Entry three " , 3 ) ;
glutAddMenuEntry ( " Entry four " , 4 ) ;
glutAddMenuEntry ( " Entry five " , 5 ) ;
glutAddSubMenu ( " Enter sub menu A " , subMenuA ) ;
glutAddSubMenu ( " Enter sub menu B " , subMenuB ) ;
2012-11-20 07:25:14 +02:00
g_mainwin1 = glutCreateWindow ( " Hello world! " ) ;
2001-07-27 07:35:54 +03:00
glutDisplayFunc ( SampleDisplay ) ;
glutReshapeFunc ( SampleReshape ) ;
glutKeyboardFunc ( SampleKeyboard ) ;
glutSpecialFunc ( SampleSpecial ) ;
2012-07-22 13:03:36 +03:00
glutEntryFunc ( SampleEntry ) ;
2001-07-27 07:35:54 +03:00
glutAttachMenu ( GLUT_LEFT_BUTTON ) ;
glutInitWindowPosition ( 200 , 200 ) ;
2012-11-20 07:25:14 +02:00
g_mainwin2 = glutCreateWindow ( " I am not Jan B. " ) ;
2001-07-27 07:35:54 +03:00
glutDisplayFunc ( SampleDisplay ) ;
glutReshapeFunc ( SampleReshape ) ;
glutKeyboardFunc ( SampleKeyboard ) ;
glutSpecialFunc ( SampleSpecial ) ;
2012-11-17 03:02:30 +02:00
glutEntryFunc ( SampleEntry ) ;
2001-07-27 07:35:54 +03:00
glutAttachMenu ( GLUT_LEFT_BUTTON ) ;
2012-07-21 06:58:04 +03:00
glutSetMenu ( subMenuA ) ;
2012-07-23 09:55:45 +03:00
glutAttachMenu ( GLUT_RIGHT_BUTTON ) ;
2001-07-27 07:35:54 +03:00
2012-11-20 07:25:14 +02:00
g_sw1 = glutCreateSubWindow ( g_mainwin2 , 200 , 0 , 100 , 100 ) ;
2012-07-21 17:15:39 +03:00
glutDisplayFunc ( SampleDisplay ) ;
glutSetMenu ( subMenuB ) ;
2012-07-23 13:37:40 +03:00
glutAttachMenu ( GLUT_LEFT_BUTTON ) ;
2012-07-21 17:15:39 +03:00
2012-07-23 09:55:45 +03:00
g_sw2 = glutCreateSubWindow ( g_sw1 , 50 , 0 , 50 , 50 ) ;
glutDisplayFunc ( SampleDisplay ) ;
glutSetMenu ( menuID ) ;
glutAttachMenu ( GLUT_RIGHT_BUTTON ) ;
2001-07-27 07:35:54 +03:00
printf ( " Testing game mode string parsing, don't panic! \n " ) ;
glutGameModeString ( " 320x240:32@100 " ) ;
glutGameModeString ( " 640x480:16@72 " ) ;
glutGameModeString ( " 1024x768 " ) ;
glutGameModeString ( " :32@120 " ) ;
2004-03-15 08:08:08 +02:00
glutGameModeString ( " Toudi glupcze, Danwin bedzie moj! " ) ;
2011-03-17 06:22:55 +02:00
glutGameModeString ( " 640x480:37@300 " ) ; /* this one should fail */
glutEnterGameMode ( ) ;
2001-07-27 07:35:54 +03:00
2011-03-17 06:22:55 +02:00
glutGameModeString ( " 800x600 " ) ; /* this one is likely to succeed */
2012-07-23 13:37:40 +03:00
g_gamemodewin = glutEnterGameMode ( ) ;
2011-03-17 06:22:55 +02:00
if ( glutGameModeGet ( GLUT_GAME_MODE_ACTIVE ) )
g_InGameMode = 1 ;
2001-07-27 07:35:54 +03:00
glutDisplayFunc ( SampleDisplay ) ;
glutReshapeFunc ( SampleReshape ) ;
glutKeyboardFunc ( SampleGameModeKeyboard ) ;
2012-11-17 03:02:30 +02:00
glutEntryFunc ( SampleEntry ) ;
2012-07-23 13:37:40 +03:00
glutSetMenu ( menuID ) ;
glutAttachMenu ( GLUT_LEFT_BUTTON ) ;
2001-07-27 07:35:54 +03:00
2011-09-04 23:30:48 +03:00
printf ( " current window is %ix%i at (%i,%i) \n " ,
2011-03-17 06:22:55 +02:00
glutGet ( GLUT_WINDOW_WIDTH ) , glutGet ( GLUT_WINDOW_HEIGHT ) ,
glutGet ( GLUT_WINDOW_X ) , glutGet ( GLUT_WINDOW_Y )
2004-03-15 08:08:08 +02:00
) ;
2001-07-27 07:35:54 +03:00
2013-02-28 10:53:31 +02:00
/*
* Describe pixel format
*/
printf ( " The current window has %i red bits, %i green bits, %i blue bits and %i alpha bits for a total buffer size of %i bits \n " , glutGet ( GLUT_WINDOW_RED_SIZE ) , glutGet ( GLUT_WINDOW_GREEN_SIZE ) , glutGet ( GLUT_WINDOW_BLUE_SIZE ) , glutGet ( GLUT_WINDOW_ALPHA_SIZE ) , glutGet ( GLUT_WINDOW_BUFFER_SIZE ) ) ;
printf ( " It furthermore has %i depth bits and %i stencil bits \n " , glutGet ( GLUT_WINDOW_DEPTH_SIZE ) , glutGet ( GLUT_WINDOW_STENCIL_SIZE ) ) ;
2001-07-27 07:35:54 +03:00
/*
* Enter the main FreeGLUT processing loop
*/
glutMainLoop ( ) ;
/*
2012-07-23 13:37:40 +03:00
* returned from mainloop after window closed
* see glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE , GLUT_ACTION_GLUTMAINLOOP_RETURNS ) ; above
2001-07-27 07:35:54 +03:00
*/
2012-07-23 13:37:40 +03:00
printf ( " glutMainLoop() termination works fine! \n " ) ;
2003-11-11 13:18:16 +02:00
return EXIT_SUCCESS ;
2001-07-27 07:35:54 +03:00
}
2012-11-19 15:32:44 +02:00
/*** END OF FILE ***/