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!
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)
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.
If the timer is for movement and animation in a game then you should use timeGetTime() or the High Performance Timer.
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)
Setting up a timer is easy.
In your main() function you callwhere 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.Code:SetTimer(hwnd, TimerID, TimerDelay, TimerProc);
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?
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
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.
Well said.sometimes Microsoft don't explain things well for n00bs.
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)
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
Actually my way is the right way. You only pass in the uint returned from SetTimer if you set hwnd to NULL, that is:>>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.
butCode:KillTimer(NULL, returnValueFromSetTimer)
This is at least what the MS docs say.Code:KillTimer(hwnd, TimerID)
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.