win 32 with windows api

Started by
10 comments, last by donjonson 19 years ago
I am just playing around with windows api programming and I have a few questions I made a class to draw a line using win api anywhare in a window. it is drawn using a start point, length and an angle. So the first thing i did to test out this new class was simply make a rotating line abouta center point using a windows timer. I set the timer for 1 milisecond. each WM_TIMER message i add 1 to the angle of the line and redraw. This works properly, the line spins around it's start point. however I dont think it is operating at 1 khz (which is ther freq of a 1 milisecond timer if I am not mistaken). It seems to be going much slower. is this because my code is perhaps not very efficient or because of some inneficency in win32/winapi? I am at work now so I cant post the exe file but if you want to see it Ill post it when i get home
These tears..leave scars...as they run down my face.
Advertisement
It is HIGHLY unlikely that your code would cause it to slow down...
I mean, after all you are just drawing a line....

How do you setup your timer???
during the WM_CREATE message handler

case WM_TIMER:SetTimer(hwnd,1,1,NULL);break;
These tears..leave scars...as they run down my face.
With Windows, there is no guarantee that your WM_TIMER messages will be received at exactly the interval you specified. GetMessage/PeekMessage will only post a WM_TIMER event when there are no events of higher priority in the queue. WM_TIMER is low priority. Also, there is a USER_TIMER_MINIMUM that defines the smallest time interval. If the value you specify is less than that, it is automatically (and quietly) set to USER_TIMER_MINIMUM. So, just because the interval is represented in milliseconds doesn't mean that the resolution will be 1 millisecond.
from msdn:

"The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue."

so what is a better way to get a more precise timer?
These tears..leave scars...as they run down my face.
Use QueryPerformanceCounter to get a high-resolution time value. Then simply update your angle as part of your message loop, scaling the change in angle by the amount of time elapsed. (Assuming you use PeekMessage instead of GetMessage).
Awsome thanks alot
These tears..leave scars...as they run down my face.
It seems to me that your problem here is that you are telling the timer to fire a timer event every millisecond, which will actually mean that the line is rotating so fast you cant actually see it smoothly move.
Try something like:

SetTimer(hwnd,1,1000,NULL);

Which will fire it every second
you are right, if the angle were actuly changin every milisecond, it would be moving to fast to see. but it takes 2 minutes for the line to get all the way around the circle. It is not working at 1 khz .so for a one minute timer it would take 6 minutes to go around once and if i set it to do that it does. I will try the other method suggested.
These tears..leave scars...as they run down my face.

This topic is closed to new replies.

Advertisement