what's the best way to set up a timer?

This is a discussion on what's the best way to set up a timer? within the Game Programming forums, part of the General Programming Boards category; Hey guys, I'm sure this message is asked quite often but I can't find what I'm looking for. I want ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    34

    what's the best way to set up a timer?

    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?
    -gunder

    if (problem)
    postcount++;

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    195
    I made a stopwatch funtion that can be doubled as a timer using a while loop just include systimer.h
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    34
    I'm at work right now but I'll take a look at it when I get home, thanks!
    -gunder

    if (problem)
    postcount++;

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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.
    Away.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    34
    oh sorry, I forgot to mention I'm doing this in the console as an ascii app. I'm using dev-c++ on winxp.
    -gunder

    if (problem)
    postcount++;

  6. #6
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    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.
    It is not the spoon that bends, it is you who bends around the spoon.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    32
    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.

  8. #8
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    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.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    25
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 01:23 AM
  2. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 11:05 AM
  3. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 03:44 AM
  4. how to set timer
    By marianne in forum C Programming
    Replies: 1
    Last Post: 02-12-2003, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21