00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string>
00004
00005 #include "rSDL.h"
00006
00007
00008 #ifdef SDL_GL_DOUBLEBUFFER
00009 #endif
00010
00011 #define HAVE_OPENGL 1
00012
00013
00014 #ifdef HAVE_OPENGL
00015 #ifdef WIN32
00016 #include <windows.h>
00017 #endif
00018 #include <rGL.h>
00019
00020
00021 int HandleEvent(SDL_Event *tEvent)
00022 {
00023 int done;
00024
00025 done = 0;
00026 switch( tEvent->type ) {
00027 case SDL_ACTIVEEVENT:
00028
00029 printf( "app %s ", tEvent->active.gain ? "gained" : "lost" );
00030 if ( tEvent->active.state & SDL_APPACTIVE ) {
00031 printf( "active " );
00032 } else if ( tEvent->active.state & SDL_APPMOUSEFOCUS ) {
00033 printf( "mouse " );
00034 } else if ( tEvent->active.state & SDL_APPINPUTFOCUS ) {
00035 printf( "input " );
00036 }
00037 printf( "focus\n" );
00038 break;
00039
00040 case SDL_KEYDOWN:
00041 if( tEvent->key.keysym.sym == SDLK_ESCAPE ) {
00042 done = 1;
00043 }
00044 printf("key '%s' pressed\n",
00045 SDL_GetKeyName(tEvent->key.keysym.sym));
00046 break;
00047 case SDL_QUIT:
00048 done = 1;
00049 break;
00050 }
00051 return(done);
00052 }
00053
00054 int RunGLTest( int argc, char* argv[] )
00055 {
00056 int i;
00057 int w = 640;
00058 int h = 480;
00059 int bpp = 16;
00060 double color = 0.0;
00061 int box = 50;
00062 int x0 = w / 2 - box;
00063 int x1 = w / 2 + box;
00064 int y0 = h / 2 - box;
00065 int y1 = h / 2 + box;
00066 int done = 0;
00067 float cube[8][3]= {{ 0.5, 0.5, -0.5},
00068 { 0.5, -0.5, -0.5},
00069 {-0.5, -0.5, -0.5},
00070 {-0.5, 0.5, -0.5},
00071 {-0.5, 0.5, 0.5},
00072 { 0.5, 0.5, 0.5},
00073 { 0.5, -0.5, 0.5},
00074 {-0.5, -0.5, 0.5}};
00075 Uint32 video_flags;
00076 int value;
00077
00078 if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
00079 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
00080 exit( 1 );
00081 }
00082
00083
00084 video_flags = SDL_OPENGL;
00085
00086 for ( i=1; argv[i]; ++i ) {
00087 if ( strcmp(argv[1], "-fullscreen") == 0 ) {
00088 video_flags |= SDL_FULLSCREEN;
00089 }
00090 }
00091
00092
00093 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
00094 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
00095 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
00096 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
00097 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
00098 if ( SDL_SetVideoMode( w, h, bpp, video_flags ) == NULL ) {
00099 fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
00100 SDL_Quit();
00101 exit(1);
00102 }
00103
00104 SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value );
00105 printf( "SDL_GL_RED_SIZE: requetsed 5, got %d\n", value );
00106 SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value );
00107 printf( "SDL_GL_GREEN_SIZE: requested 5, got %d\n", value);
00108 SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value );
00109 printf( "SDL_GL_BLUE_SIZE: requested 5, got %d\n", value );
00110 SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value );
00111 printf( "SDL_GL_DEPTH_SIZE: requested 16, got %d\n", value );
00112 SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value );
00113 printf( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value );
00114
00115
00116 SDL_WM_SetCaption( "SDL GL test", "testgl" );
00117
00118 glViewport( 0, 0, w, h );
00119 glMatrixMode( GL_PROJECTION );
00120 glLoadIdentity( );
00121
00122 glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 );
00123
00124 glMatrixMode( GL_MODELVIEW );
00125 glLoadIdentity( );
00126
00127 glEnable(GL_DEPTH_TEST);
00128
00129 glDepthFunc(GL_LESS);
00130
00131
00132 while( !done ) {
00133 GLenum glError;
00134 char* sdlError;
00135 SDL_Event tEvent;
00136
00137
00138 glClearColor( 0.0, 0.0, 0.0, 1.0 );
00139 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00140
00141 glBegin( GL_QUADS );
00142 glColor3f(1.0, 0.0, 0.0);
00143 glVertex3fv(cube[0]);
00144 glVertex3fv(cube[1]);
00145 glVertex3fv(cube[2]);
00146 glVertex3fv(cube[3]);
00147
00148 glColor3f(0.0, 1.0, 0.0);
00149 glVertex3fv(cube[3]);
00150 glVertex3fv(cube[4]);
00151 glVertex3fv(cube[7]);
00152 glVertex3fv(cube[2]);
00153
00154 glColor3f(0.0, 0.0, 1.0);
00155 glVertex3fv(cube[0]);
00156 glVertex3fv(cube[5]);
00157 glVertex3fv(cube[6]);
00158 glVertex3fv(cube[1]);
00159
00160 glColor3f(0.0, 1.0, 1.0);
00161 glVertex3fv(cube[5]);
00162 glVertex3fv(cube[4]);
00163 glVertex3fv(cube[7]);
00164 glVertex3fv(cube[6]);
00165
00166 glColor3f(1.0, 1.0, 0.0);
00167 glVertex3fv(cube[5]);
00168 glVertex3fv(cube[0]);
00169 glVertex3fv(cube[3]);
00170 glVertex3fv(cube[4]);
00171
00172 glColor3f(1.0, 0.0, 1.0);
00173 glVertex3fv(cube[6]);
00174 glVertex3fv(cube[1]);
00175 glVertex3fv(cube[2]);
00176 glVertex3fv(cube[7]);
00177 glEnd( );
00178
00179 glMatrixMode(GL_MODELVIEW);
00180 glRotatef(5.0, 1.0, 1.0, 1.0);
00181
00182 SDL_GL_SwapBuffers( );
00183
00184 color += 0.01;
00185
00186 if( color >= 1.0 ) {
00187 color = 0.0;
00188 }
00189
00190
00191 glError = glGetError( );
00192
00193 if( glError != GL_NO_ERROR ) {
00194 fprintf( stderr, "testgl: OpenGL error: %d\n", glError );
00195 }
00196
00197 sdlError = SDL_GetError( );
00198
00199 if( sdlError[0] != '\0' ) {
00200 fprintf(stderr, "testgl: SDL error '%s'\n", sdlError);
00201 SDL_ClearError();
00202 }
00203
00204
00205 SDL_Delay( 20 );
00206
00207
00208 while( SDL_PollEvent( &tEvent ) ) {
00209 done = HandleEvent(&tEvent);
00210 }
00211 }
00212
00213
00214 SDL_Quit( );
00215 }
00216
00217 int main(int argc, char *argv[])
00218 {
00219 int i;
00220 int numtests;
00221
00222 numtests = 1;
00223 for ( i=1; argv[i]; ++i ) {
00224 if ( strcmp(argv[1], "-twice") == 0 ) {
00225 ++numtests;
00226 }
00227 }
00228 for ( i=0; i<numtests; ++i ) {
00229 RunGLTest(argc, argv);
00230 }
00231 exit( 0 );
00232 }
00233
00234 #else
00235
00236 int main(int argc, char *argv[])
00237 {
00238 printf("No OpenGL support on this system\n");
00239 exit(1);
00240 }
00241
00242 #endif