-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.hpp
More file actions
63 lines (52 loc) · 1.38 KB
/
utils.hpp
File metadata and controls
63 lines (52 loc) · 1.38 KB
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
#ifndef URHOEXTRAS_UTILS_HPP
#define URHOEXTRAS_UTILS_HPP
#include "mathutils.hpp"
#include <Urho3D/Math/Quaternion.h>
#include <Urho3D/Math/MathDefs.h>
#include <Urho3D/Math/Vector2.h>
#include <Urho3D/Math/Vector3.h>
#include <fstream>
#include <stdexcept>
namespace UrhoExtras
{
inline Urho3D::Quaternion getDirectionalLightRotation(Urho3D::Vector3 const& dir)
{
Urho3D::Vector2 dir_xz(dir.x_, dir.z_);
float yaw = getAngle(dir_xz);
float pitch = getAngle(-dir.y_, dir_xz.Length());
return Urho3D::Quaternion(yaw, Urho3D::Vector3::UP) * Urho3D::Quaternion(pitch, Urho3D::Vector3::RIGHT);
}
inline unsigned secureRand()
{
unsigned result;
std::ifstream file("/dev/urandom");
if (!file.is_open()) {
throw std::runtime_error("Unable to open RNG!");
}
file.read((char*)&result, 4);
return result;
}
template<class T>
inline Urho3D::String zfill(T val, unsigned length)
{
Urho3D::String result(val);
while (result.Length() < length) {
result = "0" + result;
}
return result;
}
inline unsigned countOccurences(Urho3D::String const& str, Urho3D::String const& substr)
{
unsigned result = 0;
for (unsigned offset = 0; offset < str.Length(); ) {
auto find = str.Find(substr, offset);
if (find == Urho3D::String::NPOS) {
break;
}
++ result;
offset = find + substr.Length();
}
return result;
}
}
#endif