Code: Select all
sleep 1, 1
Code: Select all
sleep 1, 1
Code: Select all
Function framecounter() As Integer
Var t1=Timer,t2=t1
Static As Double t3,frames,answer
frames+=1
If (t2-t3)>=1 Then t3=t2:answer=frames:frames=0
Function= answer
End Function
Function Regulate(MyFps As Integer,Byref fps As Integer) As Integer
fps=framecounter()
Static As Double timervalue,lastsleeptime
Dim As Double delta
Var k=fps-myfps,i=1/myfps
If Abs(k)>1 Then delta+=i*Sgn(k)
Var sleeptime=lastsleeptime+(i-Timer+timervalue)*2000+delta
If sleeptime<1 Then sleeptime=1
lastsleeptime=sleeptime
timervalue=Timer
Return sleeptime
End Function
screen 7
color ,4
dim as integer fps
do
var sleeptime= regulate(20,fps)
screenlock
cls
draw string (50,50),"FPS = " &fps
draw string(50,70),"SLEEP " &sleeptime
screenunlock
sleep sleeptime
loop until len(inkey)
Code: Select all
Sub musicplaying() 'Play the music in the background, in a separate thread
Do
PLAY music
Loop
End Sub
the DO LOOP without any condition to abort around the play command looks strange for me.chrowle wrote:Code: Select all
Sub musicplaying() 'Play the music in the background, in a separate thread Do PLAY music Loop End Sub
Code: Select all
Sub musicplaying_thread(parg as any ptr)
PLAY music
End Sub
ThreadCreate @musicplaying_thread,0
Code: Select all
Sub musicplaying
PLAY music ' starts a seperate thread
End Sub
musicplaying
Code: Select all
Sub musicplaying() 'Play the music in the background, in a separate thread
Do
PLAY music
Loop
End Sub
Dim as any ptr musicthread = threadcall musicplaying 'the music thread
I have investigated the use of select() on Unix-type systems, and I am hearing through the grapevine that it is used because it will not interfere with other timing mechanisms. It has high resolution (4ms or so), and is apparently completely valid when called the way fb is doing:pestery wrote:I'm a little late to the party, but I have a semi related question. I just checked the source code and the FreeBASIC Sleep() command calls a function called fb_Delay(). The Unix version of fb_Delay() calls the network command Select(), which is what does the work.
So in short, on Unix/Linux, Sleep() is for the most part just an alias for Select(). I'm a Windows user and have not idea about the Unix/Linux API but isn't there another function that could be used instead? Sorry if I'm coming of critical, it's just that from my understanding Select() is not intended to be used at a sleep command.
Code: Select all
#include "../fb.h"
#include <sys/select.h>
FBCALL void fb_Delay( int msecs )
{
struct timeval tv;
tv.tv_sec = msecs / 1000;
tv.tv_usec = (msecs % 1000) * 1000;
select(0, NULL, NULL, NULL, &tv);
}
Code: Select all
/* sleep multiplexer function */
#include "fb.h"
/*:::::*/
FBCALL int fb_SleepEx ( int msecs, int kind )
{
switch( kind ) {
case 0:
fb_Sleep( msecs );
break;
case 1:
fb_Delay( msecs );
break;
default:
return fb_ErrorSetNum( FB_RTERROR_ILLEGALFUNCTIONCALL );
}
return fb_ErrorSetNum( FB_RTERROR_OK );
}
Code: Select all
#include "../fb.h"
#include <windows.h>
FBCALL void fb_Delay( int msecs )
{
Sleep( msecs );
}
Code: Select all
/* sleep function */
#include "fb.h"
/*:::::*/
FBCALL void fb_Sleep ( int msecs )
{
FB_SLEEPPROC sleepproc;
FB_LOCK();
sleepproc = __fb_ctx.hooks.sleepproc;
FB_UNLOCK();
if( sleepproc ) {
sleepproc( msecs );
} else {
fb_ConsoleSleep( msecs );
}
}
Code: Select all
/* sleep() function */
#include "fb.h"
void fb_ConsoleSleep( int msecs )
{
#if defined( HOST_XBOX )
/* NOTE: No need to test for input keys because sleep will be hooked
* when the application is switched to graphics mode and the
* console implementations for keyboard handling are only dummy
* functions.
*/
fb_Delay( msecs );
#else
/* infinite? wait until any key is pressed */
if( msecs == -1 ) {
while( !fb_hConsoleInputBufferChanged( ) )
fb_Delay( 50 );
return;
}
/* if above n-mili-seconds, check for key input, otherwise,
don't screw the precision with slow console checks */
if( msecs >= 100 ) {
while( msecs > 50 ) {
if( fb_hConsoleInputBufferChanged( ) )
return;
fb_Delay( 50 );
msecs -= 50;
}
}
if( msecs >= 0 )
fb_Delay( msecs );
#endif
}
Code: Select all
/* sleep function */
#include "fb_gfx.h"
int fb_hGfxInputBufferChanged( void );
/*:::::*/
void fb_GfxSleep ( int msecs )
{
/* infinite? wait until any key is pressed */
if( msecs == -1 )
{
while( !fb_hGfxInputBufferChanged( ) )
fb_Delay( 50 );
return;
}
/* if above n-mili-seconds, check for key input, otherwise,
don't screw the precision with slow console checks */
if( msecs >= 100 )
while( msecs > 50 )
{
if( fb_hGfxInputBufferChanged( ) )
return;
fb_Delay( 50 );
msecs -= 50;
}
if( msecs > 0 )
fb_Delay( msecs );
}
It used to be the typical maximally cross-*nix (though wrongly advocated as crossplatform) way, and is thus recommended in many textbooks.pestery wrote:Sorry if I'm coming of critical, it's just that from my understanding Select() is not intended to be used at a sleep command.