Difference between revisions of "C: C++ timers"

From FSDeveloper Wiki
Jump to: navigation, search
Line 29: Line 29:
 
   GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"simscheduler.dll", &hsimsched);
 
   GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"simscheduler.dll", &hsimsched);
 
   if(hsimsched)
 
   if(hsimsched)
   GetSimTime6 = (fGetSimTime)GetProcAddress(hsimsched, MAKEINTRESOURCEA(6));//Note, for FSX SP2 use 4 instead of 6. Also note that if you wish to distinguish dynamically between sim versions, FSX Acceleration has a offset (function address-hsimsched value) of 0x1530, and SP2 of 0x14D0. FSX SE may vary as builds get released but since FSXA and SP2 aren't likely to change you can check if it is not equal to their offsets and the P3D v2 function isn't located by function name, you can assume FSX SE and use ordinal 6.
+
   GetSimTime6 = (fGetSimTime)GetProcAddress(hsimsched, MAKEINTRESOURCEA(6));//Note, for FSX SP2 use 4 instead of 6. Also since neither Acceleration nor SP 2 are likely to be updated,
  }
+
//one can assume the offsets (0x14D0 for SP2 and 0x1530 for acceleration) will remain constant, thus leaving only P3D and FSX SE with offsets that vary.
 +
//P3D can be retrieved with the function name thus elliminating confusion of function ordinals.  }
  
 
Now wherever you wish to call this function, include the header where the prototype is contained and add:
 
Now wherever you wish to call this function, include the header where the prototype is contained and add:

Revision as of 08:01, 27 July 2015

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. Also since neither Acceleration nor SP 2 are likely to be updated,
			//one can assume the offsets (0x14D0 for SP2 and 0x1530 for acceleration) will remain constant, thus leaving only P3D and FSX SE with offsets that vary.
			//P3D can be retrieved with the function name thus elliminating confusion of function ordinals.  }

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");

Complete example for automatic function selection:

GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"simscheduler.dll", &hsimsched);
if(hsimsched)
{
	DWORD fsxa = (DWORD)GetProcAddress(hsimsched, MAKEINTRESOURCEA(6));
	DWORD p3dv2 = (DWORD)GetProcAddress(hsimsched, "?GetElapsedSimTimeSec@@YGNXZ");
	if(fsxa-(DWORD)hsimsched == 0x1530)
		GetSimTime6 = (fGetSimTime)fsxa;//FSX Acceleration version
	else
	{
		if(p3dv2)
			GetSimTime6 = (fGetSimTime)p3dv2;
		else
		{
			GetSimTime6 = (fGetSimTime)GetProcAddress(hsimsched, MAKEINTRESOURCEA(4));//FSX SP2. 
			//Since neither Acceleration nor SP 2 are likely to be updated,
			//one can assume the offsets (0x14D0 for SP2 and 0x1530 for acceleration) will remain constant, thus leaving only P3D and FSX SE with offsets that vary.
			//P3D can be retrieved with the function name thus elliminating confusion of function ordinals. 
			if(DWORD(GetSimTime6)-DWORD(hsimsched) != 0x14D0)
				GetSimTime6 = (fGetSimTime)GetProcAddress(hsimsched, MAKEINTRESOURCEA(6));
		}
	}
}