-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathSystemUtil.hpp
140 lines (116 loc) · 3.47 KB
/
SystemUtil.hpp
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
/*
This file is a part of Stonefish.
Stonefish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stonefish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//
// SystemUtil.hpp
// Stonefish
//
// Created by Patryk Cieslak on 11/28/12.
// Copyright (c) 2012-2017 Patryk Cieslak. All rights reserved.
//
#ifndef __Stonefish_SystemUtil__
#define __Stonefish_SystemUtil__
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctime>
#include <chrono>
#ifdef __linux__
#include <unistd.h>
#elif __APPLE__
#include <unistd.h>
#include <Carbon/Carbon.h>
#else //WINDOWS
#endif
#include "core/GraphicalSimulationApp.h"
#include "graphics/OpenGLDataStructs.h"
namespace sf
{
inline int64_t GetTimeInMicroseconds()
{
std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
}
inline int64_t GetTimeInNanoseconds()
{
std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
}
void GetCWD(char* buffer, int length);
inline std::string GetShaderPath()
{
return ((GraphicalSimulationApp*)SimulationApp::getApp())->getShaderPath();
}
inline std::string GetDataPath()
{
return SimulationApp::getApp()->getDataPath();
}
const char* GetDataPathPrefix(const char* directory);
//Extensions
inline bool CheckForExtension(const char* extensionName)
{
char* extensions = (char*)glGetString(GL_EXTENSIONS);
if(extensions == NULL)
return false;
size_t extNameLen = strlen(extensionName);
char* end = extensions + strlen(extensions);
while (extensions < end)
{
size_t n = strcspn(extensions, " ");
if((extNameLen == n) && (strncmp(extensionName, extensions, n) == 0))
return true;
extensions += (n+1);
}
return false;
}
//Random functions
inline long lrandom(long *seed)
{
*seed = (*seed * 1103515245 + 12345) & 0x7FFFFFFF;
return *seed;
}
inline float frandom(long *seed)
{
long r = lrandom(seed) >> (31 - 24);
return r / (float)(1 << 24);
}
inline float grandom(float mean, float stdDeviation, long *seed)
{
float x1, x2, w, y1;
static float y2;
static int use_last = 0;
if (use_last)
{
y1 = y2;
use_last = 0;
}
else
{
do
{
x1 = 2.0f * frandom(seed) - 1.0f;
x2 = 2.0f * frandom(seed) - 1.0f;
w = x1 * x1 + x2 * x2;
}
while (w >= 1.0f);
w = sqrt((-2.0f * log(w)) / w);
y1 = x1 * w;
y2 = x2 * w;
use_last = 1;
}
return mean + y1 * stdDeviation;
}
}
#endif