#Constant True = 0x01
#Constant False = 0x00
Global VSync As Integer = 60
#Constant TimeSync = 1000/VSync `set this to your desidered FPS
#Constant mediaCube = 1
#Constant mediaCubePosition = 1
#Constant sizeCube = 10.0
#Constant baseTime = 1
#Constant FrameSync = Screen FPS()/VSync
Sync On
Sync Rate 0
Disable EscapeKey
Type Tick
Previous As Float
Current As Float
Duration As Float
Accumulate As Float
isTick As Boolean
EndType
Global Dim Time(0) As Tick
Array Insert At Bottom Time(0)
Time(baseTime).Current = Timer()
Global Debug As Boolean = True
Global ApplicationTitle As String = "Time-Based Movment in DarkBASIC Professional"
Make Object Cube mediaCube, sizeCube
If Not Make Vector3( mediaCubePosition ) Then Exit Prompt "Error #0001", "Unable to Create Vector on Line 26"
Repeat
UpdateTime( baseTime )
If Time(baseTime).Accumulate >= TimeSync
` timer based events go in here
Time(baseTime).isTick = True
Set Vector3 mediaCubePosition, WrapValue( Object Angle X(mediaCube) + 1.0 ), WrapValue( Object Angle Y(mediaCube) + 1.0 ), WrapValue( Object Angle Z(mediaCube) + 1.0 )
Sync
EndIf
`use this area to interpol movements, and objects that need to run as quickly as possible
`normally i'd do that in the function below, but honestly can't be arsed right now
Rotate Object mediaCube, X Vector3(mediaCubePosition), Y Vector3(mediaCubePosition), Z Vector3(mediaCubePosition)
If Debug = True
Set Window Title ApplicationTitle + " - "+STR$(Screen Width())+"x"+STR$(Screen Height())+" @ "+STR$(Screen FPS())
Else
Set Window Title ApplicationTitle
EndIf
RemStart ` This is quirky, because if you swap the above code with below just notice the speed almost double!
If Debug = True
Text 0, 0, ApplicationTitle + " - "+STR$(Screen Width())+"x"+STR$(Screen Height())+" @ "+STR$(Screen FPS())
Else
Text 0, 0, ApplicationTitle
EndIf
RemEnd ; ` my guess is it has something to do with the Set Windows Title being slow, but meh!
FastSync
Until EscapeKey()
Function UpdateTime( Base As Integer )
Local this As Integer
Local Index As Integer
If Base = 0
For Index = 1 To Array Count( Time(0) )
Inc this
GoTo Update
Next
Else
this = Base
GoTo Update
EndIf
Update:
Time( this ).Previous = Time( this ).Current
Time( this ).Current = Timer()
Time( this ).Duration = Time( this ).Current - Time( this ).Previous
If Time( this ).isTick
Time( this ).Accumulate = Time( this ).Duration
Time( this ).isTick = False
Else
Time( this ).Accumulate = Time( this ).Accumulate + Time( this ).Duration `would be nice if the Inc covered floats
EndIf
EndFunction
Alright so the source is overly complex for what it's showing, but it works very nicely.
I didn't add motion interpoling, figured people would enjoy trying to think of thier own solutions to that.. especially given as mine just complicates matters further, no doubt needlessly.
I'll post it up if people want it though.
Keeping the movement to a VSync of 60 though, should allow you to by-pass the whole worry about smooth rendering, it's only when your trying to render slower than that it starts having a hissy fit.
Setting up your program in this mannor though allows you to do more before it starts affecting performance. If you use a physics engine like newton especially... you see a HUGE performance boost from limiting the number of calls, and it interpols the movement values for you. So it's smooth as anything.
Generally speaking try to keep most of your more processor intensive tasks and base movement within the Timer Loop, then just update the 3D and non-speed related (ie Controls) to outside of the loop.
The best part is when your program slows down, what is in the timer loop doesn't. In-fact you can even forgot syncing outside of the Timer loop and you'll find that the application will run slightly smoother.
It's a very good technique is alot of your application needs to be broken into smaller parts.
Your signature has been erased by a mod. Please resize it to under 600x120. Thanks!