Base.h 9.7 KB

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