-1
\$\begingroup\$

I'm having a long headache (which may not be that complicated) with this frame rate thing. I already tested several frame rate algorithms, lastly I tested this here and they all gave something strange at some point, like choppy movements, skipping and other bizarre things.

Now I've used this "Variable Step" from the answer of this link and everything is running fine, but with all the stress past because of this I would like to know if the frame rate is actually right now:

(Just mentioning that this frame rate algorithm is from the user "Stephane Hockenhull" that is on the last link.)

Uint32 iMinimum_Fps_Delta_Time = (1000/50); // minimum 50 fps, if the computer is slower than this: slow down.
Uint32 iLast_Game_Step = SDL_GetTicks(); // initial value

while(bRunning) {
    Uint32 iNow = SDL_GetTicks();

    // Check so we don't render for no reason, avoid having a 0 delta time
    if(iLast_Game_Step < iNow) {

        Uint32 iDelta = iNow - iLast_Game_Step;

        if(iDelta > iMinimum_Fps_Delta_Time) iDelta = iMinimum_Fps_Delta_Time; // slow down if the computer is too slow

        //////////////////////////////////////////////////////////////////////////////////////////////////////////
        pState_Manager->handle_events(iDelta);
        pState_Manager->update(iDelta);

        iLast_Game_Step = iNow;

        SDL_SetRenderDrawColor(cEngine::oWindow.get_renderer(), 0, 0xAC, 0xE6, 0xFF);
        SDL_RenderClear(cEngine::oWindow.get_renderer());
        pState_Manager->draw();
        SDL_RenderPresent(cEngine::oWindow.get_renderer());

        cKey_Handler::clear_keys();
        cJoystick_Handler::check_joystick_connection();

        while(SDL_PollEvent(&oEvent)) {
            if(oEvent.type == SDL_QUIT) {
                bRunning = false;
                break;
            }
            else if(oEvent.key.keysym.scancode == SDL_SCANCODE_RETURN){
                bRunning = false;
                break;
            }

            if(oEvent.type == SDL_KEYDOWN) {
                if(oEvent.key.repeat == 0) cKey_Handler::set_pressed_keys(oEvent);
            }
            else if(oEvent.type == SDL_KEYUP) cKey_Handler::set_released_keys(oEvent);

            if(cJoystick_Handler::aDual_Shock_4) {
                if(oEvent.type == SDL_JOYHATMOTION) cJoystick_Handler::set_hat(oEvent);
                else if(oEvent.type == SDL_JOYAXISMOTION) cJoystick_Handler::set_axis(oEvent);
            }
        }

        pState_Manager->handle_inputs();
        //////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
    else SDL_Delay(1);
}

Some useful (maybe) information:

Renderer is set to:

SDL_CreateRenderer(this->aWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

Window's size is 960x720 and renderer logical size is:

SDL_RenderSetLogicalSize(this->aRenderer, 960, 720);
\$\endgroup\$
  • 3
    \$\begingroup\$ It's not clear what you're asking. Are you trying to construct a loop for rendering? Or are you trying to measure the actual frame rate? \$\endgroup\$ – Jay Jan 22 '18 at 3:17
  • \$\begingroup\$ @Jay I just want to know if there's something wrong with this frame rate calculation, if it has a weak point somewhere and what precautions should I have using it \$\endgroup\$ – Dormin Jan 22 '18 at 15:42
0
\$\begingroup\$

I would suggest you move

iLast_Game_Step = iNow;

Up just under your IDelta minimum test as you are executing logic in between that may affect your stepping. if you have anything that causes slow down between that and the setting of Last game step, then its going to cause your frame rate to jump up and down. As that logic is not being counted against the complete frametime.

\$\endgroup\$
  • \$\begingroup\$ I discovered that this algorithm varies a lot. There is time that I open the game and everything is going well, but most of the time is giving some cuts and a movement out of rhythm in the image. I'm going back to the lazyFoo algorithm and adapt to it, I've already lost a lot of time in this problem ... \$\endgroup\$ – Dormin Jan 22 '18 at 20:49
  • \$\begingroup\$ btw, I did your suggestion and it keeps randomizing the frame rate... \$\endgroup\$ – Dormin Jan 22 '18 at 20:49
  • \$\begingroup\$ Can understand, these sort of problems end up consuming you. \$\endgroup\$ – ErnieDingo Jan 22 '18 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.