Solved

Win32 Timers

Posted on 1998-10-20
4
2,101 Views
Last Modified: 2009-12-04
I am programming a DirectX game in Delphi 2.0, and I want to have a timer fire off messages about 50 times a second so I can update the display and game variables. I tried the TTimer component that comes with Delphi, but the display kept "shaking" and I don't like this. Is it possible that I can utilize the timers in the Windows API? If so, how can I do this? Say I want Form1.OnUpdate to recieve the messages. How can I specify this in the Win32 SetTimer() function?
0
Comment
Question by:mmarvi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1343526
The WinAPI SetTimer functions uses a low precision clock.  Tick 18.2 per sec.  Too slow
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1343527
mmarvi

Here is how the SetTimer API is called in delphi...

SetTimer(Form1.Handle, WM_TIMER, 50, nil);

Later
BoRiS


0
 
LVL 5

Accepted Solution

by:
inter earned 70 total points
ID: 1343528
Here is the implementation part of correctly setting a high resolution multimedia timer, note that you can not acquire higher than your system reports, I think the code is self explanatory, anyway just ask if you stuck -I have already test the code-
regards, igor

implementation

uses
  mmsystem;

const
  TARGET_RESOLUTION = 1;         // 1-millisecond target resolution
var
  wTimerRes : UINT; //this is a global variable where we read the min timer period
  wTimerID : UINT; //id of our timer if succesfull

function Min(x,y : integer):integer;
begin
  Result := y;
  if x<y then Result := x;
end;

function Max(x,y : integer):integer;
begin
  Result := y;
  if x>y then Result := x;
end;

//obtain timer capabilities
function CheckTimerCaps:boolean;
var
 tc : TTIMECAPS;
begin
  if (timeGetDevCaps(@tc, sizeof(TTIMECAPS)) <> TIMERR_NOERROR) then
    Result := false
  else
  begin
    wTimerRes := min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
    timeBeginPeriod(wTimerRes);
    Result := true;
  end;
end;

//this is the core called by windows
procedure TimerCallback(TimerID , msg : UINT;
      dwUser, dw1, dw2 : DWORD); stdcall;
begin
  //************ DO WHAT YOU WANT HERE **************************
end;

// setup timer event and callback
// returns false if failed
// just give the number of miliseconds you want
function SetTimerCallback(msInterval : UINT):boolean;  // event interval
begin
    wTimerID := timeSetEvent(
        msInterval,                    // delay
        wTimerRes,                     // resolution (global variable)
        TimerCallback,               // callback function
        0,                  // user data
        TIME_PERIODIC );                // single timer event

    Result := wTimerID <> 0;
end;

procedure DestroyTimer;
begin
  if(wTimerID <> 0) then// is timer event pending?
  begin
     timeKillEvent(wTimerID);  // cancel the event
     wTimerID := 0;
  end;
end;


// TRY TO START TIMER with 10 msec
procedure TForm1.Button1Click(Sender: TObject);
begin
  if CheckTimerCaps then
    if SetTimerCallback(10) then
    begin
      ShowMessage('Timer Succesful : '+IntToStr(wTimerRes));
    end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DestroyTimer;
end;

//DELETE TIMER
procedure TForm1.Button2Click(Sender: TObject);
begin
  DestroyTimer;
end;

end.  //unit
0
 
LVL 5

Expert Comment

by:inter
ID: 1343529
It generates periodic event as you want, but I missed a wrong comment there (I have addaped it before msdn)
regards, igor
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question