Hello, I have this problem where basically everything in my game loop except flipping buffers is done within ~2 milliseconds, but when I flip the buffer with SDL_GL_SwapBuffers() that alone takes ~60ms.
Im not that experienced with OpenGL or SDL. Here is the initialization code:
void startGL() {
if (SDL_Init(SDL_INIT_VIDEO) < 0)
throw "SDL failed";
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_SetVideoMode(width, height, 24, SDL_OPENGL);
SDL_WM_SetCaption("Tank vs Aliens", NULL);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, width, 0, height, -1, 0 );
glClearColor( 0.0, 0.0, 0.0, 0.0);
glEnable( GL_TEXTURE_2D );
glShadeModel( GL_FLAT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
checkGlErrors();
}
And here is my game loop where I use my xTimer to time it:
void GameLoop() {
bool keepOn = true;
while ( keepOn ) {
xTimer t(1);
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
keepOn = events.checkEvents();
draw();
std::cout << "t1: " << (int)t.Elapsedms() << " ms.\t";
xTimer t2(1);
SDL_GL_SwapBuffers();
std::cout << "t2: " << (int)t2.Elapsedms() << " ms.\n";
SDL_Delay(2);
}
}
Here is the requested source code for the drawing.
void draw() {
glBindTexture(GL_TEXTURE_2D, bugTexture.texture );
checkGlErrors();
glBegin( GL_QUADS );
for (int i=0; i<maxBugs; ++i) {
/* alot of math where I calculate
x, y, dx1, dx2. The reason I use
dx1 and dx2 is to avoid glRotate and glTranslate
which have some overhead. */
glTexCoord2f(0.0, 0.0);
glVertex2f( x-dx1, y-dy1 ); //lower left
glTexCoord2f(1.0, 0.0);
glVertex2f( x+dx2, y+dy2 ); //lower right
glTexCoord2f(1.0, 1.0);
glVertex2f( x+dx1, y+dy1 ); //upper right
glTexCoord2f(0.0, 1.0);
glVertex2f( x-dx2, y-dy2 ); //upper left
}
glEnd();
return;
}
The compiled exe can be found
here. (121kb)
It will print in a command window, the values of the two timers in each loop.
My t1 (everything except flippingbuffers) stays around 2-3 ms.
But my t2 (only flipping buffers) stays around 60ms.
I know this sounds like a hardware thing, but many games work perfectly, and I have the newest graphics drivers.
It's an AthlonXP 1800+ with a Radeon 8500le, and 512mb of ram.
[Edited by - PureW on May 20, 2007 6:02:40 AM]