0

I am trying to limit a SDL program to 60 FPS and calculate the FPS using this code:

static const Uint32 min_frame_time = 16;
Uint32 start_time = SDL_GetTicks();

// Rendering stuff...

time_delta = SDL_GetTicks() - start_time;
fps_sum += 1000.0 / (float)time_delta;
fps_count++;

if(fps_count >= fps_max_count)
{
    printf("FPS: %f\n", fps_sum / (float)fps_count);
    fps_count = 0;
    fps_sum = 0.0;
}

if(time_delta < min_frame_time)
    SDL_Delay(min_frame_time - time_delta);

But it seems like SDL_Delay somehow affects the return values of SDL_GetTicks, so time_delta gets values like 0 to 3, while it normally is about 15 when I only remove the last 2 lines.

For me, this makes no sense. Does anyone know what's wrong?

EDIT:

The code above is basically the main loop of my program. I first implemented an fps counter by saving the time before rendering the scene in start_time and afterwords calculating the average fps for multiple loops which worked fine.

Then, I added the last two lines to limit the fps to 60. So, if the rendering was faster than min_frame_time, the programm should wait the rest of the time. But after adding this, the results of SDL_GetTicks() of all loops except for the first one became strange, so the delta became these small values as I mentioned above.

lennon310
  • 12,503
  • 11
  • 43
  • 61
Florian M
  • 2,815
  • 3
  • 17
  • 14
  • 1
    I don't understand your question. Could you edit it and elaborate some on what you see with and without the delay. Also, is it between loops? – MicroVirus Aug 20 '14 at 17:24

2 Answers2

0

Going to take a slight guess here, based on what you have shown.

When running windowed, or fullscreen with VSync enabled, SDL_RenderPresent will wait on the vertical sync. This wait will occur inside your timed code, therefore you see values at around time_delta = 15. With SDL_Delay you'll manually wait until close to or just after the next vsync, but this wait is outside your timing. So, then the next time you render it doesn't have to wait, or doesn't have to wait long, for the next vsync to occur and you run through the render-loop much faster.

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
  • Thank you for the answer, but that is not the case here. I am drawing everything using OpenGL only and it does not automatically wait for VSync. I can get way more FPS than 60 by disabling some shadow mapping stuff. – Florian M Aug 22 '14 at 07:51
  • @FlorianM Ok, then this is not it. I'll leave the answer here for other people to see (who knows, it might help someone). – MicroVirus Aug 22 '14 at 08:47
0

Some programs like Google Chrome. Will set the SystemclockResolution to 15ms. There are ways around this.

Sleep(1) and SDL_Delay(1) takes 15 ms

But it will Slow down the CPU to tick every 1ms if I understand correctly. I hope this can help you, Ive been looking for info for a while about this!

I think I should not mess with this, and maybe just work around it.

Community
  • 1
  • 1
HgMerk
  • 71
  • 11