Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

SDLSystem.cpp

Go to the documentation of this file.
00001 
00006 
00007 
00008 #ifdef WIN32
00009 #include <windows.h>
00010 #pragma warning (disable: 4786)
00011 #endif //WIN32
00012 
00013 #include <GL/gl.h>
00014 
00015 #include <SDL/SDL.h>
00016 
00017 #include "SDLSystem.h"
00018 #include "SDLKeyboardDevice.h"
00019 
00020 
00021 SDLSystem* gSDLSystem = NULL;
00022 
00023 
00024 SCA_ISystem* NewSDLSystem(void)

00025 {
00026         if(!gSDLSystem)
00027                 gSDLSystem = new SDLSystem();
00028 
00029         return gSDLSystem;
00030 }
00031 
00032 
00033 SDLSystem::SDLSystem()

00034 {
00035   int bpp = 16;
00036   float gamma = 0.0;
00037 
00038         int rgb_size[3];
00039         int w = 640;
00040         int h = 480;
00041         Uint32 video_flags;
00042         int value;
00043 
00044         if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
00045         {
00046                 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
00047                 exit( 1 );
00048         }
00049 
00050         /* See if we should detect the display depth */
00051         if(bpp == 0)
00052         {
00053                 if(SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8)
00054                         bpp = 8;
00055                 else
00056                         bpp = 16;  /* More doesn't seem to work */
00057         }
00058 
00059         /* Set the flags we want to use for setting the video mode */
00060         video_flags = SDL_OPENGL /*| SDL_FULLSCREEN*/;
00061 
00062         /* Initialize the display */
00063         switch(bpp)
00064         {
00065                 case 8:
00066                                 rgb_size[0] = 2;
00067                                 rgb_size[1] = 3;
00068                                 rgb_size[2] = 3;
00069                                 break;
00070     case 15:
00071     case 16:
00072                                 rgb_size[0] = 5;
00073                                 rgb_size[1] = 5;
00074                                 rgb_size[2] = 5;
00075                                 break;
00076     default:
00077                                 rgb_size[0] = 8;
00078                                 rgb_size[1] = 8;
00079                                 rgb_size[2] = 8;
00080                                 break;
00081         }
00082         SDL_GL_SetAttribute( SDL_GL_RED_SIZE, rgb_size[0] );
00083         SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, rgb_size[1] );
00084         SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, rgb_size[2] );
00085         SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
00086         SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
00087 
00088         if(SDL_SetVideoMode(w, h, bpp, video_flags) == NULL)
00089         {
00090                 fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
00091                 SDL_Quit();
00092                 exit(1);
00093         }
00094 
00095         printf("\n");
00096         printf( "Vendor     : %s\n", glGetString( GL_VENDOR ) );
00097         printf( "Renderer   : %s\n", glGetString( GL_RENDERER ) );
00098         printf( "Version    : %s\n", glGetString( GL_VERSION ) );
00099         printf( "Extensions : %s\n", glGetString( GL_EXTENSIONS ) );
00100         printf("\n");
00101 
00102         SDL_GL_GetAttribute( SDL_GL_RED_SIZE, &value );
00103         printf( "SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0],value);
00104         SDL_GL_GetAttribute( SDL_GL_GREEN_SIZE, &value );
00105         printf( "SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1],value);
00106         SDL_GL_GetAttribute( SDL_GL_BLUE_SIZE, &value );
00107         printf( "SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2],value);
00108         SDL_GL_GetAttribute( SDL_GL_DEPTH_SIZE, &value );
00109         printf( "SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value );
00110         SDL_GL_GetAttribute( SDL_GL_DOUBLEBUFFER, &value );
00111         printf( "SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value );
00112 
00113         /* Set the window manager title bar */
00114         SDL_WM_SetCaption( "SDL GL test", "testgl" );
00115 
00116         /* Set the gamma for the window */
00117         if(gamma != 0.0)
00118                 SDL_SetGamma(gamma, gamma, gamma);
00119 
00120         glViewport(0, 0, w, h);
00121         glMatrixMode(GL_PROJECTION);
00122         glLoadIdentity();
00123 
00124         glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
00125 
00126         glMatrixMode(GL_MODELVIEW);
00127         glLoadIdentity();
00128 
00129         glEnable(GL_DEPTH_TEST);
00130 
00131         glDepthFunc(GL_LESS);
00132 
00133         glShadeModel(GL_SMOOTH);
00134 }
00135 
00136 
00137 void SDLSystem::NextFrame()

00138 {
00139   std::vector<SCA_IInputDevice*>::iterator idev;
00140   for (idev = m_inputDevices.begin();!(idev == m_inputDevices.end());idev++)
00141   {
00142     (*idev)->NextFrame();
00143   }
00144 
00145 #if 0
00146 // original SDL sample code...
00147         int done = 0;
00148 
00149   float color[8][3]= {{ 1.0,  1.0,  0.0},
00150                             { 1.0,  0.0,  0.0},
00151                             { 0.0,  0.0,  0.0},
00152                             { 0.0,  1.0,  0.0},
00153                             { 0.0,  1.0,  1.0},
00154                             { 1.0,  1.0,  1.0},
00155                             { 1.0,  0.0,  1.0},
00156                             { 0.0,  0.0,  1.0}};
00157         float cube[8][3]= {{ 0.5,  0.5, -0.5},
00158                            { 0.5, -0.5, -0.5},
00159                            {-0.5, -0.5, -0.5},
00160                            {-0.5,  0.5, -0.5},
00161                            {-0.5,  0.5,  0.5},
00162                            { 0.5,  0.5,  0.5},
00163                            { 0.5, -0.5,  0.5},
00164                            {-0.5, -0.5,  0.5}};
00165 
00166                 GLenum gl_error;
00167                 char* sdl_error;
00168 
00169                 /* Do our drawing, too. */
00170                 glClearColor( 0.0, 0.0, 0.0, 1.0 );
00171                 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00172 
00173                 glBegin( GL_QUADS );
00174 
00175                         glColor3fv(color[0]);
00176                         glVertex3fv(cube[0]);
00177                         glColor3fv(color[1]);
00178                         glVertex3fv(cube[1]);
00179                         glColor3fv(color[2]);
00180                         glVertex3fv(cube[2]);
00181                         glColor3fv(color[3]);
00182                         glVertex3fv(cube[3]);
00183                         
00184                 glEnd( );
00185                 
00186                 glMatrixMode(GL_MODELVIEW);
00187                 glRotatef(5.0, 1.0, 1.0, 1.0);
00188 
00189                 SDL_GL_SwapBuffers();
00190 
00191                 /* Check for error conditions. */
00192 #if 0
00193                 gl_error = glGetError( );
00194 
00195                 if(gl_error != GL_NO_ERROR)
00196                         fprintf(stderr, "testgl: OpenGL error: %d\n", gl_error);
00197 #endif
00198 
00199                 sdl_error = SDL_GetError();
00200 
00201                 if(sdl_error[0] != '\0')
00202                 {
00203                         fprintf(stderr, "testgl: SDL error '%s'\n", sdl_error);
00204                         SDL_ClearError();
00205                 }
00206 
00207                 /* Allow the user to see what's happening */
00208 //              SDL_Delay(20);
00209 #endif
00210 }
00211 
00212 
00213 void SDLSystem::StartMainLoop()

00214 {
00215         Uint32 start_time, this_time;
00216         int frames;
00217   int done = 0;
00218   SDL_Event event;
00219 
00220         /* Loop until done. */
00221         start_time = SDL_GetTicks();
00222         frames = 0;
00223         
00224         //while(!done)
00225         // there is already a flag in the system for this :)
00226         while(!this->IsExitRequested())
00227         {
00228                 // call the ISystem loop callback once
00229                 DoMainLoopCallback();
00230 
00231                 NextFrame();
00232 
00233                 /* Check if there's a pending event. */
00234                 while(SDL_PollEvent(&event))
00235                         done = HandleEvent(&event);
00236 
00237                 ++frames;
00238         }
00239 
00240         /* Print out the frames per second */
00241         this_time = SDL_GetTicks();
00242         if(this_time != start_time)
00243                 printf("%2.2f FPS\n", ((float)frames / (this_time - start_time)) * 1000.0);
00244 }
00245 
00246 
00247 int SDLSystem::HandleEvent(SDL_Event *event)

00248 {
00249         int done;
00250 
00251         done = 0;
00252         switch( event->type )
00253         {
00254           case SDL_ACTIVEEVENT:
00255                                 /* See what happened */
00256                                 printf( "app %s ", event->active.gain ? "gained" : "lost" );
00257                                 if(event->active.state & SDL_APPACTIVE)
00258                                         printf("active ");
00259                                 else
00260           if(event->active.state & SDL_APPMOUSEFOCUS)
00261                                         printf( "mouse " );
00262                                 else
00263             if(event->active.state & SDL_APPINPUTFOCUS)
00264                                         printf( "input " );
00265 
00266                                 printf( "focus\n" );
00267                                 break;
00268                 
00269 
00270     case SDL_KEYDOWN:
00271                 {
00272                                 //printf("key '%s' pressed\n",
00273                                 //SDL_GetKeyName(event->key.keysym.sym));
00274                                 SDLKeyboardDevice* keydev = (SDLKeyboardDevice*) this->GetKeyboardDevice();
00275                                 if (keydev->ConvertSDLEvent(event->key.keysym.sym,1))
00276                                 {
00277                                         this->m_exitrequested=true;
00278                                 }
00279 
00280                                 break;
00281                 }
00282         case SDL_KEYUP:
00283                 {
00284 
00285                                 SDLKeyboardDevice* keydev = (SDLKeyboardDevice*) this->GetKeyboardDevice();
00286                                 keydev->ConvertSDLEvent(event->key.keysym.sym,0);
00287                                 break;
00288                 }
00289 
00290     case SDL_QUIT:
00291                                 done = 1;
00292                                 break;
00293         }
00294         return(done);
00295 }
00296 
00297 
00298 double SDLSystem::GetTimeInSeconds()

00299 {
00300         return 0;
00301 }
00302 
00303 
00304 // Some helper functions
00305 #if 0
00306 void SDLDisplay(void)

00307 {
00308         if (gSDLSystem)
00309                 gSDLSystem->DoMainLoopCallback();
00310 
00311         // this is hopefully done by the client later on
00312         //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00313     //glFlush();
00314     //SDLSwapBuffers();
00315 }
00316 
00317 void MyIdleFunc(void)

00318 {
00319 //      SDLPostRedisplay();
00320 }
00321 char *fake_argv[] =
00322 {
00323   "program",
00324 //  "-display",
00325 //  ":0",
00326 //  "-geometry",
00327 //  "500x400-34-23",
00328 //  "-indirect",
00329 //  "-iconic",
00330   NULL};
00331 
00332 int fake_argc = sizeof(fake_argv) / sizeof(char *) - 1;
00333 
00334 
00335 void SDLKeyboardUp(unsigned char key, int x, int y)

00336 {
00337         if(gSDLSystem)
00338                 gSDLSystem->AddKey(key,0);
00339 }
00340 
00341 void SDLKeyboardDown(unsigned char key, int x, int y)

00342 {
00343         if(gSDLSystem)
00344                 gSDLSystem->AddKey(key,1);
00345 }
00346 #endif

Generated at Thu Feb 1 13:03:11 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000