Calculating Framerate in SDL

Started by
-1 comments, last by Yart 12 years, 1 month ago
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!

This topic is closed to new replies.

Advertisement