1

Hello i am developing a great game in SDL2 and i have added a FPS regulator to make it smooth on every machine , but then i have created a sample program to ouput fps (not regulate , just output to see the power of current machine its running on ) and i have test it on 2 different computers , one is 12 years old , and the second is new , the old has like 512 mb ram and the new one has 16gb and both of them showed me that this sample code is running under 62,5 FPS , i am pretty sure that im not calculating my fps correctly , whats wrong please ?

#include<SDL.h>
#include<iostream>
#include<sstream>
#include<string>

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = nullptr;
    Uint32 startclock = 0;
    Uint32 deltaclock = 0;
    Uint32 currentFPS = 0;
    window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    SDL_Renderer *renderTarget = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    SDL_Texture *texture = NULL;

    bool isRunning = true;
    SDL_Event ev;

    while (isRunning)
    {
        startclock = SDL_GetTicks();
        while (SDL_PollEvent(&ev) != 0)
        {
            if (ev.type == SDL_QUIT)
                isRunning = false;
        }
        SDL_RenderClear(renderTarget);
        SDL_RenderCopy(renderTarget, texture, NULL, NULL);
        SDL_RenderPresent(renderTarget);

        deltaclock = SDL_GetTicks() - startclock;
        startclock = SDL_GetTicks();

        currentFPS = 1000.0f / deltaclock;
        std::string x;
        std::stringstream ss;
        ss << currentFPS;
        ss >> x;
        x = "FPS: " + x;
        SDL_SetWindowTitle(window, x.c_str());
    }
    return 0;
}
Jessica99
  • 49
  • 5
  • Because FPS of a simple program depends on RAM capacity? – LogicStuff Feb 20 '16 at 18:34
  • it was just an example i wanted to proof that these machines are absolutely different , how can it have same fps on machine with 12 y diff .... – Jessica99 Feb 20 '16 at 18:38
  • The second `startclock = SDL_GetTicks();` is redundant, nothing else wrong here. – LogicStuff Feb 20 '16 at 18:38
  • 2
    You'll see the difference without VSync. A computer from 2004 can handle rendering a texture 60 times per second. – LogicStuff Feb 20 '16 at 18:40
  • i have deleted VSync and i get like 0 fps then 1k then 0 and again 1k... – Jessica99 Feb 20 '16 at 18:43
  • Possible duplicate of [Am I calculating my FPS correctly?](http://stackoverflow.com/questions/5614018/am-i-calculating-my-fps-correctly) – LogicStuff Feb 20 '16 at 18:48
  • so does it mean every computer will run this code for 62 fps ? i dont get it.... when i was watching tutorials nearly everyone had like 3k fps on a sample fps counter and my super computer gets 62? – Jessica99 Feb 20 '16 at 18:49

1 Answers1

0

Obviously the VSYNC flag as LogicStuff mentioned in the comments. But there is more:

The accuracy of the timer and your calculations won't cut it. Assume you you get 1 ms of delta (minimal delta you can get) you will get 1000/1 as a result. If you draw faster than 1 ms you will calculate 1000/0. Luckily you didn't crash here but get a 0 result.

You are better of counting how many frames you have rendered during a time period of 1 second.

Also note that just because SDL_Ticks() returns a value in milliseconds that does not mean the accurately reported delta is in 1 ms accuracy. I would expect it to report deltas in approx. 10 ms steps (or maybe even worse). SDL_Ticks is not a high performance counter.

Florian Zwoch
  • 6,764
  • 2
  • 12
  • 21