dmaz.TRL: Types Modinfo Source  

TRL - Fixed rate update logic with Tweening

Types Summary

TRL The TRL type.

Types

Type TRL
DescriptionThe TRL type.
Methods Summary
GetFPS Get the actual frames being draw per second.
GetTween Get the number inbetween the old and the new value.
GetUPS Get the number of logic updates being called per second.
ResetCounters Reset time counters.
SetUpdateRate Set the rate at which your "update" function will be run.
Update This goes in the main loop and should be called every graphic frame.
Method GetFPS:Int()
ReturnsFrames per second.
DescriptionGet the actual frames being draw per second.
Method GetTween:Double( oldValue:Double, value:Double, tween:Double )
ReturnsThe interpolated number.
DescriptionGet the number inbetween the old and the new value.
Method GetUPS:Int()
ReturnsLogic updates per second.
DescriptionGet the number of logic updates being called per second.
Method ResetCounters()
DescriptionReset time counters.
Method SetUpdateRate( gameFPS:Double )
DescriptionSet the rate at which your "update" function will be run.
InformationChoose a logic rate at the start of your project, 30 or 60 work the best. For a game with lots of AI I'd go 30.
Method Update:Double( captureCallback(), updateCallback() )
DescriptionThis goes in the main loop and should be called every graphic frame.
Information2 functions will be called back to, one that capture the old position and one that performs the game update logic. returns: tween number that should be used for drawing.
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

Module Information

Version1.0
AuthorDavid Maziarka
Copyright(c) 2007 David Maziarka
LicenseFree for commercial and private use
History1.00 Release