Base.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Base.h
  3. */
  4. #ifndef BASE_H_
  5. #define BASE_H_
  6. // C/C++
  7. #include <cassert>
  8. #include <memory>
  9. #include <iostream>
  10. #include <string>
  11. #include <cwchar>
  12. #include <cwctype>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <vector>
  16. #include <list>
  17. #include <stack>
  18. #include <map>
  19. #include <hash_map>
  20. #include <algorithm>
  21. #include <ctime>
  22. #include <cstdio>
  23. #include <limits>
  24. #include <functional>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <time.h>
  31. #include <math.h>
  32. #include <cstdarg>
  33. // Common
  34. #ifndef NULL
  35. #define NULL 0
  36. #endif
  37. // Print logging (implemented per platform)
  38. namespace gameplay
  39. {
  40. extern void printError(const char* format, ...);
  41. }
  42. // System Errors
  43. #define LOG_ERROR(x) \
  44. { \
  45. printError(x); \
  46. assert(0); \
  47. }
  48. #define LOG_ERROR_VARG(x, ...) \
  49. { \
  50. printError(x, __VA_ARGS__); \
  51. assert(0); \
  52. }
  53. // Warning macro
  54. #ifdef WARN
  55. #undef WARN
  56. #endif
  57. #define WARN(x) printError(x)
  58. #define WARN_VARG(x, ...) printError(x, __VA_ARGS__)
  59. // Object deletion macro
  60. #define SAFE_DELETE(x) \
  61. if (x) \
  62. { \
  63. delete x; \
  64. x = NULL; \
  65. }
  66. // Array deletion macro
  67. #define SAFE_DELETE_ARRAY(x) \
  68. if (x) \
  69. { \
  70. delete[] x; \
  71. x = NULL; \
  72. }
  73. // Ref cleanup macro
  74. #define SAFE_RELEASE(x) \
  75. if (x) \
  76. { \
  77. x->release(); \
  78. x = NULL; \
  79. }
  80. // Math
  81. #define MATH_DEG_TO_RAD(x) ((x) * 0.0174532925f)
  82. #define MATH_RAD_TO_DEG(x) ((x)* 57.29577951f)
  83. #define MATH_RANDOM_MINUS1_1() ((2.0f*((float)rand()/RAND_MAX))-1.0f) // Returns a random float between -1 and 1.
  84. #define MATH_RANDOM_0_1() ((float)rand()/RAND_MAX) // Returns a random float between 0 and 1.
  85. #define MATH_FLOAT_SMALL 1.0e-37f
  86. #define MATH_TOLERANCE 2e-37f
  87. #define MATH_E 2.71828182845904523536f
  88. #define MATH_LOG10E 0.4342944819032518f
  89. #define MATH_LOG2E 1.442695040888963387f
  90. #define MATH_PI 3.14159265358979323846f
  91. #define MATH_PIOVER2 1.57079632679489661923f
  92. #define MATH_PIOVER4 M_PI_4
  93. #define MATH_PIX2 6.28318530717958647693f
  94. #define MATH_EPSILON 0.000001f
  95. #ifndef M_1_PI
  96. #define M_1_PI 0.31830988618379067154
  97. #endif
  98. // Audio (OpenAL/alut)
  99. #ifdef __QNX__
  100. #include <AL/al.h>
  101. #include <AL/alc.h>
  102. #include <AL/alut.h>
  103. #elif WIN32
  104. #include <al.h>
  105. #include <alc.h>
  106. #include <alut.h>
  107. #endif
  108. // Graphics (OpenGLES/OpenGL/png)
  109. #define WINDOW_VSYNC 1
  110. #define WINDOW_FULLSCREEN 0
  111. #define WINDOW_WIDTH 1024
  112. #define WINDOW_HEIGHT 600
  113. #ifdef __QNX__
  114. #include <EGL/egl.h>
  115. #include <GLES2/gl2.h>
  116. #include <GLES2/gl2ext.h>
  117. extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray;
  118. extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays;
  119. extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays;
  120. extern PFNGLISVERTEXARRAYOESPROC glIsVertexArray;
  121. #define glClearDepth glClearDepthf
  122. #elif WIN32
  123. #define WIN32_LEAN_AND_MEAN
  124. #include <GL/glew.h>
  125. #include <GL/wglew.h>
  126. #endif
  127. #include <png.h>
  128. // Attributes
  129. #define VERTEX_ATTRIBUTE_POSITION_NAME "a_position"
  130. #define VERTEX_ATTRIBUTE_NORMAL_NAME "a_normal"
  131. #define VERTEX_ATTRIBUTE_COLOR_NAME "a_color"
  132. #define VERTEX_ATTRIBUTE_TANGENT_NAME "a_tangent"
  133. #define VERTEX_ATTRIBUTE_BINORMAL_NAME "a_binormal"
  134. #define VERTEX_ATTRIBUTE_BLENDWEIGHTS_NAME "a_blendWeights"
  135. #define VERTEX_ATTRIBUTE_BLENDINDICES_NAME "a_blendIndices"
  136. #define VERTEX_ATTRIBUTE_TEXCOORD_PREFIX "a_texCoord"
  137. // Hardware Resources
  138. namespace gameplay
  139. {
  140. typedef GLint VertexAttribute;
  141. typedef GLuint VertexBuffer;
  142. typedef GLuint IndexBuffer;
  143. typedef GLuint TextureHandle;
  144. typedef GLuint FrameBufferHandle;
  145. typedef GLuint RenderBufferHandle;
  146. }
  147. /**
  148. * GL assertion that can be used for any OpenGL function call.
  149. *
  150. * This macro will assert if an error is detected when executing
  151. * the specified GL code. This macro will do nothing in release
  152. * mode and is therefore safe to use for realtime/per-frame GL
  153. * function calls.
  154. */
  155. #ifdef NDEBUG
  156. #define GL_ASSERT( gl_code ) gl_code
  157. #else
  158. #define GL_ASSERT( gl_code ) \
  159. { \
  160. gl_code; \
  161. __gl_error_code = glGetError(); \
  162. if (__gl_error_code != GL_NO_ERROR) \
  163. { \
  164. LOG_ERROR_VARG(#gl_code ": %d", (int)__gl_error_code); \
  165. } \
  166. assert(__gl_error_code == GL_NO_ERROR); \
  167. }
  168. #endif
  169. /**
  170. * Executes the specified GL code and checks the GL error afterwards
  171. * to ensure it succeeded.
  172. *
  173. * This macro should be used instead of GL_ASSERT for code that must
  174. * be checked in both debug and release builds. The GL_LAST_ERROR
  175. * macro can be used afterwards to check whether a GL error was
  176. * encountered executing the specified code.
  177. */
  178. #define GL_CHECK( gl_code ) \
  179. { \
  180. while (glGetError() != GL_NO_ERROR) ; \
  181. gl_code; \
  182. __gl_error_code = glGetError(); \
  183. if (__gl_error_code != GL_NO_ERROR) \
  184. { \
  185. LOG_ERROR_VARG(#gl_code ": %d", (int)__gl_error_code); \
  186. } \
  187. }
  188. // Global variable to hold GL errors
  189. extern GLenum __gl_error_code;
  190. /**
  191. * Accesses the most recently set global GL error.
  192. */
  193. #define GL_LAST_ERROR() __gl_error_code
  194. // Missing platform functionality and warnings.
  195. #ifdef WIN32
  196. inline float fminf(float a, float b)
  197. {
  198. return a < b ? a : b;
  199. }
  200. inline float fmin(float a, float b)
  201. {
  202. return a < b ? a : b;
  203. }
  204. inline float fmaxf(float a, float b)
  205. {
  206. return a > b ? a : b;
  207. }
  208. inline float fmax(float a, float b)
  209. {
  210. return a > b ? a : b;
  211. }
  212. #pragma warning( disable : 4172 )
  213. #pragma warning( disable : 4244 )
  214. #pragma warning( disable : 4311 )
  215. #pragma warning( disable : 4390 )
  216. #pragma warning( disable : 4800 )
  217. #pragma warning( disable : 4996 )
  218. #endif
  219. #endif