Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Framerate Regulator Not Working


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Dante12129   Members   -  Reputation: 1480

Like
0Likes
Like

Posted 27 April 2013 - 02:36 PM

I have followed Lazyfoo's tutorial on regulating FPS to to make a timer for it. I have put the timer in my own library so that I can reuse it in a couple of programs. The problem is that using it doesn't actually cause any change in the framerate.

 

Appropriate part of Timer.hpp

class FPSRegulator
    {
        public:
            //The constructors and destructor
            FPSRegulator();
            FPSRegulator(int framerate);
            ~FPSRegulator();

            //Functions to operate the regulator
            void Start();
            void Delay();

        private:
            //The user-sprecified framerate
            unsigned int m_framerate;

            //Variables to store the time a tiemr has been going or been paused.
            unsigned int m_start_ticks;

            //The timer's status variables.
            bool m_started;

            //Get the ticks
            unsigned int GetTicks();
    };

 

Appropriate part of Timer.cpp

//FPSRegulator
//The constructors and destructor
grnbll::FPSRegulator::FPSRegulator()
{
    m_framerate = 30;
    m_start_ticks = 0;
    m_started = false;
}
grnbll::FPSRegulator::FPSRegulator(int framerate)
{
    m_framerate = framerate;
    m_start_ticks = 0;
    m_started = false;
}
grnbll::FPSRegulator::~FPSRegulator() = default;

//Functions to operate the regulator
void grnbll::FPSRegulator::Start()
{
    //Set the timer's status.
    m_started = true;

    //Start the timer.
    m_start_ticks = SDL_GetTicks();
}
void grnbll::FPSRegulator::Delay()
{
    if(GetTicks() < (1000/m_framerate))
    {
        SDL_Delay((1000/m_framerate)-GetTicks());
    }
}

//Private function to get the ticks
unsigned int grnbll::FPSRegulator::GetTicks()
{
    return SDL_GetTicks() - m_start_ticks;
}

 

And my main.cpp

#include "New_Engine.hpp"

int main ( int argc, char* argv[] )
{
    //Variables necessary to the screen and program
    grnbll::App app;
    grnbll::Screen screen(640, 480, "Game");
    grnbll::Event event;
    grnbll::FPSRegulator fps(30);
    fps.Start();
    bool running = true;

    SDL_Rect rect{0, 0, 50, 50};
    int x_amount = 1, y_amount = 1;

    Uint32 background_color = SDL_MapRGB(SDL_GetVideoSurface()->format, 0xFF, 0xA5, 0x00);
    Uint32 square_color = SDL_MapRGB(SDL_GetVideoSurface()->format, 0xFF, 0xFF, 0xFF);

    while(running)
    {
        //Event handling
        while(event.Poll())
        {
            if(event.GetOther().other == grnbll::OtherEvents::Quit)
            {
                running = false;
            }
        }
        //Logic
        if((rect.x + 50) > screen.GetWidth())
        {
            x_amount = -1;
        }
        if((rect.y + 50) > screen.GetHeight())
        {
            y_amount = -1;
        }
        if(rect.x <= 0)
        {
            x_amount = 1;
        }
        if(rect.y <= 0)
        {
            y_amount = 1;
        }

        rect.x += x_amount; rect.y += y_amount;

        //Rendering
        screen.SetBackgroundColor(background_color);
        SDL_FillRect(SDL_GetVideoSurface(), &rect, square_color);
        screen.Flip_Screen();
        std::cout << SDL_GetTicks() << std::endl;
        fps.Delay();
        //SDL_Delay(1000/30);
    }

    return 0;
}

 

If I use the SDL_Delay instead of the Delay method of my timer, it actually does slow it down and work. It's only with the regulator that it doesn't actually change the framerate.



Sponsor:

#2 Satharis   Members   -  Reputation: 1737

Like
0Likes
Like

Posted 27 April 2013 - 06:54 PM

Unless I'm mistaken your Delay() if statement won't actually trigger.

 

You set m_start_ticks to a value when you start the counter, which means if it was at say: 100 m/s since the library started, then you're at 1500 in SDL_GetTicks, you'll end up with a value like 1400 < 33.3(rough estimate of 1000/m_framerate), which of course would only be true under very specific circumstances.



#3 Dante12129   Members   -  Reputation: 1480

Like
0Likes
Like

Posted 27 April 2013 - 07:35 PM

Well I based it off of Lazyfoo's tutorials on calculating framerate. I made another program that uses timer class from his lesson 14 and the way he delays it, and it works. But when I do it using the Delay() function, it doesn't work even though they're pretty much the same, just adjusted for using member data and methods.



#4 Bacterius   Crossbones+   -  Reputation: 10961

Like
0Likes
Like

Posted 27 April 2013 - 07:38 PM

You should probably reset the timer start ticks (m_start_ticks) after each delay, otherwise it will only delay once. Unless I misunderstood.

 

 

 

if(GetTicks() < (1000/m_framerate))
 

 

Will only be true when GetTicks() is between 0 and your frametime, and unless I misunderstood the GetTicks() will get arbitrarily large if you don't keep m_start_ticks equal to the start time of the last frame.


“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


#5 Dante12129   Members   -  Reputation: 1480

Like
1Likes
Like

Posted 27 April 2013 - 07:50 PM

Resetting the m_start_ticks solved the problem. Thanks.


Edited by Dante12129, 27 April 2013 - 07:50 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS