C: C++ timers

From FSDeveloper Wiki
Revision as of 13:52, 23 April 2015 by BASys (talk | contribs) (FSVersion infobox . Reinstated standard listing)
Jump to: navigation, 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).

The following however is an undocumented function in the simulator which returns total time elapsed in seconds with sub-18Hz accuracy.

In your choice of your project's header files, do the following function prototype:

typedef double (__cdecl *fGetSimTime)();

Then in your module_init function:

HMODULE hsimsched = NULL;
fGetSimTime GetSimTime6 = NULL;
void FSAPI module_init(void)
{
 GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"simscheduler.dll", &hsimsched);
 if(hsimsched)
  GetSimTime6 = (fGetSimTime)GetProcAddress(hsimsched, MAKEINTRESOURCEA(6));//Note, for FSX SP2 use 4 instead of 6
}

Now wherever you wish to call this function, include the header where the prototype is contained and add:

extern fGetSimTime GetSimTime6;

To call it and find out how much time has passed:

double time = GetSimTime6();

UPDATE:

The ordinal used by P3D v2.5 is different to that of FSXA. Also, the format is such that the decorated function name is available from the dll. So for P3D v2.5, use the following:

GetSimTime6 = (fGetSimTime)GetProcAddress(hsimsched, "?GetElapsedSimTimeSec@@YGNXZ");