# SDL and slow FPS

## Recommended Posts

I originally posted this in the scripting forum as I'm using pygame, but I wrote a quick test app in C++ and it turns out the problem is with something outside of pygame so I thought I'd drop a line here since I wasn't getting any help there. I wrote a super simple program to test for FPS when you're doing nothing but flipping, here it is: timer.h
class Timer
{
public:
Timer()
{
numFrames = 0;
startTime = SDL_GetTicks() - 1;
fps = 0;
}

void Frame()
{
if ( SDL_GetTicks() - startTime >= 1000)
{
startTime = SDL_GetTicks();
fps = numFrames;
numFrames = 0;
}

numFrames++;
}

double GetFPS()
{
return fps;
}

private:
Uint32 numFrames;
Uint32 startTime;
Uint32 fps;

};

timer.cpp
#include "SDL/SDL.h"
#include "timer.h"
#include <iostream>

int main()
{

SDL_Init(SDL_INIT_VIDEO);

SDL_Surface *screen = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE | SDL_DOUBLEBUF);

if( screen == NULL )
exit(1);

Timer timer;

while(true)
{
timer.Frame();
SDL_Flip(screen);

std::cout << timer.GetFPS() << std::endl;
}

return 0;
}

As you can see all it does is constantly flip in a tight loop, while keeping track of the current FPS. When I'm not flipping I get an FPS of between 15k-23k. When I flip It won't get up above 43 fps. I have no idea why but I'd *really* appreciate any input into this please. I'm running a 2600+ AMD athlon-xp with 768MB ram with an nvidia Geforce FX with Gentoo Linux as my OS. My current refresh rate is 75hz I get up over 10k fps when running glxgears so I know my vid card is setup correctly :) I've looked through my kernel and verified that I've got the correct drivers installed for my mobo and agpgart is currently loaded as a module. Does anyone have any ideas? Even if it's just a direction for me to look into I'd really appreciate it.

##### Share on other sites
Try it in software mode- you are using a double buffer so swapping the buffers and then getting it to your gfx card might be slow.

##### Share on other sites
That had no effect. I played around with it and I found something a bit strange. If I do an SDL_UpdateRect(0,0,640,480) I see about the same performance. However, if I do an SDL_UpdateRect(1,0,640,480) the performance skyrockets to around 18k fps.

I think the 18k fps is probably due to a bug in my timer, but regardless, it was showing a dramatic improvement from that 1 change. Does anyone have any ideas about why that would be? I tried doing it the opposite way, starting at 0,0 and taking a pixel or two off the width/height, but it had no effect on the performance.

I'm also wondering if my fps counter is a bit screwy as those numbers seem a bit high, even for what I'm doing. Anyone mind looking over it and telling me if they see anything? 1 small bug that I fixed is changing the startTime = SDL_GetTicks(); to startTime = SDL_GetTicks() - 1; ni the frame, but the results were still consistent.

##### Share on other sites
Quote:
 When I'm not flipping I get an FPS of between 15k-23k. When I flip It won't get up above 43 fps.

I'd guess that it's waiting for vsync, possibly with some other delay holding things up. Which video driver mode are you running SDL under? (eg. have you set the environment variable SDL_VIDEODRIVER to "dga"?)

Quote:
 I get up over 10k fps when running glxgears so I know my vid card is setup correctly :)

Never compare 3D performance (ie. OpenGL) with 2D performance as they use almost totally different code paths and hardware on the gfx card and drivers.

By the way, SDL_UpdateRect(0,0,640,480) just calls SDL_Flip, hence the performance difference you see.

##### Share on other sites
I honestly don't know what vid mode SDL is running under, how would I go about checking that? I would assume jus the default X11. I'll try your suggest with dga and see if that helps me at all.

Thanks for the advice, I'll let you know what my results are :)

##### Share on other sites
For the record, the above was me. I'm at work and forgot to log in.

##### Share on other sites
I remember when i made my pong game with SDL i had very ugly fps since i did start using SDL_GetTicks().

I was calling it in a while ( that was never ending ) and because it was called so many time, that after a short while (6-7 sec) my game started to lag.

I only changed my code a bit so i wouldn't call SDL_GetTicks() too often.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627700
• Total Posts
2978695

• 21
• 14
• 12
• 10
• 12