Timers, Timers, Timers!

This is a discussion on Timers, Timers, Timers! within the Game Programming forums, part of the General Programming Boards category; I want to know how to make a simple timer. I don't understand the whole WM_TIMER message thing so can ...

  1. #1
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Exclamation Timers, Timers, Timers!

    I want to know how to make a simple timer. I don't understand the whole WM_TIMER message thing so can someone tell me simply how to do it? I mean the whole SetTimer function.
    Thanks!
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    A simple search on MSDN...
    http://msdn.microsoft.com/library/de...singtimers.asp
    You could have easily come up with that yourself.

    Hope that helps.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    3
    If the timer is for movement and animation in a game then you should use timeGetTime() or the High Performance Timer.

  4. #4
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    A simple search on MSDN...
    http://msdn.microsoft.com/library/d...usingtimers.asp
    You could have easily come up with that yourself.

    You see, the funny thing is, that I don't understand. Speak to me like I don't understand. Slow, and easy. I'm just getting used to Win32 programming, and I wanna make a timer. Some example code would be nice
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  5. #5
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    sometimes Microsoft don't explain things well for n00bs.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    Setting up a timer is easy.

    In your main() function you call
    Code:
    SetTimer(hwnd, TimerID, TimerDelay, TimerProc);
    where TimerID is any ID you make up (say, 1), TimerDelay is the period in milliseconds, and TimerProc is the name of a function to be notified whenever the period has elapsed. Set the last argument to NULL to get a WM_TIMER message posted to the message queue. Then use a WM_TIMER case in your WndProc and do whatever it is you want to do every X millseconds.

    When you're done with the timer call KillTimer(hwnd, TimerID). Hope that helps.

    BarrySlisk -- what is the High Performance Timer? Is it a timer whose messages are high-priority? My experience using this WM_TIMER method to control animation is that it's limited because the smallest unit you can use is 1 millesecond, which sounds fine except it isn't -- in the game I'm working on now (which draws hundreds of images per frame) it's just too slow. I haven't investigated too far but I'm doubting that the WM_TIMER message is actually being posted every millisecond. Could you explain how/if you use them to control game speed? Or what method you do use?

    I've written my own functions to determine the computer's speed, and it leads to faster, smoother, more dynamic action but it doesn't reliably discern the speed of the computer -- it could use some work. I could show you what I'm doing if you'd like, and maybe you could suggest a better solution.

    What methods do you all use?

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    WM_TIMER msg's are low priority, they will actually be dropped down the msg que if other more inportant msg's arrive.

    QueryPerformanceFrequency() and QueryPerformanceCounter() for better resolution.


    I like to define some numbers
    #define ID_MY_TIMER WM_USER+1001
    #define TIMER_DELAY 30000 //30 seconds

    Use this under WM_TIMER in your call back

    Code:
    case  WM_TIMER:
    if(wParam==g_uTimer)//it is this particular timer if we have more than one timer
    {
           //timer is running so kill it
           KillTimer(hWnd,g_uTimer);
    
           //run the code for the timer
    
           //start a new one  (use this other places)
           g_uTimer=SetTimer(hWnd, ID_MY_TIMER, TIMER_DELAY, NULL);
           // add error check here 
    break;
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Starting to understand...

    Ok, fusikon Really, REALLY, helped. But how to I go upon using the WM_TIMER thingy? I'm really a noob at this stuff, and I need to be spoken to likely.

    sometimes Microsoft don't explain things well for n00bs.
    Well said.
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    I track the timers by the UINT returned from the SetTimer() function so fusikon snippet will not work (as he does not catch the return). (easiest way is to set it as a global variable)

    When you need to start the timer call SetTimer(). In your main callback (wndproc) insert the WM_TIMER case. All you need to add is the code you want to execute when the timer 'goes off'.

    Try what we have given you, post specific problems, as you have all the code you need to run a timer here.


    NOTE:
    >>When you're done with the timer call KillTimer(hwnd, TimerID).

    AFAIK this is will not work. TimerID should be the return from the SetTimer() call, Not the ID used.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    >>When you're done with the timer call KillTimer(hwnd, TimerID).
    AFAIK this is will not work. TimerID should be the return from the SetTimer() call, Not the ID used.
    Actually my way is the right way. You only pass in the uint returned from SetTimer if you set hwnd to NULL, that is:
    Code:
    KillTimer(NULL, returnValueFromSetTimer)
    but
    Code:
    KillTimer(hwnd, TimerID)
    This is at least what the MS docs say.

    Another way to get a high-resolution timer is to use timeGetTime() (which requires mmsystem.h and libwinmm.a), but set the performance resolution to 1ms first using a call to timeBeginPeriod(UINT), and, before you close the program, a call to timeEndPeriod(UINT) where UINT is the resolution in ms and you pass the same value to both functions.
    timeGetTime returns a DWORD number (milliseconds since Windows started) and you can use this number for timing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 08:56 AM
  2. Enumerating timers and hotkeys
    By maxorator in forum Windows Programming
    Replies: 3
    Last Post: 01-01-2007, 11:47 AM
  3. Programming Timers
    By jpablo in forum C Programming
    Replies: 5
    Last Post: 04-09-2006, 01:53 AM
  4. switch cases and timers
    By soranz in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 07:43 PM
  5. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 04:34 AM

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