time.h and its uses
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?
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.]
-jouley
[Edit: Forgot to hitthe spacebar.]
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
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
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?
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]
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]
Quote:Original post by adam_o
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?
try CLOCKS_PER_SEC instead of CLK_TCK
Quote:Original post by gumpy macdrunkenQuote:Original post by adam_o
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?
try CLOCKS_PER_SEC instead of CLK_TCK
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)
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?
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:
#include <stdio.h>
#include <iostream>
#include <GLUT/glut.h>
#include <time.h>
using namespace std;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement