Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7814391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:10:47+00:00 2026-06-02T05:10:47+00:00

We’re making a game engine for our c++ class, and we’re mostly done with

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:10:48+00:00Added an answer on June 2, 2026 at 5:10 am

    I think there an error in your fonction

    int timeThisFrame = /* do you mean */ _deltaFrameTime;  //SDL_GetTicks() - _newFrameTime;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a reasonable size flat file database of text documents mostly saved in
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.