[SDL] Slow OpenGL code

SkunkGuru skunkguru at gmail.com
Sun Apr 29 05:50:32 PDT 2007


Hi all,
I'm doing some test with OpenGl, but with strange results.

Here is some code:

bool UseOpenGL = true;  // Use OpenGL
//bool UseOpenGL = false;  // Use Standard SDL functions

if(UseOpenGL) {
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );
    glPointSize( 2.0f );
}

bool done          = false;
int NumberOfPoints = 0;
int PosX, PosY;

while (!done) {
    PosX = rand() % screen_width;
    PosY = rand() % screen_height;

    if(!UseOpenGL) {
       R = rand() % 255;
       G = rand() % 255;
       B = rand() % 255;
       color = SDL_MapRGB(Surface->format, R, G, B);
       // this funcions changes surface->pixels and updates screen
       DrawPixel(PosX, PosY, color, true);
       NumberOfPoints++;
    }
    else {
       R = (float)rand() / RAND_MAX;
       G = (float)rand() / RAND_MAX;
       B = (float)rand() / RAND_MAX;
       glBegin(GL_POINTS);
          glColor3f( R , G , B );
          glVertex2d( PosX, PosY );
       glEnd();

       SDL_GL_SwapBuffers();
       NumberOfPoints++;
    }

    if( SDL_GetTicks() > StartTicks + 5000) { done = true; }
}


It's a little intro for a game: it starts with a black screen,
then it draws random pixels with random colours.
After 5 seconds, it ends.

If variable UseOpenGL is true, I use OpenGL functions,
otherwise I use standard SDL functions.

The problem is that when UseOpenGL is false, I'm using
SDL functions and I can draw 243664 points;
with OpenGl functions, only 393


I'm using WinXP Pro with a GeForce2 MX card. My setup code:

Surface = SDL_SetVideoMode(640, 480, 0, videoFlags);

/* the flags to pass to SDL_SetVideoMode */
videoFlags  = SDL_OPENGL;          /* Enable OpenGL in SDL */
videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
videoFlags |= SDL_HWPALETTE;       /* Store the palette in hardware */

/* This checks to see if surfaces can be stored in memory */
if ( videoInfo->hw_available ) videoFlags |= SDL_HWSURFACE;
else videoFlags |= SDL_SWSURFACE;

/* This checks if hardware blits can be done */
if ( videoInfo->blit_hw ) videoFlags |= SDL_HWACCEL;

/* Sets up OpenGL double buffering */
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );


I have several games that use OpenGl,
so I don't think it could be a driver problem.


What's wrong in my code ?


-- 
SkunkGuru



More information about the SDL mailing list