Solved

Win32 Timers

Posted on 1998-10-20
4
2,098 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

Technology Partners: 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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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