Base.h 9.8 KB

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