-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathBase.h
295 lines (266 loc) · 7.32 KB
/
Base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#ifndef BASE_H_
#define BASE_H_
// C/C++
#include <new>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cwchar>
#include <cwctype>
#include <cctype>
#include <cmath>
#include <cstdarg>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <map>
#include <algorithm>
#include <limits>
#include <functional>
#include "ccConfig.h"
#include "CCGL.h"
#define C3D_Max(a,b) (((a) > (b)) ? (a) : (b))
#define C3D_Min(a,b) (((a) < (b)) ? (a) : (b))
// Common
#ifndef NULL
#define NULL 0
#endif
#ifndef FLT_EPSILON
#define FLT_EPSILON 1.192092896e-07F
#endif
// Print logging (implemented per platform)
namespace cocos3d
{
extern void printError(const char* format, ...);
}
#ifdef __ANDROID__
#include <android/log.h>
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
// System Errors
#define LOG_ERROR(x) \
{ \
LOGI(x); \
}
#define LOG_ERROR_VARG(x, ...) \
{ \
LOGI(x, __VA_ARGS__); \
}
#define LOG_TRACE(x)
#define LOG_TRACE_VARG(x, ...)
// Warning macro
#ifdef WARN
#undef WARN
#endif
#define WARN(x) LOGI(x)
#define WARN_VARG(x, ...) LOGI(x, __VA_ARGS__)
#else
// System Errors
#define LOG_ERROR(x) \
{ \
printError(x); \
assert(#x == 0); \
}
#define LOG_ERROR_VARG(x, ...) \
{ \
printError(x, __VA_ARGS__); \
assert(#x == 0); \
}
#define LOG_TRACE(x) \
{ \
printError(x); \
}
#define LOG_TRACE_VARG(x, ...) \
{ \
printError(x, __VA_ARGS__); \
}
// Warning macro
#ifdef WARN
#undef WARN
#endif
#define WARN(x) printError(x)
#define WARN_VARG(x, ...) printError(x, __VA_ARGS__)
#endif
// Object deletion macro
#define SAFE_DELETE(x) \
{ \
delete x; \
x = NULL; \
}
// Array deletion macro
#define SAFE_DELETE_ARRAY(x) \
{ \
delete[] x; \
x = NULL; \
}
// Ref cleanup macro
#define SAFE_RELEASE(x) \
if (x) \
{ \
x->release(); \
x = NULL; \
}
// Math
#define MATH_DEG_TO_RAD(x) ((x) * 0.0174532925f)
#define MATH_RAD_TO_DEG(x) ((x)* 57.29577951f)
#define MATH_RANDOM_MINUS1_1() ((2.0f*((float)rand()/RAND_MAX))-1.0f) // Returns a random float between -1 and 1.
#define MATH_RANDOM_0_1() ((float)rand()/RAND_MAX) // Returns a random float between 0 and 1.
#define MATH_FLOAT_SMALL 1.0e-37f
#define MATH_TOLERANCE 2e-37f
#define MATH_E 2.71828182845904523536f
#define MATH_LOG10E 0.4342944819032518f
#define MATH_LOG2E 1.442695040888963387f
#define MATH_PI 3.14159265358979323846f
#define MATH_PIOVER2 1.57079632679489661923f
#define MATH_PIOVER4 0.785398163397448309616f
#define MATH_PIX2 6.28318530717958647693f
#define MATH_EPSILON 0.000001f
#define MATH_CLAMP(x, lo, hi) ((x < lo) ? lo : ((x > hi) ? hi : x))
//#ifndef M_1_PI
//#define M_1_PI 0.31830988618379067154
//#endif
#ifdef WIN32
inline float round(float r)
{
return (r > 0.0f) ? floor(r + 0.5f) : ceil(r - 0.5f);
}
#endif
// NOMINMAX makes sure that windef.h doesn't add macros min and max
#ifdef WIN32
#define NOMINMAX
#endif
#define WINDOW_VSYNC 1
#define WINDOW_FULLSCREEN 0
// Graphics (OpenGL)
#if defined(__ANDROID__)
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray;
extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays;
extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays;
extern PFNGLISVERTEXARRAYOESPROC glIsVertexArray;
#define glClearDepth glClearDepthf
#define OPENGL_ES
#define USE_PVRTC
// todo reedhong:
// #define USE_VAO
#elif WIN32
#define WIN32_LEAN_AND_MEAN
#include <GL/glew.h>
//#define USE_VAO
#elif __APPLE__
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#define glBindVertexArray glBindVertexArrayOES
#define glDeleteVertexArrays glDeleteVertexArraysOES
#define glGenVertexArrays glGenVertexArraysOES
#define glIsVertexArray glIsVertexArrayOES
#define glClearDepth glClearDepthf
#define OPENGL_ES
#define USE_PVRTC
// #define USE_VAO
#elif TARGET_OS_MAC
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#define glBindVertexArray glBindVertexArrayAPPLE
#define glDeleteVertexArrays glDeleteVertexArraysAPPLE
#define glGenVertexArrays glGenVertexArraysAPPLE
#define glIsVertexArray glIsVertexArrayAPPLE
// #define USE_VAO
#else
#error "Unsupported Apple Device"
#endif
#endif
// Hardware buffer
namespace cocos3d
{
typedef GLint VertexAttribute;
typedef GLuint VertexBufferHandle;
typedef GLuint IndexBufferHandle;
typedef GLuint TextureHandle;
typedef GLuint FrameBufferHandle;
typedef GLuint RenderBufferHandle;
}
/**
* GL assertion that can be used for any OpenGL function call.
*
* This macro will assert if an error is detected when executing
* the specified GL code. This macro will do nothing in release
* mode and is therefore safe to use for realtime/per-frame GL
* function calls.
*/
#ifdef NDEBUG
#define GL_ASSERT( gl_code ) gl_code
#else
#define GL_ASSERT( gl_code ) \
{ \
gl_code; \
__gl_error_code = glGetError(); \
if (__gl_error_code != GL_NO_ERROR) \
{ \
LOG_ERROR_VARG(#gl_code ": 0x%x", (int)__gl_error_code); \
} \
assert(__gl_error_code == GL_NO_ERROR); \
}
#endif
/**
* Executes the specified GL code and checks the GL error afterwards
* to ensure it succeeded.
*
* This macro should be used instead of GL_ASSERT for code that must
* be checked in both debug and release builds. The GL_LAST_ERROR
* macro can be used afterwards to check whether a GL error was
* encountered executing the specified code.
*/
#define GL_CHECK( gl_code ) \
{ \
while (glGetError() != GL_NO_ERROR) ; \
gl_code; \
__gl_error_code = glGetError(); \
if (__gl_error_code != GL_NO_ERROR) \
{ \
LOG_ERROR_VARG(#gl_code ": %d", (int)__gl_error_code); \
} \
}
// Global variable to hold GL errors
extern GLenum __gl_error_code;
/**
* Accesses the most recently set global GL error.
*/
#define GL_LAST_ERROR() __gl_error_code
#if defined(WIN32)
#pragma warning( disable : 4172 )
#pragma warning( disable : 4244 )
#pragma warning( disable : 4311 )
#pragma warning( disable : 4390 )
#pragma warning( disable : 4800 )
#pragma warning( disable : 4996 )
#endif
#ifdef __ANDROID__
//#include <android_native_app_glue.h>
//extern void amain(struct android_app* state);
#endif
// Assert has special behavior on Windows (for Visual Studio).
#ifdef WIN32
#ifdef assert
#undef assert
#endif
#ifdef _DEBUG
#define assert(expression) do { \
if (!(expression)) \
{ \
printError("Assertion \'" #expression "\' failed."); \
__debugbreak(); \
} } while (0)
#else
#define assert(expression) do { (void)sizeof(expression); } while (0)
#endif
#endif
#endif