Base.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #ifndef BASE_H_
  2. #define BASE_H_
  3. // C/C++
  4. #include <new>
  5. #include <memory>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cassert>
  9. #include <cwchar>
  10. #include <cwctype>
  11. #include <cctype>
  12. #include <cmath>
  13. #include <cstdarg>
  14. #include <ctime>
  15. #include <iostream>
  16. #include <string>
  17. #include <vector>
  18. #include <list>
  19. #include <set>
  20. #include <stack>
  21. #include <map>
  22. #include <algorithm>
  23. #include <limits>
  24. #include <functional>
  25. // Bring common functions from C into global namespace
  26. using std::memcpy;
  27. using std::fabs;
  28. using std::sqrt;
  29. using std::cos;
  30. using std::sin;
  31. using std::tan;
  32. using std::isspace;
  33. using std::isdigit;
  34. using std::toupper;
  35. using std::tolower;
  36. using std::size_t;
  37. using std::min;
  38. using std::max;
  39. using std::modf;
  40. // Common
  41. #ifndef NULL
  42. #define NULL 0
  43. #endif
  44. // Print logging (implemented per platform)
  45. namespace gameplay
  46. {
  47. extern void printError(const char* format, ...);
  48. }
  49. // Current function macro.
  50. #ifdef WIN32
  51. #define __current__func__ __FUNCTION__
  52. #else
  53. #define __current__func__ __func__
  54. #endif
  55. // Assert macros.
  56. #ifdef _DEBUG
  57. #ifdef WIN32
  58. #define GP_FORCE_ASSERTION_FAILURE do { __debugbreak(); } while (0)
  59. #else
  60. #define GP_FORCE_ASSERTION_FAILURE do { assert(0); } while (0)
  61. #endif
  62. #define GP_ASSERT(expression) do { \
  63. if (!(expression)) \
  64. { \
  65. printError("%s -- Assertion '" #expression "' failed.\n", __current__func__); \
  66. GP_FORCE_ASSERTION_FAILURE; \
  67. } } while (0)
  68. #else
  69. #define GP_FORCE_ASSERTION_FAILURE do { (void)sizeof(int); } while (0)
  70. #define GP_ASSERT(expression) do { (void)sizeof(expression); } while (0)
  71. #endif
  72. // Error macro.
  73. #ifdef GP_ERRORS_AS_WARNINGS
  74. #define GP_ERROR GP_WARN
  75. #else
  76. #define GP_ERROR(...) do \
  77. { \
  78. printError("%s -- ", __current__func__); \
  79. printError(__VA_ARGS__); \
  80. printError("\n"); \
  81. GP_FORCE_ASSERTION_FAILURE; \
  82. std::exit(-1); \
  83. } while (0)
  84. #endif
  85. // Warning macro.
  86. #define GP_WARN(...) do \
  87. { \
  88. printError("%s -- ", __current__func__); \
  89. printError(__VA_ARGS__); \
  90. printError("\n"); \
  91. } while (0)
  92. // Bullet Physics
  93. #include <btBulletDynamicsCommon.h>
  94. #include <BulletCollision/CollisionDispatch/btGhostObject.h>
  95. #define BV(v) (btVector3((v).x, (v).y, (v).z))
  96. #define BQ(q) (btQuaternion((q).x, (q).y, (q).z, (q).w))
  97. // Debug new for memory leak detection
  98. #include "DebugNew.h"
  99. // Object deletion macro
  100. #define SAFE_DELETE(x) \
  101. { \
  102. delete x; \
  103. x = NULL; \
  104. }
  105. // Array deletion macro
  106. #define SAFE_DELETE_ARRAY(x) \
  107. { \
  108. delete[] x; \
  109. x = NULL; \
  110. }
  111. // Ref cleanup macro
  112. #define SAFE_RELEASE(x) \
  113. if (x) \
  114. { \
  115. x->release(); \
  116. x = NULL; \
  117. }
  118. // Math
  119. #define MATH_DEG_TO_RAD(x) ((x) * 0.0174532925f)
  120. #define MATH_RAD_TO_DEG(x) ((x)* 57.29577951f)
  121. #define MATH_RANDOM_MINUS1_1() ((2.0f*((float)rand()/RAND_MAX))-1.0f) // Returns a random float between -1 and 1.
  122. #define MATH_RANDOM_0_1() ((float)rand()/RAND_MAX) // Returns a random float between 0 and 1.
  123. #define MATH_FLOAT_SMALL 1.0e-37f
  124. #define MATH_TOLERANCE 2e-37f
  125. #define MATH_E 2.71828182845904523536f
  126. #define MATH_LOG10E 0.4342944819032518f
  127. #define MATH_LOG2E 1.442695040888963387f
  128. #define MATH_PI 3.14159265358979323846f
  129. #define MATH_PIOVER2 1.57079632679489661923f
  130. #define MATH_PIOVER4 0.785398163397448309616f
  131. #define MATH_PIX2 6.28318530717958647693f
  132. #define MATH_EPSILON 0.000001f
  133. #define MATH_CLAMP(x, lo, hi) ((x < lo) ? lo : ((x > hi) ? hi : x))
  134. #ifndef M_1_PI
  135. #define M_1_PI 0.31830988618379067154
  136. #endif
  137. #ifdef WIN32
  138. inline float round(float r)
  139. {
  140. return (r > 0.0f) ? floor(r + 0.5f) : ceil(r - 0.5f);
  141. }
  142. #endif
  143. // NOMINMAX makes sure that windef.h doesn't add macros min and max
  144. #ifdef WIN32
  145. #define NOMINMAX
  146. #endif
  147. // Audio (OpenAL/Vorbis)
  148. #if defined (__QNX__) || defined(__ANDROID__)
  149. #include <AL/al.h>
  150. #include <AL/alc.h>
  151. #elif WIN32
  152. #include <al.h>
  153. #include <alc.h>
  154. #elif __APPLE__
  155. #include <OpenAL/al.h>
  156. #include <OpenAL/alc.h>
  157. #endif
  158. #include <vorbis/vorbisfile.h>
  159. // Image
  160. #include <png.h>
  161. #define WINDOW_VSYNC 1
  162. // Graphics (OpenGL)
  163. #ifdef __QNX__
  164. #include <EGL/egl.h>
  165. #include <GLES2/gl2.h>
  166. #include <GLES2/gl2ext.h>
  167. extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray;
  168. extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays;
  169. extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays;
  170. extern PFNGLISVERTEXARRAYOESPROC glIsVertexArray;
  171. #define glClearDepth glClearDepthf
  172. #define OPENGL_ES
  173. #define USE_PVRTC
  174. #ifdef __ARM__
  175. #define USE_NEON
  176. #endif
  177. #elif __ANDROID__
  178. #include <EGL/egl.h>
  179. #include <GLES2/gl2.h>
  180. #include <GLES2/gl2ext.h>
  181. extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray;
  182. extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays;
  183. extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays;
  184. extern PFNGLISVERTEXARRAYOESPROC glIsVertexArray;
  185. #define glClearDepth glClearDepthf
  186. #define OPENGL_ES
  187. #elif WIN32
  188. #define WIN32_LEAN_AND_MEAN
  189. #include <GL/glew.h>
  190. #define USE_VAO
  191. #elif __APPLE__
  192. #include "TargetConditionals.h"
  193. #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
  194. #include <OpenGLES/ES2/gl.h>
  195. #include <OpenGLES/ES2/glext.h>
  196. #define glBindVertexArray glBindVertexArrayOES
  197. #define glDeleteVertexArrays glDeleteVertexArraysOES
  198. #define glGenVertexArrays glGenVertexArraysOES
  199. #define glIsVertexArray glIsVertexArrayOES
  200. #define glClearDepth glClearDepthf
  201. #define OPENGL_ES
  202. #define USE_VAO
  203. #elif TARGET_OS_MAC
  204. #include <OpenGL/gl.h>
  205. #include <OpenGL/glext.h>
  206. #define glBindVertexArray glBindVertexArrayAPPLE
  207. #define glDeleteVertexArrays glDeleteVertexArraysAPPLE
  208. #define glGenVertexArrays glGenVertexArraysAPPLE
  209. #define glIsVertexArray glIsVertexArrayAPPLE
  210. #define USE_VAO
  211. #else
  212. #error "Unsupported Apple Device"
  213. #endif
  214. #endif
  215. // Graphics (GLSL)
  216. #define VERTEX_ATTRIBUTE_POSITION_NAME "a_position"
  217. #define VERTEX_ATTRIBUTE_NORMAL_NAME "a_normal"
  218. #define VERTEX_ATTRIBUTE_COLOR_NAME "a_color"
  219. #define VERTEX_ATTRIBUTE_TANGENT_NAME "a_tangent"
  220. #define VERTEX_ATTRIBUTE_BINORMAL_NAME "a_binormal"
  221. #define VERTEX_ATTRIBUTE_BLENDWEIGHTS_NAME "a_blendWeights"
  222. #define VERTEX_ATTRIBUTE_BLENDINDICES_NAME "a_blendIndices"
  223. #define VERTEX_ATTRIBUTE_TEXCOORD_PREFIX_NAME "a_texCoord"
  224. // Hardware buffer
  225. namespace gameplay
  226. {
  227. typedef GLint VertexAttribute;
  228. typedef GLuint VertexBufferHandle;
  229. typedef GLuint IndexBufferHandle;
  230. typedef GLuint TextureHandle;
  231. typedef GLuint FrameBufferHandle;
  232. typedef GLuint RenderBufferHandle;
  233. }
  234. /**
  235. * GL assertion that can be used for any OpenGL function call.
  236. *
  237. * This macro will assert if an error is detected when executing
  238. * the specified GL code. This macro will do nothing in release
  239. * mode and is therefore safe to use for realtime/per-frame GL
  240. * function calls.
  241. */
  242. #ifdef NDEBUG
  243. #define GL_ASSERT( gl_code ) gl_code
  244. #else
  245. #define GL_ASSERT( gl_code ) do \
  246. { \
  247. gl_code; \
  248. __gl_error_code = glGetError(); \
  249. GP_ASSERT(__gl_error_code == GL_NO_ERROR); \
  250. } while(0)
  251. #endif
  252. /**
  253. * Executes the specified GL code and checks the GL error afterwards
  254. * to ensure it succeeded.
  255. *
  256. * This macro should be used instead of GL_ASSERT for code that must
  257. * be checked in both debug and release builds. The GL_LAST_ERROR
  258. * macro can be used afterwards to check whether a GL error was
  259. * encountered executing the specified code.
  260. */
  261. #define GL_CHECK( gl_code ) do \
  262. { \
  263. while (glGetError() != GL_NO_ERROR) ; \
  264. gl_code; \
  265. __gl_error_code = glGetError(); \
  266. if (__gl_error_code != GL_NO_ERROR) \
  267. { \
  268. GP_ERROR(#gl_code ": %d", (int)__gl_error_code); \
  269. } \
  270. } while(0)
  271. // Global variable to hold GL errors
  272. extern GLenum __gl_error_code;
  273. /**
  274. * Accesses the most recently set global GL error.
  275. */
  276. #define GL_LAST_ERROR() __gl_error_code
  277. /**
  278. * Executes the specified AL code and checks the AL error afterwards
  279. * to ensure it succeeded.
  280. *
  281. * The AL_LAST_ERROR macro can be used afterwards to check whether a AL error was
  282. * encountered executing the specified code.
  283. */
  284. #define AL_CHECK( al_code ) do \
  285. { \
  286. while (alGetError() != AL_NO_ERROR) ; \
  287. al_code; \
  288. __al_error_code = alGetError(); \
  289. if (__al_error_code != AL_NO_ERROR) \
  290. { \
  291. GP_ERROR(#al_code ": %d", (int)__al_error_code); \
  292. } \
  293. } while(0)
  294. // Global variable to hold AL errors
  295. extern ALenum __al_error_code;
  296. /**
  297. * Accesses the most recently set global AL error.
  298. */
  299. #define AL_LAST_ERROR() __al_error_code
  300. #if defined(WIN32)
  301. #pragma warning( disable : 4172 )
  302. #pragma warning( disable : 4244 )
  303. #pragma warning( disable : 4311 )
  304. #pragma warning( disable : 4390 )
  305. #pragma warning( disable : 4800 )
  306. #pragma warning( disable : 4996 )
  307. #endif
  308. #endif