-
Delays
I've been looking all over the net for some way to make C delay the print of either each character, or entire printf statements. I need to make a small program that will display text as if it was being typed by someone else in real time, so therefore delaying the display of each character. Does anyone know how this can be done in C? (I don't care for portability from Win32 to linux on this one)
Thx in advance! :)
-
Have you tried using GetTicCount()? Game developers use this to sync their frame rates... when programming in windows..
I tested this code inside main and it worked....
every half second a char was sent to the screen....
place the code inside main or another function of course....
Code:
#include<windows.h>
DWORD time = GetTickCount();
char mybuff[100] = "zman";
int i = 0;
while( i < 5 )
{
if( GetTickCount() > time + 500 )//in milliseconds
{
time = GetTickCount();
putchar( mybuff[i] );
i++;
}
}
-
And there is also Sleep().
#include <windows.h>
.
.
Sleep(1000); //Sleep 1 second
-
Code:
/* Delays 10 seconds, then exits........................*/
#include "stdio.h"
#define 10_SECONDS 500000000 // well, its about 10 seconds
void delay(int nbr)
{
int i = 0;
for(i = 0; i < nbr; i++)
;
}
int main()
{
printf("\n Hold on..."); delay(10_SECONDS);
printf("\n Ok, returned from 'delay()'");
return 0;
}
*Note : The delay function above is good for personal use only, because if you were to run this program on a faster computer..... the delay function would return faster. If you ran this program on a slow computer, delay() might take a while to return, thus if you pass the constant '10_SECONDS' that is defined as 500000000 to the delay function, it could be perfect for you, but could be extremely slow or fast depending on another users PC because of its cpu speed etc....
-
Hi!
You can make knaveīs delay-function universal if it measures at first the systemīs speed and makes the value 10_SECONDS depend on it. But I donīt know how to read the miliseconds that have passed after midnight, but I know that itīs possible.
Does anyone know?
I could write the universal function then.
klausi