Search code examples
c++sdlgame-engineframe-rategame-loop

SDL - FPS problems with LoopTimer in c++


We're making a game engine for our c++ class, and we're mostly done with everything except we're having some minor issues with the FPS. It always runs about 10-20% slower than what we set it to, and we're wondering if this is due to a bug/crappy code or if it's just the way it is? It Caps at 350 if we set it to be 500+, but that's mostly due to the computer not being able to handle more. If we set it to 50, we get around 40-44.

Here's some snippets from the LoopTimer class handling the timer

Header:

class LoopTimer {
public:
    LoopTimer(const int fps = 50);
    ~LoopTimer();

    void Update();
    void SleepUntilNextFrame();

    float GetFPS() const;
    float GetDeltaFrameTime() const { return _deltaFrameTime; }

    void SetWantedFPS(const int wantedFPS);
    void SetAccumulatorInterval(const int accumulatorInterval);

private:
    int _wantedFPS;
    int _oldFrameTime;
    int _newFrameTime;
    int _deltaFrameTime;
    int _frames;
    int _accumulator;
    int _accumulatorInterval;
    float _averageFPS;
};

CPP file

LoopTimer::LoopTimer(const int fps)
{
    _wantedFPS = fps;
    _oldFrameTime = 0;
    _newFrameTime = 0;
    _deltaFrameTime = 0;
    _frames = 0;
    _accumulator = 0;
    _accumulatorInterval = 1000;
    _averageFPS = 0.0;
}
void LoopTimer::Update()
{
    _oldFrameTime = _newFrameTime;
    _newFrameTime = SDL_GetTicks();
    _deltaFrameTime = _newFrameTime - _oldFrameTime;

    _frames++;
    _accumulator += _deltaFrameTime;

    if(_accumulatorInterval < _accumulator)
    {
        _averageFPS = static_cast<float>(_frames / (_accumulator / 1000.f));
        //cout << "FPS: " << fixed << setprecision(1) << _averageFPS << endl;
        _frames = 0;
        _accumulator = 0;
    }
}


void LoopTimer::SleepUntilNextFrame()
{
    int timeThisFrame = SDL_GetTicks() - _newFrameTime;
    if(timeThisFrame < (1000 / _wantedFPS))
    {
        // Sleep the remaining frame time.
        SDL_Delay((1000 / _wantedFPS) - timeThisFrame);
    }
}

The update is basically being called in our GameManager in the run method:

int GameManager::Run()
{
while(QUIT != _gameStatus)
    {
        SDL_Event ev;

        while(SDL_PollEvent(&ev))
    {
            _HandleEvent(&ev);
    }

    _Update();
    _Draw();

    _timer.SleepUntilNextFrame();
   }    
    return 0;
}

I can give out some more code if needed, or i'd be happy to give out the code to anyone who might need it. It's quite a lot going on including sdl_net functions and whatnot so there's no point dumping it all onto here.

Anyway, I hope someone has a clever tip regarding the framerate, either how to improve it, a different looptimer function or just by telling me it's normal to have a small loss in fps :)


Solution

  • I think there an error in your fonction

    int timeThisFrame = /* do you mean */ _deltaFrameTime;  //SDL_GetTicks() - _newFrameTime;