0
\$\begingroup\$

When I run the program at 60 fps, I get 0.016 seconds per frame most of the time, then 0.017 s once every few frames. A similar thing happens for 30 fps, varying between 0.033 and 0.034 seconds per frame.

Should my framerate and frame duration be stable? Even when nothing different is happening in each frame?

while (m_bRunning) { m_Timer.Start(); m_frameStart = SDL_GetTicks(); m_frameCount += 1; HandleEvent(e); Update(); Draw(); m_Timer.Pause(); if(m_frameDelay >= m_Timer.GetTicks()) { SDL_Delay(m_frameDelay - m_Timer.GetTicks()); } m_deltaTime = (double(SDL_GetTicks() - m_frameStart)/1000.f); return m_bRunning; } 
\$\endgroup\$
4
  • \$\begingroup\$ Note that this is exactly what you'd expect to see, since 1000 milliseconds does not divide evenly into either 30 or 60. So to run at 60 fps consistently, you can't always have 16 ms frames (too short - after 60 of them you've passed only 0.96 of a second) or 17 ms frames (too long - 60 of those would put you over 1.02 seconds), you have to have about two thirds of your frames 17 ms long and the other third 16 ms - or all frames at a fractional 16.3333333333333333333... etc. \$\endgroup\$ Commented Apr 20, 2023 at 15:43
  • \$\begingroup\$ thanks, you must be onto something aye. because 0.96 and 1.02 are the numbers i get so am i doing it right? by right i mean is this passable for a game to use that doesnt need to run for days or anything like that \$\endgroup\$ Commented Apr 22, 2023 at 3:38
  • \$\begingroup\$ It's unclear to me what problem you're concerned about here. \$\endgroup\$ Commented Apr 22, 2023 at 3:40
  • \$\begingroup\$ you say its what i should expect im asking if that means its right \$\endgroup\$ Commented Apr 22, 2023 at 3:57

1 Answer 1

0
\$\begingroup\$

If you are using V-Sync in the renderer via flag SDL_RENDERER_PRESENTVSYNC then the 0.001 difference is nothing you can control because of how communication between GPU and CPU works. However, if you are halting your loop with a Sleep() function to get to 0.016s, assuming you do your calculations correctly, you should know that any Sleep() call is never accurate. I.e. Sleep(1 : milliseconds) is never guaranteed to last exactly 1 millisecond because of how kernels and their task scheduling works. When you call Sleep() your cpu does some other work in another place, unrelated to your process (it can take more than 1ms for it to resume your process).

\$\endgroup\$
2
  • \$\begingroup\$ If I run a fraps type program I should see 30 or 60 fps constantly? \$\endgroup\$ Commented Mar 28, 2023 at 15:21
  • \$\begingroup\$ I'm not entirely sure how programs like Fraps measure frame time accurately, but in Fraps' case, it is displaying the FPS number as an integer, so when your frame time stutters from 0.016 to 0.017 Fraps is just getting rid of the fractional part and you would see most certainly 30 or 60 constantly yes :) \$\endgroup\$ Commented Mar 28, 2023 at 23:03

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.