0

I am trying to figure out why this FPS limit doesn't works properly on some computers.. for example when I limit the fps to 333 it will show 250 and on some computers the FPS limitation will work just fine (when limit 333 it will limit 333 and not 250 or any other number).

    g_nFrameCount++;
    DWORD currentTime = timeGetTime();

    {
        float fFrameLimit = 0;
        if (g_nFrameLimitValue > 0)
            fFrameLimit = 1000 / g_nFrameLimitValue;
        if ((currentTime - g_dwLastTime) < fFrameLimit)
        {
            Sleep((int)fFrameLimit - (currentTime - g_dwLastTime));
            currentTime = timeGetTime();
        }
        g_dwLastTime = currentTime;
    }

    if (g_dwLastFPSTime + FPS_INTERVAL<currentTime)
    {
        g_fFPS = (g_nFrameCount - g_nLastFrameCount)*FPS_INTERVAL / ((float)(currentTime - g_dwLastFPSTime)*(FPS_INTERVAL / 1000));
        g_dwLastFPSTime = currentTime;
        g_nLastFrameCount = g_nFrameCount;
    }
IDev
  • 1
  • 4
  • You limit something to 333 and you get 250.. that's within your bounds, what's the problem? – Gizmo Nov 06 '17 at 18:38
  • in this game my computer can run over 2000 FPS, its an old game. so if I limit the FPS to 333 i will get something new the 250 instead of 333. any number I will type I will get -50 FPS then the actual I wrote.. – IDev Nov 06 '17 at 18:41
  • If your code runs at 250 times per second, your assumption is clearly wrong. As for older games, I have old games that run like crap (25fps) on new hardware and much better on old hardware (60fps). Old meaning 20 years old. – Gizmo Nov 06 '17 at 18:44
  • 1
    Supposedly `Sleep` sleeps an extra ms. As `timeGetTime` have ms precision at best, so this way you cannot control exactly the fps. What if you want to set 300fps? It needs 3.33 ms sleep, which cannot be done with `Sleep` – geza Nov 06 '17 at 18:45
  • Same results for 300 as the 333.. If I limit to 250 I will get 200. I actually will get -50 FPS then the actual I wrote. – IDev Nov 06 '17 at 18:48
  • But in the code you are sleeping (to get the FPS you want) AND you are waiting until `if (g_dwLastFPSTime + FPS_INTERVAL – Gizmo Nov 06 '17 at 18:51
  • 1
    @IDev: it is not possible to do what you want with `Sleep`, as its precision is low. If other parts of your program doesn't take time, then Sleep can fps limit to 500, 333, 250, 200, etc. discrete values. Presumed that `Sleep` does an exact sleep. Which is not guaranteed by the OS at all. – geza Nov 06 '17 at 18:53
  • 1
    Why don't you just limit fps using vsync? – user7860670 Nov 06 '17 at 18:54
  • @VTT I would think that vsync doesn't allow for arbitrary values. We don't know what problem the author is trying to solve. – Gizmo Nov 06 '17 at 19:01
  • Try `std::this_thread::sleep_for(std::chrono::microseconds(3330));` or whatever resolution you want to use.. – Brandon Nov 06 '17 at 19:20
  • @Brandon It's no use, `std::this_thread::sleep_for` will call `Sleep`. – user7860670 Nov 06 '17 at 19:35
  • Basically you need a sub-millisecond precision sleep/wait. Maybe you can ask this question instead of this current one. At first, I'd try: sleep 1 ms less than needed, then do the remaining waiting with `QueryPerformanceCounter`. – geza Nov 06 '17 at 19:44

0 Answers0