📄 platform.h
字号:
#define BigShort( val ) WordSwap( val )
#define BigWord( val ) WordSwap( val )
#define BigLong( val ) DWordSwap( val )
#define BigDWord( val ) DWordSwap( val )
#define BigFloat( val ) DWordSwap( val )
#define LittleShort( val ) ( val )
#define LittleWord( val ) ( val )
#define LittleLong( val ) ( val )
#define LittleDWord( val ) ( val )
#define LittleFloat( val ) ( val )
#elif defined(BIG_ENDIAN)
#define BigShort( val ) ( val )
#define BigWord( val ) ( val )
#define BigLong( val ) ( val )
#define BigDWord( val ) ( val )
#define BigFloat( val ) ( val )
#define LittleShort( val ) WordSwap( val )
#define LittleWord( val ) WordSwap( val )
#define LittleLong( val ) DWordSwap( val )
#define LittleDWord( val ) DWordSwap( val )
#define LittleFloat( val ) DWordSwap( val )
#else
// @Note (toml 05-02-02): this technique expects the compiler to
// optimize the expression and eliminate the other path. On any new
// platform/compiler this should be tested.
inline short BigShort( short val ) { int test = 1; return ( *(char *)&test == 1 ) ? WordSwap( val ) : val; }
inline uint16 BigWord( uint16 val ) { int test = 1; return ( *(char *)&test == 1 ) ? WordSwap( val ) : val; }
inline long BigLong( long val ) { int test = 1; return ( *(char *)&test == 1 ) ? DWordSwap( val ) : val; }
inline uint32 BigDWord( uint32 val ) { int test = 1; return ( *(char *)&test == 1 ) ? DWordSwap( val ) : val; }
inline float BigFloat( float val ) { int test = 1; return ( *(char *)&test == 1 ) ? DWordSwap( val ) : val; }
inline short LittleShort( short val ) { int test = 1; return ( *(char *)&test == 1 ) ? val : WordSwap( val ); }
inline uint16 LittleWord( uint16 val ) { int test = 1; return ( *(char *)&test == 1 ) ? val : WordSwap( val ); }
inline long LittleLong( long val ) { int test = 1; return ( *(char *)&test == 1 ) ? val : DWordSwap( val ); }
inline uint32 LittleDWord( uint32 val ) { int test = 1; return ( *(char *)&test == 1 ) ? val : DWordSwap( val ); }
inline float LittleFloat( float val ) { int test = 1; return ( *(char *)&test == 1 ) ? val : DWordSwap( val ); }
#endif
#ifdef TIER0_DLL_EXPORT
#define PLATFORM_INTERFACE DLL_EXPORT
#define PLATFORM_OVERLOAD DLL_GLOBAL_EXPORT
#else
#define PLATFORM_INTERFACE DLL_IMPORT
#define PLATFORM_OVERLOAD DLL_GLOBAL_IMPORT
#endif
PLATFORM_INTERFACE double Plat_FloatTime(); // Returns time in seconds since the module was loaded.
PLATFORM_INTERFACE unsigned long Plat_MSTime(); // Time in milliseconds.
// b/w compatibility
#define Sys_FloatTime Plat_FloatTime
// Processor Information:
struct CPUInformation
{
int m_Size; // Size of this structure, for forward compatability.
bool m_bRDTSC : 1, // Is RDTSC supported?
m_bCMOV : 1, // Is CMOV supported?
m_bFCMOV : 1, // Is FCMOV supported?
m_bSSE : 1, // Is SSE supported?
m_bSSE2 : 1, // Is SSE2 Supported?
m_b3DNow : 1, // Is 3DNow! Supported?
m_bMMX : 1, // Is MMX supported?
m_bHT : 1; // Is HyperThreading supported?
unsigned char m_nLogicalProcessors, // Number op logical processors.
m_nPhysicalProcessors; // Number of physical processors
int64 m_Speed; // In cycles per second.
char* m_szProcessorID; // Processor vendor Identification.
};
PLATFORM_INTERFACE const CPUInformation& GetCPUInformation();
// ---------------------------------------------------------------------------------- //
// Memory allocation.
// ---------------------------------------------------------------------------------- //
typedef void (*Plat_AllocErrorFn)( unsigned long size );
// Use these to allocate and free memory.
PLATFORM_INTERFACE void* Plat_Alloc( unsigned long size ); // Allocate some memory.
PLATFORM_INTERFACE void* Plat_Realloc( void *ptr, unsigned long size ); // Rellocate some memory. It may or may not return the same
// block of memory.
PLATFORM_INTERFACE void Plat_Free( void *ptr ); // Free some memory.
// Use this to hook allocation errors.
PLATFORM_INTERFACE void Plat_SetAllocErrorFn( Plat_AllocErrorFn fn );
//-----------------------------------------------------------------------------
// Thread related functions
//-----------------------------------------------------------------------------
// Registers the current thread with Tier0's thread management system.
// This should be called on every thread created in the game.
PLATFORM_INTERFACE unsigned long Plat_RegisterThread( const char *pName = "Source Thread");
// Registers the current thread as the primary thread.
PLATFORM_INTERFACE unsigned long Plat_RegisterPrimaryThread();
// VC-specific. Sets the thread's name so it has a friendly name in the debugger.
// This should generally only be handled by Plat_RegisterThread and Plat_RegisterPrimaryThread
PLATFORM_INTERFACE void Plat_SetThreadName( unsigned long dwThreadID, const char *pName );
// These would be private if it were possible to export private variables from a .DLL.
// They need to be variables because they are checked by inline functions at performance
// critical places.
PLATFORM_INTERFACE unsigned long Plat_PrimaryThreadID;
// Returns the ID of the currently executing thread.
PLATFORM_INTERFACE unsigned long Plat_GetCurrentThreadID();
// Returns the ID of the primary thread.
inline unsigned long Plat_GetPrimaryThreadID()
{
return Plat_PrimaryThreadID;
}
// Returns true if the current thread is the primary thread.
inline bool Plat_IsPrimaryThread()
{
//return true;
return (Plat_GetPrimaryThreadID() == Plat_GetCurrentThreadID() );
}
//-----------------------------------------------------------------------------
// Security related functions
//-----------------------------------------------------------------------------
// Ensure that the hardware key's drivers have been installed.
PLATFORM_INTERFACE bool Plat_VerifyHardwareKeyDriver();
// Ok, so this isn't a very secure way to verify the hardware key for now. It
// is primarially depending on the fact that all the binaries have been wrapped
// with the secure wrapper provided by the hardware keys vendor.
PLATFORM_INTERFACE bool Plat_VerifyHardwareKey();
// The same as above, but notifies user with a message box when the key isn't in
// and gives him an opportunity to correct the situation.
PLATFORM_INTERFACE bool Plat_VerifyHardwareKeyPrompt();
// Can be called in real time, doesn't perform the verify every frame. Mainly just
// here to allow the game to drop out quickly when the key is removed, rather than
// allowing the wrapper to pop up it's own blocking dialog, which the engine doesn't
// like much.
PLATFORM_INTERFACE bool Plat_FastVerifyHardwareKey();
//-----------------------------------------------------------------------------
// Include additional dependant header components.
//-----------------------------------------------------------------------------
#include "tier0/fasttimer.h"
//-----------------------------------------------------------------------------
// Just logs file and line to simple.log
//-----------------------------------------------------------------------------
PLATFORM_INTERFACE void* Plat_SimpleLog( const char* file, int line );
//#define Plat_dynamic_cast Plat_SimpleLog(__FILE__,__LINE__),dynamic_cast
//-----------------------------------------------------------------------------
// Methods to invoke the constructor, copy constructor, and destructor
//-----------------------------------------------------------------------------
template <class T>
inline void Construct( T* pMemory )
{
new( pMemory ) T;
}
template <class T>
inline void CopyConstruct( T* pMemory, T const& src )
{
new( pMemory ) T(src);
}
template <class T>
inline void Destruct( T* pMemory )
{
pMemory->~T();
#ifdef _DEBUG
memset( pMemory, 0xDD, sizeof(T) );
#endif
}
//
// GET_OUTER()
//
// A platform-independent way for a contained class to get a pointer to its
// owner. If you know a class is exclusively used in the context of some
// "outer" class, this is a much more space efficient way to get at the outer
// class than having the inner class store a pointer to it.
//
// class COuter
// {
// class CInner // Note: this does not need to be a nested class to work
// {
// void PrintAddressOfOuter()
// {
// printf( "Outer is at 0x%x\n", GET_OUTER( COuter, m_Inner ) );
// }
// };
//
// CInner m_Inner;
// friend class CInner;
// };
#define GET_OUTER( OuterType, OuterMember ) \
( ( OuterType * ) ( (char *)this - offsetof( OuterType, OuterMember ) ) )
/* TEMPLATE_FUNCTION_TABLE()
(Note added to platform.h so platforms that correctly support templated
functions can handle portions as templated functions rather than wrapped
functions)
Helps automate the process of creating an array of function
templates that are all specialized by a single integer.
This sort of thing is often useful in optimization work.
For example, using TEMPLATE_FUNCTION_TABLE, this:
TEMPLATE_FUNCTION_TABLE(int, Function, ( int blah, int blah ), 10)
{
return argument * argument;
}
is equivilent to the following:
(NOTE: the function has to be wrapped in a class due to code
generation bugs involved with directly specializing a function
based on a constant.)
template<int argument>
class FunctionWrapper
{
public:
int Function( int blah, int blah )
{
return argument*argument;
}
}
typedef int (*FunctionType)( int blah, int blah );
class FunctionName
{
public:
enum { count = 10 };
FunctionType functions[10];
};
FunctionType FunctionName::functions[] =
{
FunctionWrapper<0>::Function,
FunctionWrapper<1>::Function,
FunctionWrapper<2>::Function,
FunctionWrapper<3>::Function,
FunctionWrapper<4>::Function,
FunctionWrapper<5>::Function,
FunctionWrapper<6>::Function,
FunctionWrapper<7>::Function,
FunctionWrapper<8>::Function,
FunctionWrapper<9>::Function
};
*/
PLATFORM_INTERFACE bool vtune( bool resume );
#define TEMPLATE_FUNCTION_TABLE(RETURN_TYPE, NAME, ARGS, COUNT) \
\
typedef RETURN_TYPE (FASTCALL *__Type_##NAME) ARGS; \
\
template<const int nArgument> \
struct __Function_##NAME \
{ \
static RETURN_TYPE FASTCALL Run ARGS; \
}; \
\
template <int i> \
struct __MetaLooper_##NAME : __MetaLooper_##NAME<i-1> \
{ \
__Type_##NAME func; \
inline __MetaLooper_##NAME() { func = __Function_##NAME<i>::Run; } \
}; \
\
template<> \
struct __MetaLooper_##NAME<0> \
{ \
__Type_##NAME func; \
inline __MetaLooper_##NAME() { func = __Function_##NAME<0>::Run; } \
}; \
\
class NAME \
{ \
private: \
static const __MetaLooper_##NAME<COUNT> m; \
public: \
enum { count = COUNT }; \
static const __Type_##NAME* functions; \
}; \
const __MetaLooper_##NAME<COUNT> NAME::m; \
const __Type_##NAME* NAME::functions = (__Type_##NAME*)&m; \
template<int nArgument> \
RETURN_TYPE FASTCALL __Function_##NAME<nArgument>::Run ARGS
#define LOOP_INTERCHANGE(BOOLEAN, CODE)\
if( (BOOLEAN) )\
{\
CODE;\
} else\
{\
CODE;\
}
#endif /* PLATFORM_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -