defines.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /******************************************************************************/
  2. // DEFINITIONS
  3. /******************************************************************************/
  4. #undef NULL
  5. #define NULL 0
  6. #define C const
  7. #define T (*this)
  8. #define null nullptr
  9. #define null_t std::nullptr_t
  10. #if WINDOWS
  11. #define super __super
  12. #endif
  13. /******************************************************************************/
  14. // TEMPLATE MACROS
  15. /******************************************************************************/
  16. #define T1(a ) template<typename a > // 1 type template
  17. #define T2(a, b ) template<typename a, typename b > // 2 types template
  18. #define T3(a, b, c) template<typename a, typename b, typename c> // 3 types template
  19. #if EE_PRIVATE
  20. #define T4(a, b, c, d ) template<typename a, typename b, typename c, typename d > // 4 types template
  21. #define T5(a, b, c, d, e ) template<typename a, typename b, typename c, typename d, typename e > // 5 types template
  22. #define T6(a, b, c, d, e, f ) template<typename a, typename b, typename c, typename d, typename e, typename f > // 6 types template
  23. #define T7(a, b, c, d, e, f, g ) template<typename a, typename b, typename c, typename d, typename e, typename f, typename g > // 7 types template
  24. #define T8(a, b, c, d, e, f, g, h ) template<typename a, typename b, typename c, typename d, typename e, typename f, typename g, typename h > // 8 types template
  25. #define T9(a, b, c, d, e, f, g, h, i) template<typename a, typename b, typename c, typename d, typename e, typename f, typename g, typename h, typename i> // 9 types template
  26. #endif
  27. /******************************************************************************/
  28. // HELPER MACROS
  29. /******************************************************************************/
  30. #define SIZE sizeof // get raw size of C++ element in bytes
  31. #define MEMBER( Class, member) (((Class*)null)-> member) // null based Class::member, this macro is used to obtain member information by many other macros/functions
  32. #define OFFSET( Class, member) UIntPtr(&MEMBER(Class, member)) // get offset of member in class
  33. #define MEMBER_SIZE(Class, member) SIZE( MEMBER(Class, member)) // get size of member in class
  34. #define MEMBER_ELMS(Class, member) Elms( MEMBER(Class, member)) // get elements of member in class
  35. #define CAST( Class, object) dynamic_cast<Class*>(object) // perform a dynamic cast of 'object' to 'Class' class
  36. #define SCAST( Class, object) static_cast<Class&>(object) // perform a static cast of 'object' to 'Class' class
  37. #define ELMS( Array ) (SIZE(Array)/SIZE(Array[0])) // get number of elements in array (this is the compile-time version, use 'Elms' instead of 'ELMS' whenever possible)
  38. T1(TYPE) TYPE& ConstCast(C TYPE &x) {return const_cast<TYPE&>(x);} // remove the const modifier
  39. T1(TYPE) TYPE* ConstCast(C TYPE *x) {return const_cast<TYPE*>(x);} // remove the const modifier
  40. T1(TYPE) TYPE& NoTemp( TYPE &x) {return x;}
  41. T1(TYPE) C TYPE& NoTemp(C TYPE &x) {return x;}
  42. T1(TYPE) TYPE&& RValue(TYPE &type) {return (TYPE&&)type;}
  43. /******************************************************************************/
  44. // ITERATION MACROS
  45. /******************************************************************************/
  46. #define REP( n) for(Int i=(n); --i>= 0 ; ) // repeat : n-1 .. 0
  47. #define REPD(i, n) for(Int i=(n); --i>= 0 ; ) // repeat with definition: n-1 .. 0
  48. #define FREP( n) for(Int i= 0 ; i< (n); i++) // forward repeat : 0 .. n-1
  49. #define FREPD(i, n) for(Int i= 0 ; i< (n); i++) // forward repeat with definition: 0 .. n-1
  50. #define REPA( a) for(Int i=Elms(a); --i>= 0 ; ) // repeat all : Elms(a)-1 .. 0
  51. #define REPAD(i, a) for(Int i=Elms(a); --i>= 0 ; ) // repeat all with definition: Elms(a)-1 .. 0
  52. #define FREPA( a) for(Int i= 0 ; i< Elms(a); i++) // forward repeat all : 0 .. Elms(a)-1
  53. #define FREPAD(i, a) for(Int i= 0 ; i< Elms(a); i++) // forward repeat all with definition: 0 .. Elms(a)-1
  54. #define REPAO( a) for(Int i=Elms(a); --i>= 0 ; ) (a)[i] // repeat all and operate: Elms(a)-1 .. 0
  55. #define REPAOD(i, a) for(Int i=Elms(a); --i>= 0 ; ) (a)[i] // repeat all with definition and operate: Elms(a)-1 .. 0
  56. #define FREPAO( a) for(Int i= 0 ; i< Elms(a); i++) (a)[i] // forward repeat all and operate: 0 .. Elms(a)-1
  57. #define FREPAOD(i, a) for(Int i= 0 ; i< Elms(a); i++) (a)[i] // forward repeat all with definition and operate: 0 .. Elms(a)-1
  58. #define REPS( i, n) for((i)= (n); --(i)>= 0 ; ) // repeat with i specified: n -1 .. 0
  59. #define FREPS( i, n) for((i)= 0 ; (i)< (n); (i)++) // forward repeat with i specified: 0 .. n-1
  60. #define REPAS(i, a) for((i)=Elms(a); --(i)>= 0 ; ) // repeat all with i specified: Elms(a)-1 .. 0
  61. #if EE_PRIVATE
  62. #define REPP(n) for(IntPtr i=(n); --i>=0; ) // repeat: n-1 .. 0
  63. #endif
  64. /******************************************************************************/
  65. // ENUM MACROS
  66. /******************************************************************************/
  67. #define ENABLE_IF_ENUM(ENUM, RESULT) typename std::enable_if< std::is_enum<ENUM>::value, RESULT>::type
  68. #define DISABLE_IF_ENUM(ENUM, RESULT) typename std::enable_if<!std::is_enum<ENUM>::value, RESULT>::type
  69. T1(TYPE) constexpr ENABLE_IF_ENUM(TYPE, Int) operator+ (Bool a, TYPE b) {return a+Int(b);}
  70. T1(TYPE) constexpr ENABLE_IF_ENUM(TYPE, Int) operator+ (Int a, TYPE b) {return a+Int(b);}
  71. T1(TYPE) constexpr ENABLE_IF_ENUM(TYPE, Flt) operator+ (Flt a, TYPE b) {return a+Int(b);}
  72. T1(TYPE) constexpr ENABLE_IF_ENUM(TYPE, Int) operator+ (TYPE a, Bool b) {return Int(a)+b;}
  73. T1(TYPE) constexpr ENABLE_IF_ENUM(TYPE, Int) operator+ (TYPE a, Int b) {return Int(a)+b;}
  74. T1(TYPE) constexpr ENABLE_IF_ENUM(TYPE, Flt) operator+ (TYPE a, Flt b) {return Int(a)+b;}
  75. T2(ENUM0, ENUM1) constexpr typename std::enable_if< std::is_enum<ENUM0>::value && std::is_enum<ENUM1>::value, Int>::type operator+ (ENUM0 a, ENUM1 b) {return Int(a)+Int(b);} // ENUM0+ENUM1
  76. /******************************************************************************/
  77. // ASSERTIONS
  78. /******************************************************************************/
  79. #define ASSERT_CONCAT2(a, b) a##b // don't use this
  80. #define ASSERT_CONCAT( a, b) ASSERT_CONCAT2(a, b) // don't use this
  81. #define ASSERT(value ) typedef Int ASSERT_CONCAT(_AssertDummyName, __LINE__)[(value) ? 1 : -1] // compile time assertion, alternative to static_assert(value, "assert failed"); which is more flexible on Clang/GCC
  82. #define DYNAMIC_ASSERT(value, error ) {if(!(value))Exit(S+(error)+"\nFile: \""+__FILE__+"\"\nLine: "+__LINE__);} // dynamic assertion
  83. #if DEBUG
  84. #define DEBUG_ASSERT(value, error ) DYNAMIC_ASSERT(value, error) // debug assertion available only in debug mode
  85. #else
  86. #define DEBUG_ASSERT(value, error ) {} // debug assertion unavailable in release mode
  87. #endif
  88. #define RANGE_ASSERT(index, elms ) DEBUG_ASSERT(InRange(index, elms), "Element out of range") // out of range assertion, asserts that 'index' is in range "0..elms-1"
  89. #define ALIGN_ASSERT(Class, member) ASSERT(!(OFFSET(Class, member)&(SIZE(Ptr)-1))) // assert that class member has alignment native to the target platform
  90. ASSERT(SIZE(Bool)==1); // size of Bool must be 1 byte
  91. ASSERT(SIZE(Char)==2); // size of Char must be 2 bytes
  92. /******************************************************************************/
  93. // STRUCT DECLARATION
  94. /******************************************************************************/
  95. #define const_mem_addr // custom keyword specifying that the struct/class must be stored in constant memory address, if you see this keyword next to a struct/class declaration you must ensure that when defining objects of that struct/class you will store them in constant memory address (this can be either global namespace or inside 'Memx' 'Meml' containers)
  96. #define STRUCT( Extended, Base ) struct Extended : Base { PLATFORM(, typedef Base super;) // macro for declaring an 'Extended' class from 'Base' , and declaring 'super' keyword at the same time (this is needed for non Windows platforms which don't declare 'super' automatically)
  97. #define STRUCT_PRIVATE(Extended, Base ) struct Extended : private Base { PLATFORM(, typedef Base super;) // macro for declaring an 'Extended' class from 'Base' , and declaring 'super' keyword at the same time (this is needed for non Windows platforms which don't declare 'super' automatically)
  98. #define STRUCT2( Extended, Base, Base1) struct Extended : Base, Base1 { PLATFORM(, typedef Base super;) // macro for declaring an 'Extended' class from 'Base' and 'Base1', and declaring 'super' keyword at the same time (this is needed for non Windows platforms which don't declare 'super' automatically)
  99. #define NO_COPY_CONSTRUCTOR(Class) \
  100. void operator=(C Class &src)=delete; \
  101. Class(C Class &src)=delete; // when declared inside a class this macro disables the use of copy constructors
  102. // alignment
  103. #undef ALIGN
  104. #define ALIGN( x) PLATFORM(__declspec(align(x)), )
  105. #define ALIGN_END(x) PLATFORM(, __attribute__((aligned(x))))
  106. /******************************************************************************/
  107. // FUNCTION DECLARATION
  108. /******************************************************************************/
  109. #if WINDOWS
  110. #define INLINE __forceinline // force inlining, this is stronger than 'inline'
  111. #define NOINLINE __declspec(noinline) // disable inlining
  112. #else
  113. #define INLINE inline __attribute__((always_inline)) // force inlining, this is stronger than 'inline'
  114. #define NOINLINE __attribute__(( noinline)) // disable inlining
  115. #endif
  116. /******************************************************************************/
  117. // CONFIGURATION
  118. /******************************************************************************/
  119. // Rendering
  120. #define SUPPORT_EARLY_Z (!MOBILE) // disable on Mobile because it's discouraged by PowerVR, Mali, ..
  121. #define SUPPORT_MATERIAL_AMBIENT 1
  122. #define COUNT_MATERIAL_USAGE 0 // never use "DEBUG" here, because it affects Material class size/members, which needs to remain constant
  123. #define CACHE_DX9_CONSTANTS 0 // enabling didn't make any performance difference, so disable to reduce memory usage
  124. #define SUPPORT_MATERIAL_CHANGE_IN_RENDERING 0
  125. #define SUPPORT_MLAA 0
  126. // Compression
  127. #define SUPPORT_RLE (!WEB)
  128. #define SUPPORT_SNAPPY (!WEB)
  129. #define SUPPORT_LZ4 1
  130. #define SUPPORT_ZLIB (!WEB)
  131. #define SUPPORT_ZSTD 1
  132. #define SUPPORT_LZHAM 1
  133. #define SUPPORT_LZMA 1
  134. // Audio
  135. #define SUPPORT_FLAC 1
  136. #define SUPPORT_VORBIS 1
  137. #define SUPPORT_VORBIS_ENC 1 // isn't going to be linked unless used
  138. #define SUPPORT_OPUS 1
  139. #define SUPPORT_OPUS_ENC 1
  140. #define SUPPORT_MP3 1
  141. // Image
  142. #define SUPPORT_JPG 1
  143. #define SUPPORT_PNG 1
  144. #define SUPPORT_PSD (!WEB)
  145. #define SUPPORT_TIF (!WEB)
  146. #define SUPPORT_WEBP 1
  147. // Video
  148. #define SUPPORT_THEORA 1
  149. #define SUPPORT_VP (WINDOWS || MAC || LINUX || ANDROID || (IOS && !IOS_SIMULATOR))
  150. // Database
  151. #define SUPPORT_SQLITE 1 // isn't going to be linked unless used
  152. #define SUPPORT_ODBC (DESKTOP && !WINDOWS_NEW)
  153. /******************************************************************************/
  154. #if EE_PRIVATE
  155. #define MAX_LONG_PATH 1024
  156. #define MAX_UTF_PATH 2048
  157. #define SIZEI(x) Int(SIZE(x)) // get size of element
  158. #define SIZEU(x) UInt(SIZE(x)) // get size of element
  159. #if LINUX
  160. #define FIND_ATOM(x) x=XInternAtom(XDisplay, #x, true ) // null on fail
  161. #define GET_ATOM(x) x=XInternAtom(XDisplay, #x, false) // New on fail
  162. #endif
  163. T1(TYPE ) TYPE& DTOR(TYPE &elm ) { elm.~TYPE( ); return elm;} // destructor
  164. T1(TYPE ) TYPE& CTOR(TYPE &elm ) {new(&elm) TYPE ; return elm;} // constructor
  165. T2(TA,TB) TA & CTOR(TA &elm, TB &param) {new(&elm) TA (param); return elm;} // constructor with a parameter
  166. T2(TA,TB) TA & CTOR(TA &elm, C TB &param) {new(&elm) TA (param); return elm;} // constructor with a parameter
  167. T1(TYPE) Bool OK (TYPE x) {return x>=0;}
  168. T1(TYPE) void RELEASE(TYPE* &x) {if(x){x->Release(); x=null;}}
  169. #define IS_POW_2(x) (!( (x) & ((x)-1) ))
  170. #endif
  171. /******************************************************************************/