Public Group

Calculating Framerate in SDL

This topic is 2464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

So I'm going through LazyFoo.net's tutorials and I wanted to make my program keep a constant framerate of 60FPS while displaying the framerate on the top caption of the window.

I can achieve it! ...but only one at a time.

The code acts differently depending on where I place two of my timers. (They are classes)

This code makes the framerate unregulated but provides a framerate counter that's accurate.//Start the frame and update timers. update.start(); fps.start(); while(quit == false){ while( SDL_PollEvent(&event)){ if( event.type == SDL_QUIT ){ //Quit the program quit = true; }

And this code makes the framerate regulated but the counter stays at 0.
while(quit == false){ //Start the frame and update timers. update.start(); fps.start(); while( SDL_PollEvent(&event)){ if( event.type == SDL_QUIT ){ //Quit the program quit = true; }

If I separate both of the timer classes the program crashes.

Now I can see where my problem may lie. In my timer class, if I "start" it, it resets the ticker thingamabob. Later on in the code, after 1000 ticks on the update (for the caption) class's timer, it returns the amount of time and then resets it. But if I put it at the start of the loop, it keeps resetting before it can even count thus always being 0.

The update timer also relies on the fps class' timer to make it's calculations. Oops! So if the fps class (which also ACTUALLY regulates the framerate) is outside of the loop, there is no control, and if it is inside, there is, but the update class has to be with it or else it crashes.

I don't know what I should do to solve this. Any suggestions?

I included my whole source (with executable) as an attachment.

Keys: Left and Right arrows to subtract and add balls.

Thanks!

1. 1
2. 2
3. 3
Rutin
24
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• Forum Statistics

• Total Topics
633651
• Total Posts
3013129
×