time.h and its uses

Started by
8 comments, last by adam_o 17 years, 4 months ago
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?
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
Advertisement
Are you looking for something like Sleep() in windows.h?
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.]
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
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?
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
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]
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
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
This space for rent.
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)
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
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?
This space for rent.
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:

#include <stdio.h>
#include <iostream>
#include <GLUT/glut.h>
#include <time.h>
using namespace std;
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!

This topic is closed to new replies.

Advertisement