Example | SuperStrict
Import dmaz.TRL
Graphics 640,480
Local vwait:Int
Local rate:Int = 20
Local stage:TRL = New TRL
stage.SetUpdateRate(rate) ' game login updates per second
stage.ResetCounters ' now, choose a frame at the start of your project and keep that one.
' 30 or 60 are the best.... for a game with a whole lot of ai I'd go 30.
' my games run at 60 though and do fine even on the slowest of machines.
SetBlend ALPHABLEND
' don't put too much in this main loop, all objects should be updated in the GameUpdate callback.
While Not KeyHit(KEY_ESCAPE) Or AppTerminate()
If KeyHit(KEY_UP) Then rate :+ 1 ; stage.SetUpdateRate(rate)
If KeyHit(KEY_DOWN) Then rate :- 1 ; stage.SetUpdateRate(rate)
If KeyHit(KEY_V) Then vwait = 1-vwait
' 2 functions will be called back to, one that capture the old position
' and one that performs the game update logic.
Local tween:Double = stage.Update( TObject.CaptureAll, GameUpdate )
' draw all object with a tween value
TObject.DrawAll tween
SetAlpha 1
DrawText "balls: "+TObject.total,10,10
DrawText " fps: "+stage.GetFPS(),10,20
DrawText " ups: "+stage.GetUPS()+" [up/down]",10,30
DrawText "vwait: "+vwait+" [v]",10,40
Flip vwait
Cls
Wend
Function GameUpdate()
If TObject.total < 100 Then New TObject
TObject.UpdateAll
End Function
' your object(s)
Type TObject
Const GRAVITY:Float = 0.2
Const FRICTION:Float = 0.99
Global list:TList = New TList
Global total:Int
Field link:TLink
Field x:Float, ox:Float
Field y:Float, oy:Float
Field xvel:Float
Field yvel:Float
Field alpha:Float = 1 ' I could tween things like alpha, size, rotataion and so on if needed.
' this is a callback, that gets passed to obj:TRL.Update
Function CaptureAll()
For Local o:TObject = EachIn list
o.Capture
Next
End Function
' this of course is called by your GameUpdate function that is passed to obj:TRL.Update
Function UpdateAll()
For Local o:TObject = EachIn list
o.Update
Next
End Function
Function DrawAll( tween:Double )
For Local o:TObject = EachIn list
o.Draw tween
Next
End Function
' ---------------------------------------------------------------------
Method New()
link = list.AddLast(Self)
x = GraphicsWidth()/2
y = 20
xvel = Rnd(-8,8)
Capture
total :+ 1
End Method
Method remove()
total :- 1
link.Remove
End Method
' ---------------------------------------------------------------------
' capture (record current value before update) whatever values you want to
' tween. generally x and y but you could do alpha, size, rotation.... and more
Method Capture()
ox = x
oy = y
End Method
' this update routine gets called a fixed number per second... I like this
' over delta time because you don't have to add weird numbers to all of your
' values.
Method Update()
yvel :+ GRAVITY
xvel :* FRICTION
yvel :* FRICTION
x :+ xvel
y :+ yvel
If x < 0 Or x > GraphicsWidth() Then xvel = -xvel ; x :+ xvel
If y < 0 Or y > GraphicsHeight() Then yvel = -yvel ; y :+ yvel
alpha :- .005
If alpha < 0 Then Remove
End Method
' render all tweened values.
Method Draw( tween:Double )
SetAlpha alpha
DrawOval GetTween(ox,x,tween),GetTween(oy,y,tween),10,10
End Method
' ---------------------------------------------------------------------
Method GetTween:Double( oldValue:Double, value:Double, tween:Double )
Return oldValue + (value-oldValue)*tween
End Method
End Type |