Base.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #ifdef __ANDROID__
  41. #include <android/asset_manager.h>
  42. #endif
  43. // Common
  44. #ifndef NULL
  45. #define NULL 0
  46. #endif
  47. // Print logging (implemented per platform)
  48. namespace gameplay
  49. {
  50. extern void printError(const char* format, ...);
  51. }
  52. #ifdef __ANDROID__
  53. #include <android/log.h>
  54. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
  55. // System Errors
  56. #define LOG_ERROR(x) \
  57. { \
  58. LOGI(x); \
  59. assert(#x == 0); \
  60. }
  61. #define LOG_ERROR_VARG(x, ...) \
  62. { \
  63. LOGI(x, __VA_ARGS__); \
  64. assert(#x == 0); \
  65. }
  66. // Warning macro
  67. #ifdef WARN
  68. #undef WARN
  69. #endif
  70. #define WARN(x) LOGI(x)
  71. #define WARN_VARG(x, ...) LOGI(x, __VA_ARGS__)
  72. #else
  73. // System Errors
  74. #define LOG_ERROR(x) \
  75. { \
  76. printError(x); \
  77. assert(#x == 0); \
  78. }
  79. #define LOG_ERROR_VARG(x, ...) \
  80. { \
  81. printError(x, __VA_ARGS__); \
  82. assert(#x == 0); \
  83. }
  84. // Warning macro
  85. #ifdef WARN
  86. #undef WARN
  87. #endif
  88. #define WARN(x) printError(x)
  89. #define WARN_VARG(x, ...) printError(x, __VA_ARGS__)
  90. #endif
  91. // Bullet Physics
  92. #include <btBulletDynamicsCommon.h>
  93. #include <BulletCollision/CollisionDispatch/btGhostObject.h>
  94. #define BV(v) (btVector3((v).x, (v).y, (v).z))
  95. #define BQ(q) (btQuaternion((q).x, (q).y, (q).z, (q).w))
  96. // Debug new for memory leak detection
  97. #include "DebugNew.h"
  98. // Object deletion macro
  99. #define SAFE_DELETE(x) \
  100. { \
  101. delete x; \
  102. x = NULL; \
  103. }
  104. // Array deletion macro
  105. #define SAFE_DELETE_ARRAY(x) \
  106. { \
  107. delete[] x; \
  108. x = NULL; \
  109. }
  110. // Ref cleanup macro
  111. #define SAFE_RELEASE(x) \
  112. if (x) \
  113. { \
  114. x->release(); \
  115. x = NULL; \
  116. }
  117. // Math
  118. #define MATH_DEG_TO_RAD(x) ((x) * 0.0174532925f)
  119. #define MATH_RAD_TO_DEG(x) ((x)* 57.29577951f)
  120. #define MATH_RANDOM_MINUS1_1() ((2.0f*((float)rand()/RAND_MAX))-1.0f) // Returns a random float between -1 and 1.
  121. #define MATH_RANDOM_0_1() ((float)rand()/RAND_MAX) // Returns a random float between 0 and 1.
  122. #define MATH_FLOAT_SMALL 1.0e-37f
  123. #define MATH_TOLERANCE 2e-37f
  124. #define MATH_E 2.71828182845904523536f
  125. #define MATH_LOG10E 0.4342944819032518f
  126. #define MATH_LOG2E 1.442695040888963387f
  127. #define MATH_PI 3.14159265358979323846f
  128. #define MATH_PIOVER2 1.57079632679489661923f
  129. #define MATH_PIOVER4 0.785398163397448309616f
  130. #define MATH_PIX2 6.28318530717958647693f
  131. #define MATH_EPSILON 0.000001f
  132. #define MATH_CLAMP(x, lo, hi) ((x < lo) ? lo : ((x > hi) ? hi : x))
  133. #ifndef M_1_PI
  134. #define M_1_PI 0.31830988618379067154
  135. #endif
  136. #ifdef WIN32
  137. inline float round(float r)
  138. {
  139. return (r > 0.0f) ? floor(r + 0.5f) : ceil(r - 0.5f);
  140. }
  141. #endif
  142. // NOMINMAX makes sure that windef.h doesn't add macros min and max
  143. #ifdef WIN32
  144. #define NOMINMAX
  145. #endif
  146. // Audio (OpenAL, OpenSL, OggVorbis)
  147. #ifdef __ANDROID__
  148. #include <SLES/OpenSLES.h>
  149. #include <SLES/OpenSLES_Android.h>
  150. #else
  151. #ifdef __QNX__
  152. #include <AL/al.h>
  153. #include <AL/alc.h>
  154. #elif WIN32
  155. #include <al.h>
  156. #include <alc.h>
  157. #elif __APPLE__
  158. #include <OpenAL/al.h>
  159. #include <OpenAL/alc.h>
  160. #endif
  161. #include <vorbis/vorbisfile.h>
  162. #endif
  163. // Image
  164. #include <png.h>
  165. #define WINDOW_VSYNC 1
  166. #define WINDOW_FULLSCREEN 0
  167. // Graphics (OpenGL)
  168. #if defined (__QNX__) || defined(__ANDROID__)
  169. #include <EGL/egl.h>
  170. #include <GLES2/gl2.h>
  171. #include <GLES2/gl2ext.h>
  172. extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray;
  173. extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays;
  174. extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays;
  175. extern PFNGLISVERTEXARRAYOESPROC glIsVertexArray;
  176. #define glClearDepth glClearDepthf
  177. #define OPENGL_ES
  178. #define OPENGL_ES_PVR
  179. #elif WIN32
  180. #define WIN32_LEAN_AND_MEAN
  181. #define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  182. #define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  183. #define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  184. #define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  185. #include <GL/glew.h>
  186. #elif __APPLE__
  187. #include "TargetConditionals.h"
  188. #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
  189. #include <OpenGLES/ES2/gl.h>
  190. #include <OpenGLES/ES2/glext.h>
  191. #define glBindVertexArray glBindVertexArrayOES
  192. #define glDeleteVertexArrays glDeleteVertexArraysOES
  193. #define glGenVertexArrays glGenVertexArraysOES
  194. #define glIsVertexArray glIsVertexArrayOES
  195. #define glClearDepth glClearDepthf
  196. #define OPENGL_ES
  197. #define OPENGL_ES_PVR
  198. #elif TARGET_OS_MAC
  199. #include <OpenGL/gl.h>
  200. #include <OpenGL/glext.h>
  201. #define glBindVertexArray glBindVertexArrayAPPLE
  202. #define glDeleteVertexArrays glDeleteVertexArraysAPPLE
  203. #define glGenVertexArrays glGenVertexArraysAPPLE
  204. #define glIsVertexArray glIsVertexArrayAPPLE
  205. #else
  206. #error "Unsupported Apple Device"
  207. #endif
  208. #endif
  209. // Graphics (GLSL)
  210. #define VERTEX_ATTRIBUTE_POSITION_NAME "a_position"
  211. #define VERTEX_ATTRIBUTE_NORMAL_NAME "a_normal"
  212. #define VERTEX_ATTRIBUTE_COLOR_NAME "a_color"
  213. #define VERTEX_ATTRIBUTE_TANGENT_NAME "a_tangent"
  214. #define VERTEX_ATTRIBUTE_BINORMAL_NAME "a_binormal"
  215. #define VERTEX_ATTRIBUTE_BLENDWEIGHTS_NAME "a_blendWeights"
  216. #define VERTEX_ATTRIBUTE_BLENDINDICES_NAME "a_blendIndices"
  217. #define VERTEX_ATTRIBUTE_TEXCOORD_PREFIX "a_texCoord"
  218. // Hardware Resources
  219. namespace gameplay
  220. {
  221. typedef GLint VertexAttribute;
  222. typedef GLuint VertexBufferHandle;
  223. typedef GLuint IndexBufferHandle;
  224. typedef GLuint TextureHandle;
  225. typedef GLuint FrameBufferHandle;
  226. typedef GLuint RenderBufferHandle;
  227. }
  228. /**
  229. * GL assertion that can be used for any OpenGL function call.
  230. *
  231. * This macro will assert if an error is detected when executing
  232. * the specified GL code. This macro will do nothing in release
  233. * mode and is therefore safe to use for realtime/per-frame GL
  234. * function calls.
  235. */
  236. #ifdef NDEBUG
  237. #define GL_ASSERT( gl_code ) gl_code
  238. #else
  239. #define GL_ASSERT( gl_code ) \
  240. { \
  241. gl_code; \
  242. __gl_error_code = glGetError(); \
  243. if (__gl_error_code != GL_NO_ERROR) \
  244. { \
  245. LOG_ERROR_VARG(#gl_code ": %d", (int)__gl_error_code); \
  246. } \
  247. assert(__gl_error_code == GL_NO_ERROR); \
  248. }
  249. #endif
  250. /**
  251. * Executes the specified GL code and checks the GL error afterwards
  252. * to ensure it succeeded.
  253. *
  254. * This macro should be used instead of GL_ASSERT for code that must
  255. * be checked in both debug and release builds. The GL_LAST_ERROR
  256. * macro can be used afterwards to check whether a GL error was
  257. * encountered executing the specified code.
  258. */
  259. #define GL_CHECK( gl_code ) \
  260. { \
  261. while (glGetError() != GL_NO_ERROR) ; \
  262. gl_code; \
  263. __gl_error_code = glGetError(); \
  264. if (__gl_error_code != GL_NO_ERROR) \
  265. { \
  266. LOG_ERROR_VARG(#gl_code ": %d", (int)__gl_error_code); \
  267. } \
  268. }
  269. // Global variable to hold GL errors
  270. extern GLenum __gl_error_code;
  271. /**
  272. * Accesses the most recently set global GL error.
  273. */
  274. #define GL_LAST_ERROR() __gl_error_code
  275. #if defined(WIN32)
  276. #pragma warning( disable : 4172 )
  277. #pragma warning( disable : 4244 )
  278. #pragma warning( disable : 4311 )
  279. #pragma warning( disable : 4390 )
  280. #pragma warning( disable : 4800 )
  281. #pragma warning( disable : 4996 )
  282. #endif
  283. #ifdef __ANDROID__
  284. #include <android_native_app_glue.h>
  285. extern void amain(struct android_app* state);
  286. #endif
  287. // Assert has special behavior on Windows (for Visual Studio).
  288. #ifdef WIN32
  289. #ifdef assert
  290. #undef assert
  291. #endif
  292. #ifdef _DEBUG
  293. #define assert(expression) do { \
  294. if (!(expression)) \
  295. { \
  296. printError("Assertion \'" #expression "\' failed."); \
  297. __debugbreak(); \
  298. } } while (0)
  299. #else
  300. #define assert(expression) do { (void)sizeof(expression); } while (0)
  301. #endif
  302. #endif
  303. #endif