Timer
CurrentMillisecond = Timer()
 
Parameters: NONE
Returns:

    CurrentMillisecond = current timer value
 

      Timer will return the internal system time in Milliseconds. There are 1000 milliseconds per second. This is the same on every computer, regardless of how fast it is.

      Hence in computer world, 1000 milliseconds makes up one second of our time, 500 milliseconds would be 1/2 a second and 250 milliseconds is a 1/4 of a second.


      Most games try to update the display at 50fps to 60fps (frames per second) (See FPS). For a 50 frames per second refresh rate, our game must update in (1000/50)=20 milliseconds. Where at a 60FPS refresh, the game would need to update in (1000/60)=16.67 milliseconds. Given there's a lot of different systems and configurations of systems out there, this can be difficult part of programming your own games, Especially when trying to keep everything moving smooth and consistently on different systems.

      One strategy you can use is often called "Timer Based Movement" (Search forums for some tutorials) which is just another way of controlling movements. Which just means that rather than thinking of motions in pixels per frame, we can convert our motions to pixels per millisecond.

      So if we had a game running at 50 frames per second and a character is moving 2 pixels per frame. Then ideally every second it's going to move (50*2)=100 pixels. Now we're assuming the computer can keep the refresh rate constant though, if it slows down (too much stuff on screen) to say 25 updates per second, then the object is only going to have moved (25*2)=50 pixels in that second. These kinds of performances bumps can really affect how the game plays for your users.

      There's a few ways to counter this, but one way is to measure the time past since the object started moving in it's current direction. We then multiply that time by the number pixels it should move in a millisecond. This will let us compute where the object should be at any given time. Check out the second example bellow and don't forget to search the forums for some tutorials on timer based movement also.




FACTS:


      * Note: Timer() returns the current millisecond. This value is not from the start of your application though, it's the time since the computer the program is running upon was last reset. Moreover since timer() is a 32bit value and it's ticking over at 1000 units per second, the value can wrap (overflow back to zero) while your program is running.






Mini Tutorial #1:


      This example uses the timer() to make the program update at interval of every 200 milliseconds (1/5 of a second).


  
  
; Get the current internal system time
  time = Timer()
  
  Repeat
     Cls RGB(0,0,0)
     
   ; display a progress bar
     BoxC 01, Progress, 201,RGB(100,200,40)
     
   ; display the progress bar pecentage
     Print Str$(Progress)+"%"
     
   ; If 200 milliseoncds have passed increase the Progress variable
     If Timer() - time >= 200
        Progress = Progress + 5
      ; set the variable time to the current time
        time = Timer()
     EndIf
     
     Sync
  Until Progress>100
  
  Print ""
  Print "Job Complete"
  
  Sync
  WaitKey
  
  






Mini Tutorial #2: Frame & Timer Base Movement Example


      This example shows frame based and timer base movements side by side. The idea of this short program, is that it moves two circles at a rate of 120 pixels per second. The top BLUE circle is moving 2 pixels every time through the main loop, where the bottom RED circle is moving at an equivalent rate of 2 pixels per millisecond. So the bottom objects position is actually calculated by multiplying the time past (the time since the demo started) and pixels per millisecond. This means no matter how fast or slow the loop is executed, the objects motion will be consistent. However, the top object will appear to speed up and slow down if the frame rate is altered.

      To help demonstrate the difference, you can control the speed of the main loop by pressing the function keys 1 to 6 in the demo code bellow. Changing the speed to say 10FPS, will show the top object slowing down but the bottom object will keep moving at the same rate, the only difference is going to be the smoothness of the bottom objects motion.


  
  
  // Set FPS to 60 frames per second or less
  SetFPS 60
  
  
  // The number of pixels an object moves
  // in a frame
  PixelsPerFrame#=2
  
  
  // compute the movement speed as our ideal rate of
  // 60 frames per second
  PixelsPerTick# = PixelsPerFrame#*60/1000.0
  
  
  // StartTime
  StartTime=Timer()
  
  // ----------------------------------------------------
  // --[MAIN LOOP]---------------------------------------
  // ----------------------------------------------------
  
  Do
     
     // Clear the screen to black
     Cls RGB(0,0,0)
     
     // --------------------------------------------------
     // This objects movement is using frame based movement
     // --------------------------------------------------
     
     // Step this object at the current frame rate
     Object1_X#+= PixelsPerFrame#
     
     // wrap the coordinate to within the screen size
     Object1_X# = Mod(Object1_X#,800)
     
     // draw this object as a BLUE circle
     CircleC  Object1_X#,100,50,true,$ff
     
     // --------------------------------------------------
     // This objects movement is using timer based movement
     // --------------------------------------------------
     
     TimePast=Timer()-StartTime
     
     // calc objects new X position over given the
     // time past
     Object2_X#=  TimePast *PixelsPerTick#
     
     // wrap the coordinate to within the screen size
     Object2_X# = Mod(Object2_X#,800)
     
     // draw this object as a RED circle
     CircleC  Object2_X#,300,50,true,$ff0000
     
     
     
     // Display the frame rate
     Print "Current FPS:"+Str$(FPS())
     
     Print "Use Function Keys (F1 to F6) to Set Frame Rate:"
     
     // ---------------------------------------------
     // check if a function key is pressed
     // ---------------------------------------------
     For lp =1 To 6
      ; check if this F key is down
        If FunctionKeys(lp)=true
         ; if it's down, set the rate limiter
           SetFPS  lp*10
        EndIf
     Next
     
     
     // show the newly drawn frame to the user
     Sync
     
     // loop back to the do statement if the ESCkey is
     // not pressed
  Loop EscKey()=true
  
  
  
  
  


 
Related Info: CurrentDate$ | CurrentTime$ | Fps | GetFps | SetFps | Wait :
 


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