C: C++ timers

From FSDeveloper Wiki
Revision as of 12:06, 11 October 2012 by Narutokun-8877 (talk | contribs)
Jump to navigationJump to search


Most of you may be familiar with the TICK18 token variable which is incremented at 18Hz. Another option to make use of the 18Hz increment system (like making 0.5 second timers and such) is to call a function from the PANEL_SERVICE_PRE_UPDATE case of your callback function which increments your timing variable which you simply reset to 0 when it is equal to 9 (for a 0.5 second timer). However there is another option that allows for timers that range from several minutes down to 1 millisecond.

#include windows.h
void CALLBACK My_Timer();
UINT_PTR IDT_TIMER1;

The above code creates the function prototype for the callback function which will be executed at the desired interval. It also creates the timer ID variable.

To create the timer in your gauge callback function:

case PANEL_SERVICE_PRE_INITIALIZE:
SetTimer(NULL, IDT_TIMER1, 1000, (TIMERPROC)My_Timer);
//1st parameter is optional and not required in this
//case, second parameter is timer ID, third parameter is timer interval in milliseconds, 4th parameter is callback
//function
break;

Once you are finished with the timer, remove it as shown below:

case PANEL_SERVICE_DISCONNECT:
KillTimer(NULL, IDT_TIMER1);
break;

To increment your timer variable:

int timer_1sec = 0;
void CALLBACK My_Timer()
{
timer ++;
}