SetFPS
SetFPS FramesPerSecond
 
Parameters:

    FramesPerSecond = The Maximum Frames Per Second Value
Returns: NONE
 

      SetFPS sets the the maximum number of redraws (sync's) that can be drawn per second. So SetFPS is used to keep your program running at, or bellow, a fixed speed.

      Setting the FPS (Frames Per Second) rate makes PlayBASIC try and keep the program running at a consistent rate. When we set an FPS rate, we're telling PlayBASIC how many milliseconds we think each complete refresh should take. Milliseconds I hear you ask ? - Yeah, the computer has an internal counter that's accurate to 1000th of a second (see Timer). So there's 1000 milliseconds per second. When we set the fps rate, the SetFps function takes the FramesPerSecond value divides it into 1000 ( 1000 / YourRequiredFps). This gives PlayBASIC the number of milliseconds each frame should take.

      Example. Now if we wanted a peek frame rate of 50 fps, we'd use SetFps 50. PlayBASIC would then work out the number of milliseconds between each frames going to take like so 1000/50= 20 milliseconds. It then uses this number of milliseconds when rendering the screen during a sync. Upon conclusion of the sync'ing process, it checks how much time there is between the end of this frame and when the next frame is expected to start drawing, in order to stick to our ideal frame rate. If there's more than a millisecond, PlayBASIC releases control back to the operating system by sleeping (See Wait). This prevent our programs from completely hogging the system CPU on machines that are fast enough to run the game at our preferred frame rate. However, If the system can't render each frame fast enough then the game doesn't bother giving any time back to the system.

      This means that the setting the FPS only prevents our programs from running way too fast,. it doesn't have any impact upon the situation where the our game might run slower than our preferred frame rate on some machines.




FACTS:


      * Setting the FPS rate to 0, will make PlayBASIC sync as fast as the machine possibly can. Be aware though, that this can make your game hog the computers cpus and graphics processor, which can affect the other programs running on the system. We actually recommend setting the FPS rate to some high today, rather than letting it run completely unrestricted. If you want it to run at it's best, set the Fps rate of say 100 to 200 fps. This will let the program run very well, but not completely choking up the users computer.

      * It's generally not recommended to use the SetFPS in conjunction with ScreenVSync, since it tends to make the frame rate choppy. Which occurs because the then two things sre trying to limit the programs speed.

      * SetFPS is a frame limiting mechanism. Therefore, it does not tween the frame rate for when your application is running on a slower machines. So if the machine your program is running upon isn't fast enough to meet your required frame rate, the program will slow down. There's a number of strategies to coupe with this, one common one is something called Timer Based Movement. There's a number of timer based movement examples on the forums as well as the bare bones example on the timer page.




Mini Tutorial #1:


      This example sets the Max frames per second to 20, then displays PB's limit as well as the current FPS rate.

  
; Tell PlayBASIC to limit the program speed to 20(or less)
; frame updates (syncs) per second
  SetFPS 20
  
; start a Do/Loop
  Do
   ; Clear the screen
     Cls RGB(0,0,0)
     
   ; Display the Current FPS() rate
     Print "Current FPS:"+Str$(FPS())
     
   ; Display the FPS rate you set PB to Limit your syncs to
     Print "SetFPS Value:"+Str$(GetFPS())
     
   ; Display the screen
     Sync
  Loop
  



This example would output.

  
  Current FPS:20
  SetFPS Value:20
  






Mini Tutorial #2:


      This example moves a circle across the screen left to right, if we took the SetFps statement out of the program then PlayBASIC would try and execute the main DO/LOOP as fast as the machine possibly can. Which might be 1000's of times per second on modern fast machines, so rather than see a slow moving circle we'd see a circle shaped blur.

  
; Tell PlayBASIC to limit the program speed to 20(or less)
; frame updates (syncs) per second
  SetFPS 60
  
  
; start a Do/Loop
  Do
   ; Clear the screen
     Cls RGB(0,0,0)
     
   ; add one to the Xpos and wrap it with in 0 to 799
     Xpos=Mod(Xpos+1,800)
     
     
   ; draw a circle at this position on the screen
     Circle Xpos,300,50
     
   ; Display the screen
     Sync
  Loop
  
  



 
Related Info: FPS | GetFPS | ScreenVsync | Timer :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com