You can stabilize the speed by altering the sleep time between frames using a function (regulate)

Code: Select all

```
#define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
#define inbox (mx>125) and (mx<475)' and (my>300) and (my<350)
#define incircle(cx,cy,radius,x,y) (cx-x)*(cx-x) +(cy-y)*(cy-y)<= radius*radius
#macro display
fr=map(125,475,cx,10,65)
screenlock
cls
draw string(20,20),"Actual Framerate = " &fps
draw string(20,40),"Requested Framerate = " &fr
draw string(20,60),"Sleeping time = " &sleeptime
draw string(100,280),"10"
draw string(480,280),"65"
draw string(250,280),"<--slider-->"
angle+=.1
drawline(600,200,.2*sin(angle)-pi/2,300,4)
line(100,300)-(500,350),2,bf
circle(cx,cy),25,5,,,,f
screenunlock
sleep sleeptime,1
#endmacro
#macro mouse
Dim As Long x=mx,y=my,dx,dy
While mb = 1
Display
Getmouse mx,my,,mb
If inbox Then
If mx<>x Or my<>y Then
dx = mx - x
dy = my - y
x = mx
y = my
cx=x+dx
if cx<125 then cx=125
if cx>475 then cx=475
End If
End If
Wend
#endmacro
sub drawline(x as long,y as long,angle as single,length as long,col as ulong)
var x2=x+length*cos(angle)
var y2=y-length*sin(angle)
line(x,y)-(x2,y2)
circle(x2,y2),10,6,,,,f
end sub
Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
Static As Double timervalue,_lastsleeptime,t3,frames
Var t=Timer
frames+=1
If (t-t3)>=1 Then t3=t:fps=frames:frames=0
Var sleeptime=_lastsleeptime+((1/myfps)-T+timervalue)*1000
If sleeptime<1 Then sleeptime=1
_lastsleeptime=sleeptime
timervalue=T
Return sleeptime
End Function
screen 19
dim as long mx,my,mb
dim as long cx=125,cy=325,fps,fr,sleeptime
dim as single angle,pi=4*atn(1)
do
getmouse mx,my,,mb
display
if incircle(cx,cy,25,mx,my) and mb=1 then
mouse
end if
sleeptime= regulate(fr,fps)
loop until len(inkey)
```

You can increase framerate in windows by using timebegin/end period.

This is because the resolution (sleep 1) is now between 1 and 2 milliseconds, otherwise it is about 15 milliseconds.

Code: Select all

```
#ifdef __FB_WIN32__
declare function settimer alias "timeBeginPeriod"(as Ulong=1) as long
declare function freetimer alias "timeEndPeriod" (as Ulong=1) as long
#endif
#define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
#define inbox (mx>125) and (mx<475)' and (my>300) and (my<350)
#define incircle(cx,cy,radius,x,y) (cx-x)*(cx-x) +(cy-y)*(cy-y)<= radius*radius
#macro display
fr=map(125,475,cx,10,350)
screenlock
cls
draw string(20,20),"Actual Framerate = " &fps
draw string(20,40),"Requested Framerate = " &fr
draw string(20,60),"Sleeping time = " &sleeptime
draw string(100,280),"10"
draw string(480,280),"350"
draw string(250,280),"<--slider-->"
angle+=.1
drawline(600,200,.2*sin(angle)-pi/2,300,4)
line(100,300)-(500,350),2,bf
circle(cx,cy),25,5,,,,f
screenunlock
#ifdef __FB_WIN32__
settimer
#endif
sleep sleeptime,1
#ifdef __FB_WIN32__
freetimer
#endif
#endmacro
#macro mouse
Dim As Long x=mx,y=my,dx,dy
While mb = 1
Display
Getmouse mx,my,,mb
If inbox Then
If mx<>x Or my<>y Then
dx = mx - x
dy = my - y
x = mx
y = my
cx=x+dx
if cx<125 then cx=125
if cx>475 then cx=475
End If
End If
Wend
#endmacro
sub drawline(x as long,y as long,angle as single,length as long,col as ulong)
var x2=x+length*cos(angle)
var y2=y-length*sin(angle)
line(x,y)-(x2,y2)
circle(x2,y2),10,6,,,,f
end sub
Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long
Static As Double timervalue,_lastsleeptime,t3,frames
Var t=Timer
frames+=1
If (t-t3)>=1 Then t3=t:fps=frames:frames=0
Var sleeptime=_lastsleeptime+((1/myfps)-T+timervalue)*1000
If sleeptime<1 Then sleeptime=1
_lastsleeptime=sleeptime
timervalue=T
Return sleeptime
End Function
screen 19
dim as integer mx,my,mb
dim as long cx=125,cy=325,fps,fr,sleeptime
dim as single angle,pi=4*atn(1)
do
getmouse mx,my,,mb
display
if incircle(cx,cy,25,mx,my) and mb=1 then
mouse
end if
sleeptime= regulate(fr,fps)
loop until len(inkey)
```