; PROJECT : ast frag2
; AUTHOR : Microsoft
; CREATED : 16/01/2010
; EDITED : 21/12/2010
; ---------------------------------------------------------------------
ScreenVsync on
gametimer2=timer()
gameloop()
end
Constant gamespeed=20 ;fps
function gameloop()
local x
Repeat
Cls 0
;
If timer()>=gametimer2
; gametimer2=(Timer()+(1000/gamespeed));gamespeed)
gametimer2=(Timer()+20);gamespeed)
circle x,100,5,1
x=x+1
sync
EndIf
; sync
Until false
endFunction
The logic of the loop is broken, rather than redraw completely at 20 MS intervals it's drawing the circle + syncing, but clearing the graphics buffer every iteration.
ScreenVsync on
Constant gamespeed=50 ;fps
gameloop()
end
function gameloop()
local x
gametimer2=timer()
Repeat
;
CurrentTime=Timer()
If CurrentTime>=gametimer2
gametimer2=(CurrentTime+(1000/GameSpeed))
; redraw frame only when at least 20 milliseconds have pasted
RenderGame()
EndIf
; sync
Until spacekey()
endFunction
Psub RenderGame()
Cls
circle x,100,5,1
x=mod(x+1,800)
sync
EndPsub
This approach is basically what SetFPS does anyway, with one exception, setting fps means that when syncing on computers that can run the loop faster than required (Less than 20, when that's the target), than it gives the free time given back to windows. This is not he case in this loop however, as this loop is a hogging all the cpu since it's constantly waiting for the next frame to occur.
This can be dropped in a bit like this,
ScreenVsync on
Constant gamespeed=50 ;fps
gameloop()
end
function gameloop()
local x
gametimer2=timer()
Repeat
;
CurrentTime=Timer()
TimeToNextFrame=gametimer2-CurrentTime
If TimeToNextFrame<1
gametimer2=(CurrentTime+(1000/GameSpeed))
; redraw frame only when at least 20 milliseconds have pasted
RenderGame()
else
; CHeck if there's a more then 5 milliseonds to go
if TimeToNextFrame>=5
wait 4
endif
EndIf
; sync
Until spacekey()
endFunction
Psub RenderGame()
Cls
circle x,100,5,1
x=mod(x+1,800)
sync
EndPsub