Can anyone here explain lazyfoo's SDL tutorial #15

Started by
1 comment, last by Lazy Foo 15 years, 8 months ago
1) I don't get it. Why do you need to divide 1000. Why 1000? Is it because 1 sec = 1000 ms? 2) Any one can here explain me the functions used? I really need some help. http://lazyfoo.net/SDL_tutorials/lesson15/index.php Timer::Timer() { //Initialize the variables startTicks = 0; pausedTicks = 0; paused = false; started = false; } void Timer::start() { //Start the timer started = true; //Unpause the timer paused = false; //Get the current clock time //SDL_GetTicks = Gets the number of milliseconds since SDL library initialization. startTicks = SDL_GetTicks(); } void Timer::stop() { //Stop the timer started = false; //Unpause the timer paused = false; } void Timer::pause() { //If the timer is running and isn't already paused if( ( started == true ) && ( paused == false ) ) { //Pause the timer paused = true; //Calculate the paused ticks pausedTicks = SDL_GetTicks() - startTicks; } } void Timer::unpause() { //If the timer is paused if( paused == true ) { //Unpause the timer paused = false; //Reset the starting ticks startTicks = SDL_GetTicks() - pausedTicks; //Reset the paused ticks pausedTicks = 0; } } int Timer::get_ticks() { //If the timer is running if( started == true ) { //If the timer is paused if( paused == true ) { //Return the number of ticks when the timer was paused return pausedTicks; } else { //Return the current time minus the start time return SDL_GetTicks() - startTicks; } } //If the timer isn't running return 0; } bool Timer::is_started() { return started; } bool Timer::is_paused() { return paused; } in main //If we want to cap the frame rate if( ( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) ) { //Sleep the remaining frame time SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); }
Advertisement
1000 is indeed representing milliseconds, 1000 / fps is the number of milliseconds that need to pass before drawing the next frame.

SDL_GetTicks(); - this is explained by the comment: it gets the number of ticks in milliseconds since the initialization of SDL, which is why it is used for limiting the fps.
SDL_Delay(); - this pauses the program for the specified number of milliseconds.

Other than that there's not really much to explain; you should know what classes do and how the members of the Timer class are working just by understanding the two aforementioned functions.
1) Answered in tutorial 12.
2) Answered in tutorial 13.

Almost every tutorial assumes you read the one before it. If you skip tutorials you're going to be lost.

Learn to make games with my SDL 2 Tutorials

This topic is closed to new replies.

Advertisement