Vsync / Frame Rate Scaling V0.01 This example shows an approach for dealing with situations where you want to VSYNC the display, but we have no way of knowing how fast the users videocard/monitor can Vsync.
So the idea here is that we calc a scaler between the our ideal rate (the rate we imagine our game running at) and the end users actual display rate, which could be anything! Then we apply this scaler to our movement speeds. Which will give us the distance our objects have moved since the _user_ last saw the screen. So we're scaling our ideal rate to theirs, which should smooth out motion somewhat.
Of course this is assuming the user has Vsync enabled in their display drivers, they might not..
Controls: Arrows = Move Camera
Space = Change sync method
ESC = EXIT
// open a Full Screen exclusive screen mode
OpenScreen 1024,768,32,2
// make a black/white pattern image as backdrop
Dim Cols(1)
Cols(0)=$304050
Cols(1)=$80a080
Size =32
width=Size*8
height=Size*8
for ylp=0 to Height/Size
for xlp=0 to Width/Size
x1=xlp*size
x2=x1+size
y1=ylp*size
y2=y1+size
boxc x1,y1,x2,y2,true,cols( cell&1)
inc cell
next
next
GetImage 1,0,0,width,height
// ===========================================================
// Init the Mode the demo will start in.
// ===========================================================
// mode =0 Vsync display mode
// mode =1 SetFps base refresh.
Mode=0
ScreenVsync True
// This is the FPS rate our game should be running at.
Game_Required_VSYNC#=80
// try an calc the vysnc rate of the users computer..
Monitor_Vsync_Rate#= Calc_Monitor_Sync_rate()
// calc the scaler between our ideal rate and the computers Vsync rate.
Game_Movement_Scaler#=Game_Required_VSYNC#/Monitor_Vsync_Rate#
// Make a camera
cam=NewCamera()
// ===========================================================
// Start of Test loop
// ===========================================================
Do
if mode=0
Mode$="Monitor Vysnc"
else
Mode$="SetFps"
endif
// Speed of object in pixels per frame and games ideal rate
MovementSpeed#=10
if Mode=0
// in Vsync mode wea pply the VSYNC scaler to our movement speed
Speed#= MovementSpeed#* Game_Movement_Scaler#
endif
if Mode=1
// in Setfpos mode we just use the movement value as is
Speed#= MovementSpeed#* Game_Movement_Scaler#
endif
// ===========================================================
// Convert this speed
// ===========================================================
if LeftKey()
movecamera Cam,-Speed#,0
endif
if RightKey()
movecamera Cam,Speed#,0
endif
if upKey()
movecamera Cam,0,-Speed#
endif
if downKey()
movecamera Cam,0,Speed#
endif
// ===========================================================
// draw the scene so we've something to look at
// ===========================================================
CaptureToScene
ClsScene
for ylp=0 to 20
for xlp=0 to 100
Xpos=GetImageWidth(1)*Xlp
Ypos=GetImageHeight(1)*ylp
DrawIMage 1,Xpos,Ypos,true
next
next
drawcamera Cam
// ===========================================================
// Draw the info about this scene
// ===========================================================
ink $ff00ff
text 0, 0," Fps Rate:"+str$(Fps())
text 0,20," Monitor Sync Rate:"+str$(Monitor_Vsync_Rate#)
text 0,40,"Games Ideal Sync Rate:"+str$(Game_Required_VSYNC#)
text 0,60," Scaler:"+str$( Game_Movement_Scaler#)
text 0,80," Mode:"+Mode$
// ===========================================================
// Check if the space key was press
// ===========================================================
if spacekey()
// flush the mode
Mode=1-mode
// if mode =0 then we'll set PB to VSYNC the display
if Mode=0
screenvsync true
setfps 0
else
// mode <>0 then we use SetFps to limit the rate
screenvsync false
setfps Game_Required_VSYNC#
endif
// Flush keys
Flushkeys
endif
Sync
loop
;*=------------------------------------------------------------------=*
;*=------------------------------------------------------------------=*
; This function tries to calc the monitors refresh rate for this display
; mode..
Function Calc_Monitor_Sync_Rate()
oldfps=getfps()
Setfps 100
x=GetScreenWidth()/2
y=GetScreenHeight()*0.4
message$="Calculating Montior Refresh Rate:"
cls 0
centertext x,y,Message$
sync
cls 0
centertext x,y,Message$
sync
EndTime=Timer()+3000
repeat
frames=fps()
sync
until Timer()>=EndTime
Setfps oldfps
EndFUnction Frames
Related To Timer Based Movement (Tutorial)
Timer Based Movement with Vsync Tweening