Main Menu

Game / Frame Timing

Started by kevin, November 07, 2012, 04:28:20 AM

Previous topic - Next topic

kevin

  Game / Frame timing

  This example  implements a custom timer into the sync refresh.. Unlike the system timer which counts  regardless of your games state, this timer ignores large intervals between calls.  Allowing the game loop to be paused/suspended and resumed without the potential synchronization issues.


PlayBASIC Code: [Select]
   SetFps 30

Type tSystem
CurrentTime
LastMillisecond
Millisecond
Frames
EndType

Dim System as tSystem


SyncMe()

Do
Cls

print digits$(System.CurrentTime,8)

if spacekey()
print "Paused"
sync
wait 1000
endif


if System.CurrentTime>FireTime
FireTIme=System.CurrentTime+500
print "Fire"
endif
print FireTime

SyncMe()
loop




Psub SyncMe()

Sync

; store the time at last redraw
System.LastMillisecond=System.Millisecond

; current millisecond
System.Millisecond=Timer()

Delta=(System.Millisecond-System.LastMillisecond)

if Delta <50
System.CurrentTime+=Delta
System.Frames++
endif

EndPsub






Usage Example

   This example creates a mock up scene with some moving circles,  the motion of each object is based upon the time past (timer based movement).   You can Pause (Space) and even scale the time passing (Arrows) in the example.   When paused the update routine simply ingores updating the current time, since the current time isn't changing the objects stop moving.  The same applies when you scale time.   If you scale the time pasted by half, then everything in scene moves 50% also.  You can even run time backwards...


PlayBASIC Code: [Select]
   SetFps 61.7

Type tGameObject
StartX
StartY
EndX
EndY
Radius
Colour
StartTime
EndTime
EndType

Dim Object as tGameObject list


Type tSystem
CurrentTime
LastMillisecond
Millisecond
Frames
PauseTime
TimeScaler#
EndType

Dim System as tSystem


System.TimeScaler#=1

; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; -[MAIN LOOP]--------------------------------------------------------------------
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------


SyncMe()

Do
Cls

print digits$(System.CurrentTime,8)

if spacekey()
System.PauseTime=true
else
System.PauseTime=false
endif


DrawObjects()


; trigger some new obejcts at radmon intervals
if System.CurrentTime>FireTime
FireTIme=System.CurrentTime+rndrange(50,500)
for lp =0 to rnd(10)
AddNewObject()
next
endif


; Scaler the time delta
if LeftKey()
System.TimeScaler#-=0.10
flushkeys

endif

if RightKey()
System.TimeScaler#+=0.10
flushkeys
endif


print "Time Scaler:"+Str$(System.TimeScaler#)
SyncMe()
loop



; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; -[SYNC ME]--------------------------------------------------------------------
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------

Psub SyncMe()

Sync


if System.PauseTime=false

; store the time at last redraw
System.LastMillisecond=System.Millisecond

; current millisecond
System.Millisecond=Timer()

Delta=(System.Millisecond-System.LastMillisecond)

if Delta <50
System.CurrentTime+=(Delta*System.TimeScaler#)
System.Frames++
endif
endif


EndPsub

; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------
; -[Draw Objects]---------------------------------------------------------------
; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------


Psub DrawObjects()

CurrentTime=System.CurrentTime



For Each Object()

if CurrentTime>Object.EndTime
Object=Null
continue
else

TravelTime=Object.EndTime-Object.StartTime
TimePast#= CurrentTime-Object.StartTime
scaler#=TimePast#/TravelTime

dx#=Object.EndX-Object.StartX
dy#=Object.EndY-Object.StartY

x#=Object.StartX+(dx#*Scaler#)
y#=Object.StartY+(dy#*Scaler#)

circlec x#,y#,object.radius,true,object.colour


endif

next
Login required to view complete source code



 Related Articles

  * Timer Based Movement in PlayBASIC