-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtil.cpp
422 lines (332 loc) · 7.99 KB
/
Util.cpp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "Util.h"
#include <cmath>
#include <SDL/SDL.h>
float currentTime()
{
static bool firstCall = true;
static long long startTime;
long long curTime = SDL_GetTicks();
if(firstCall)
{
firstCall = false;
startTime = curTime;
}
return float(curTime - startTime);
}
uint64_t Random::seedUniquifier = 8682522807148012;
Random::Random() : seed(uniqueSeed() ^ uint64_t(currentTime)) {}
Random::Random(const uint64_t seed) : seed(initialScramble(seed)) {}
uint64_t Random::initialScramble(const uint64_t seed)
{
return (seed ^ multiplier) & mask;
}
uint64_t Random::uniqueSeed()
{
// L'Ecuyer, "Tables of Linear Congruential Generators of
// Different Sizes and Good Lattice Structure", 1999
for (;;) {
const uint64_t current = seedUniquifier;
const uint64_t next = current * 181783497276652981L;
if (seedUniquifier == current)
{
seedUniquifier = next;
return next;
}
}
}
int Random::next(const int bits)
{
seed = (seed * multiplier + addend) & mask;
return int(seed >> (48 - bits));
}
float Random::nextFloat()
{
return float(next(24)) / float(1 << 24);
}
vec2 Random::nextVec2(float magnitude)
{
float x = nextFloat() * magnitude * 2.f;
float y = nextFloat() * magnitude * 2.f;
return vec2(x - magnitude, y - magnitude);
}
uint32_t Random::nextInt()
{
return next(32);
}
vec2 Random::nextIVec2(int magnitude)
{
int x = nextInt(magnitude * 2);
int y = nextInt(magnitude * 2);
return vec2(x - magnitude, y - magnitude);
}
uint32_t Random::nextInt(const uint32_t bound)
{
uint32_t r = next(31);
const uint32_t m = bound - 1;
if ((bound & m) == 0) // i.e., bound is a power of 2
r = uint32_t(bound * uint64_t(r) >> 31);
else {
for (uint32_t u = r;
u - (r = u % bound) + m < 0;
u = next(31));
}
return r;
}
uint64_t Random::nextLong() {
return ((uint64_t) (next(32)) << 32) + next(32);
}
void Random::setSeed(const uint64_t newSeed)
{
seed = initialScramble(newSeed);
}
// Perlin noise
float scaled_cosine(const float i) {
return 0.5f * (1.0f - cos(i * PI));
}
constexpr int PERLIN_RES = 1024;
constexpr float PERLIN_OCTAVES = 4; // default to medium smooth
constexpr float PERLIN_AMP_FALLOFF = 0.5f; // 50% reduction/octave
constexpr int PERLIN_YWRAPB = 4;
constexpr int PERLIN_YWRAP = 1 << PERLIN_YWRAPB;
constexpr int PERLIN_ZWRAPB = 8;
constexpr int PERLIN_ZWRAP = 1 << PERLIN_ZWRAPB;
float perlin[PERLIN_RES + 1];
float Perlin::noise(float x, float y) { // stolen from Processing
if (perlin[0] == 0) {
Random r = Random(18295169L);
for (float& i : perlin)
i = r.nextFloat();
}
if (x < 0)
x = -x;
if (y < 0)
y = -y;
int xi = int(x);
int yi = int(y);
float xf = x - xi;
float yf = y - yi;
float r = 0;
float ampl = 0.5f;
for (int i = 0; i < PERLIN_OCTAVES; i++) {
int of = xi + (yi << PERLIN_YWRAPB);
const float rxf = scaled_cosine(xf);
const float ryf = scaled_cosine(yf);
float n1 = perlin[of % PERLIN_RES];
n1 += rxf * (perlin[(of + 1) % PERLIN_RES] - n1);
float n2 = perlin[(of + PERLIN_YWRAP) % PERLIN_RES];
n2 += rxf * (perlin[(of + PERLIN_YWRAP + 1) % PERLIN_RES] - n2);
n1 += ryf * (n2 - n1);
of += PERLIN_ZWRAP;
n2 = perlin[of % PERLIN_RES];
n2 += rxf * (perlin[(of + 1) % PERLIN_RES] - n2);
float n3 = perlin[(of + PERLIN_YWRAP) % PERLIN_RES];
n3 += rxf * (perlin[(of + PERLIN_YWRAP + 1) % PERLIN_RES] - n3);
n2 += ryf * (n3 - n2);
n1 += scaled_cosine(0) * (n2 - n1);
r += n1 * ampl;
ampl *= PERLIN_AMP_FALLOFF;
xi <<= 1;
xf *= 2;
yi <<= 1;
yf *= 2;
if (xf >= 1.0) {
xi++;
xf--;
}
if (yf >= 1.0) {
yi++;
yf--;
}
}
return r;
}
float Perlin::noise(vec2 pos)
{
return noise(pos.x, pos.y);
}
float clamp(float val, const float min, const float max)
{
if (val < min)
val = min;
else if (val > max)
val = max;
return val;
}
bool glError()
{
const GLenum err = glGetError();
if (err != GL_NO_ERROR) {
char err_s[5];
itoa(err, err_s);
prints("OpenGL error "); prints(err_s); prints("\n");
return true;
}
return false;
}
void GLAPIENTRY error_callback(GLenum source,
const GLenum type,
GLuint id,
const GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam) {
prints("GL error/warning: "); prints(message); prints("\n");
//__debugbreak();
}
float radians(float deg)
{
return deg * (PI / 180.f);
}
float degrees(float rad)
{
return rad / (PI / 180.f);
}
/*float abs(float v)
{
return v < 0 ? -v : v;
}*/
bool sign(float v)
{
return (0 < v) - (v < 0);
}
float fract(float v)
{
return v - floor(v);
}
float pow(float v, int p)
{
for(int i = 0; i < p; i++)
v *= v;
return v;
}
float max(float a, float b)
{
return a > b ? a : b;
}
float mod(float x, float y) {
return x - trunc(x / y) * y;
}
vec3 max(const vec3& a, const vec3& b)
{
return vec3(
max(a.x, b.x),
max(a.y, b.y),
max(a.z, b.z)
);
}
// NOTE: broken for negatives (doesn't matter for terrain height)
int roundFloat(float v)
{
return int(v + 0.5f);
}
unsigned int murmurHash2(const char* str, int len)
{
// TODO should this be an arg?
constexpr unsigned int seed = 10;
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
const unsigned int m = 0x5bd1e995;
const int r = 24;
// Initialize the hash to a 'random' value
unsigned int h = seed ^ len;
// Mix 4 bytes at a time into the hash
const unsigned char* data = (const unsigned char*) str;
while(len >= 4)
{
unsigned int k = *(unsigned int *)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
// Handle the last few bytes of the input array
switch(len)
{
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
/*
unsigned long strlen(const char *str)
{
const char *s;
for (s = str; *s; ++s);
return (s - str);
}
char *strcpy(char *strDest, const char *strSrc)
{
char *temp = strDest;
while(*strDest++ = *strSrc++);
return temp;
}
char *strcat(char *dest, const char *src)
{
char *rdest = dest;
while (*dest)
dest++;
while (*dest++ = *src++);
return rdest;
}*/
void memcpy(void *dest, void *src, long unsigned int n)
{
// Typecast src and dest addresses to (char *)
char *csrc = (char *)src;
char *cdest = (char *)dest;
// Copy contents of src[] to dest[]
for (int i=0; i<n; i++)
cdest[i] = csrc[i];
}
void reverse(char s[])
{
int i, j;
char c;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
#ifdef DEBUG
#ifdef __unix__
#include <unistd.h>
#endif
void crash()
{
fflush(stdout);
#ifdef __unix__
_exit(0); // UNIX has a ruder function >:)
#else
_Exit(0);
#endif
}
#else
void crash()
{
SDL_Quit();
}
#endif