View Full Version : what's the best way to set up a timer?
gunder
10-25-2003, 07:58 PM
Hey guys, I'm sure this message is asked quite often but I can't find what I'm looking for. I want to write a tetris game and I'm trying to figure out how to make the peices "fall". The only thing my book talks about is using the sleep() and delay() commands. While that would work .. what happens if someone inputs a command while the sleep or deleay is in effect, I don't think the input actually goes through does it? I've thought about just getting the system time and then checking to see if a certain amount of time has passed, if so them "drop" the peice. I read somewhere that that was a bad idea though, I can't remember why. Can anyone point me in the right direction?
KneeGrow
10-25-2003, 08:21 PM
I made a stopwatch funtion that can be doubled as a timer using a while loop just include systimer.h
gunder
10-25-2003, 08:30 PM
I'm at work right now but I'll take a look at it when I get home, thanks!
confuted
10-25-2003, 08:30 PM
If you're using the windows API, you can set a timer and handle the timer message. I don't know the details; search MSDN or the windows programming board.
gunder
10-25-2003, 08:41 PM
oh sorry, I forgot to mention I'm doing this in the console as an ascii app. I'm using dev-c++ on winxp.
Sayeh
10-30-2003, 12:13 PM
You don't use a compiler-dependent loop. If you want time with any accurate resolution, you use an interrupt. You use the time manager and set up and event with the address of a callback function that you want the O/S to call at specific intervals.
In any application where you need regular time or need to slice time in order to control the speed of execution, this is the method used. Any platform.
Inside the callback, you can set a global variable to either true or false, that the rest of your code queries whenever it goes to "do" something. If that flag is false, the code returns without doing anything. If it's true, the code does it's thing.
manwhoonlyeats
12-02-2003, 04:08 PM
after reading these posts and searching around for other information, i still cannot find anything that i can understand. I am trying to make an ASCII game with some sort of delay function. I tried what they said not to do, checking the clock() but it didn't work. How do i program the interupts like sayeh is talking about.
HybridM
12-02-2003, 04:31 PM
Even though you're writing for console you can still use Win32API functions.
I recommend you have a look at GetTickCount(), it returns a DWORD that is the number of milliseconds since Windows started. So you can make a call to GetTickCount() every time you move a block and retain the number it returned, and then only move a block if GetTickCount() returns a DWORD that is 500 (half a second) or so greater than the one you retained from last move.
But you must note: "The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if Windows is run continuously for 49.7 days."
No biggie though.
pheer
12-08-2003, 12:59 PM
If you want better resolution than GetTickCount you can use
timeGetTime() found in mmsystem.h, but first you set the resolution in milliseconds with timeBeginPeriod().
If you want even better resolution you can use QueryPerformanceCounter() and QueryPerformanceFrequency(), read about them in win32 programmers reference or at msdn
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.