you'll need your own beat.wav
this uses delta time rather than render tweening
;S. Richardson
;ICE9
;Give Me The Beat
Graphics3D 800,600,0,2
Global wavKonga = LoadSound("beat.wav")
;desired frames per second
Const TARGET_FPS# = 30.0
;for timing
Global previous% = MilliSecs()
Global lastFPS% = MilliSecs()
Global fpscount
;for beats per minute
Const MINUTE =60*TARGET_FPS#
BPM=120
camera = CreateCamera()
PositionEntity camera,0,0,-15
light = CreateLight()
PositionEntity light,20,20,40
Type cube
Field entity
End Type
piv=CreatePivot()
PositionEntity piv,0,0,0
For cnt = 1 To 100
TheCube.cube= New cube
TheCube\entity = CreateCube(piv)
PositionEntity TheCube\entity,Float(Rnd(-5,+5)),Float(Rnd(-5,+5)),Float(Rnd(-5,+5)),False
EntityColor TheCube\entity,Rnd(0,255),Rnd(0,255),Rnd(0,255)
Next
While KeyDown(1)=0
;calculate delta time
CalcTimer()
now% = MilliSecs()
delta# = (now - previous) / (1000 / TARGET_FPS)
If delta <= 0 Then delta = 0.001
previous = now
;add interval to beat variable
GiveMeTheBeat=GiveMeTheBeat+delta#
;turn the balls
TurnEntity piv,1*delta#,2*delta#,3*delta#
For TheCube= Each Cube
TurnEntity TheCube\entity,4*delta#,2*delta#,3*delta#
Next
;if interval reached then give me the beat
If GiveMeTheBeat > MINUTE/BPM
PlayBeat()
GiveMeTheBeat=0
For TheCube= Each Cube
ScaleEntity TheCube\entity,1.5,1.5,1.5
Next
scaled=1
EndIf
;update the world delta for animation timing
UpdateWorld delta
Flip True
VWait
RenderWorld
If scaled=1
For TheCube= Each Cube
ScaleEntity TheCube\entity,1,1,1
Next
scaled=0
EndIf
Wend
;Play the beat
Function PlayBeat()
chnBeat = PlaySound(wavKonga)
End Function
;Calculate interval
Function CalcTimer#()
ms_passed#=MilliSecs()-render_time
render_time=MilliSecs()
multiplier#=(multiplier#*4.0 + Float(ms_passed#/ TimePerFrame))/5.0
If multiplier# >2 Then multiplier#=2
;Get fps
frames=frames+1
If MilliSecs()-r_time=>1000 Then fpscount=frames : frames=0 : r_time=MilliSecs()
End Function
|