Sign in to follow this  
adam_o

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by gumpy macdrunken
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


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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by gumpy macdrunken

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?


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this