| [HOME] |
Calculating the frame rate |
The calculated frame rate concept that is used in Ricochet is based on the Sync Rate vs. FPS rate thread on The Game Creators Developer Forum.
The concept is simple. A timer is maintained at the start of each display loop, and this is used to average out the current frame rate in milliseconds. This is then used when calculating the actual movement on the screen in pixels per second, rather than pixels per frame. So, on a slow computer the frame rate is lower, the milliseconds value is higher, and the number of pixels moved per frame is correspondingly large. The inverse is true on a fast computer: the frame rate is higher, the milliseconds value is lower, and objects are moved fewer pixels per frame. In consequence, the program appears to run at the same rate on both machines. The only difference is that the movement on the fast machine should normally appear smoother than on the slow machine.
The following snippet of code from Ricochet shows how to calculate the frame rate in practice.
global frame_start_time global milliseconds_per_frame as float global seconds_per_frame as float global frames_per_second as float sync on sync rate 0 initialise_sync_rate() repeat calculate_sync_rate() ` ... sync until restart or return_to_menu function initialise_sync_rate() ` supply an estimated initial frame rate frames_per_second = 40 milliseconds_per_frame = 1000.0 / frames_per_second frame_start_time = timer() endfunction function calculate_sync_rate() milliseconds_per_frame = (milliseconds_per_frame * 0.8) + ((timer() - frame_start_time) * 0.2) frame_start_time = timer() seconds_per_frame = milliseconds_per_frame / 1000 frames_per_second = 1000 / milliseconds_per_frame endfunction function move_balls() ` ... ball().x = ball().x + ball().velocity_x * seconds_per_frame * speed ` ... endfunction