# time.h and its uses

## Recommended Posts

Hello everybody, I am creating a Tetris game, and as you all should know, a time interval is needed for the dropping of bricks. So I figured out how to figure out how many seconds have gone by, but I want to be able to drop the bricks at a rate faster than one second. I searched for how to do this, but I couldn't find it. Do I need a different library, or what do I do?

##### Share on other sites
Are you looking for something like Sleep() in windows.h?

##### Share on other sites
If you're using windows, I'd say go with something like this, which uses QueryPerformanceCounter. I'm not sure what to do in mac-land, though, failing that. Sorry, but hope that sparks something!

-jouley

[Edit: Forgot to hitthe spacebar.]

##### Share on other sites
Standard time.h has clock which returns the clock ticks elapsed since process start.

It is not particularly accurate as far as I know, but it is certainly higher resolution than one second.

However, I'm sure there must be a Mac equivalent to Windows QueryPerformanceCounter.

HTH

##### Share on other sites
So what I'm trying to do is get a certain amount of time to pass, in which there can be input, based on how many levels have gone by (so maybe something like -0.1 seconds per level). Does anyone know how this would work?

##### Share on other sites
Ok, so I think that I might have found something that I need for time.h, but for some reason, I got an error when entering the text reference that EC gave me.
It says that CLK_TCK was undefined, but the website says that that's a constant. What is wrong here?

[Edited by - adam_o on December 15, 2006 9:11:56 PM]

##### Share on other sites
Quote:
 Original post by adam_oOk, so I think that I might have found something that I need for time.h, but for some reason, I got an error when entering the text reference that EC gave me.It says that CLK_TCK was undefined, but the website says that that's a constant. What is wrong here?

##### Share on other sites
Quote:
Original post by gumpy macdrunken
Quote:
 Original post by adam_oOk, so I think that I might have found something that I need for time.h, but for some reason, I got an error when entering the text reference that EC gave me.It says that CLK_TCK was undefined, but the website says that that's a constant. What is wrong here?

That works, thank you tons... where it says wait(1), can this be changed to a floating-point number? (A.K.A. for less than one second)

##### Share on other sites
void wait(float seconds) {    clock_t stop = clock() + (clock_t)(seconds * CLOCKS_PER_SEC);    while(clock() < stop) {/* do nothing */}}

keep in mind that clock() isn't always a good choice for games due to accuracy issues on various platforms. what libraries/apis are you using?

##### Share on other sites
Quote:
 Original post by gumpy macdrunkenvoid wait(float seconds) { clock_t stop = clock() + (clock_t)(seconds * CLOCKS_PER_SEC); while(clock() < stop) {/* do nothing */}}keep in mind that clock() isn't always a good choice for games due to accuracy issues on various platforms. what libraries/apis are you using?

Anything including:
[code]
#include <stdio.h>
#include <iostream>
#include <GLUT/glut.h>
#include <time.h>
using namespace std;