PDA

View Full Version : clock() function



mickey
10th September 2006, 22:14
hi,
I need use clock() (or similar but not QT functions!) to do this:


int done =1
while (done) {
clearScreen()
if (anyPressedKey) done=0;
float posy = clock()/20;
mySetPos(10, posy-40);
print ("HELLO EVERY BODY ");
if (posy>600) posy -= 600;
}

I need the text go down and when it reach 600 it wait a while and restart from start point...Any hints?
this code doesn't work. and float posy could reach its high bound.......
Is there a way to reset and/or stop the clock() counter????
Thanks

jacek
10th September 2006, 22:45
Is there a way to reset and/or stop the clock() counter????
No, there isn't, but you can use modulo.

mickey
10th September 2006, 23:31
hi I do this and seem ok;


int posy = ((int) (clock()/20)) %600;

But problem is that when posy reach 600, the print should be later of 5 seconds;
I can code this:


if (posy ==0)
for (int i =0; i<1000; ++i) ;

but this has different behavior on different machines and more imposrtant: posy GOES ON......(so next print don't print at '0' point but '0' plus something...)
Are u understanding this problem? Any hints?Thanks...

jacek
10th September 2006, 23:52
You can use sleep() to wait 5 seconds.

mickey
11th September 2006, 00:21
thanks for fast reply;
unser win32 there's Sleep() function that works with milliseconds. I code this:


if (posy == 600 ) Sleep(10000) //10 secs;

But don't resolve my problem:
I'm inside a while that catch mouse event also : Sleep block all;
Furthermore: clock() isn't stopped; so next time print() doesn't print at '0' point but '0' + something.... nut problem? thanks.....
----
additional prob:
int posy = ((int) (clock()/20)) %600; posy is an int: so the when print at '0' and print at '1' I can see a very little jump.....a float value should be more linear...... but I read '%' works between int????

jacek
11th September 2006, 10:03
I'm inside a while that catch mouse event also : Sleep block all;
Furthermore: clock() isn't stopped; so next time print() doesn't print at '0' point but '0' + something.... nut problem?
You can always subtract some value from the one returned by clock(). This way you can measure time.


int posy = ((int) (clock()/20)) %600; posy is an int: so the when print at '0' and print at '1' I can see a very little jump.....a float value should be more linear...... but I read '%' works between int????

double posy = ( clock() % 12000 ) / 20.0

high_flyer
12th September 2006, 15:54
but this has different behavior on different machines
CLOCKS_PER_SEC macro will return how many clock ticks per second a the local system uses.