# Whats wrong with my timer?

## Recommended Posts

prux    138
Hey, Im writing a game and using timing in the simplest way: while Pmsg.message <> WM_QUIT do begin if PeekMessage(pMsg, 0, 0, 0, PM_NOREMOVE) then begin if not GetMessage(pMsg, 0, 0, 0) then break; TranslateMessage(pMsg); DispatchMessage(pMsg); end else begin if GetTickCount - FHTimer >= 20 then begin FHTimer:= GetTickCount; Updating etc end; Renering; end; end; well it seemed to be working - by now, because I tried it with a much better computer and it was slowed! Not 20 but 16 sec was the correct number. What can be wrong? I also use FPS-counting and it does better. Do I need to use multimedia timer? Aint works, it drops memory-violation errors randomly. What to do?

##### Share on other sites
Evil Steve    2017
GetTickCount() is only accurate to something like 16ms, so using it like this isn't very accurate at all. A multimedia timer (timeGetTime() or whatever the equivalent for your language is) would be better.

##### Share on other sites
prux    138
Yes, now I know. But many example shown that.
Now I changed the code. It seems to be perfect although its alway better to ask the others's oppinion :)

.
.
.

timerID:= TimeSetEvent(20,20, New1C, 0, TIME_PERIODIC);

.
.
.

procedure New1C (uTimerID, uMessage: UINT; dwUser, dw1, dw2: DWORD) stdcall;
begin
PostMessage (MainWindow, wm_user, 0,0);
end;

.
.
.

killtimer

.
.
.

and the cycle:
 while Msg2.message <> WM_QUIT do begin   if PeekMessage (Msg2, 0,0,0, PM_REMOVE) then     begin      TranslateMessage (Msg2);      DispatchMessage (Msg2);      if Msg2.message = wm_user then        begin         UPDATES!!!!!!!!!        end;     end     else     begin      RENDERING     end; end;

So this means the callback routine doesnt run the updates (because it crashes sometimes, dunno why - maybe this is about the synchronization?! lol I neved unserstood the threads) but it just send a message and the main application processes it.