Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Win32 Timers

Posted on 1998-10-20
4
Medium Priority
?
2,115 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
  • 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 140 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

783 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