This commit is contained in:
nastys
2019-07-30 13:48:28 +02:00
parent 25f819bdfb
commit 998f4274a5
157 changed files with 0 additions and 0 deletions
@@ -0,0 +1,12 @@
#pragma once
namespace TLAC::Utilities
{
template<class T> inline T operator~ (T a) { return (T)~(int)a; }
template<class T> inline T operator| (T a, T b) { return (T)((int)a | (int)b); }
template<class T> inline T operator& (T a, T b) { return (T)((int)a & (int)b); }
template<class T> inline T operator^ (T a, T b) { return (T)((int)a ^ (int)b); }
template<class T> inline T& operator|= (T& a, T b) { return (T&)((int&)a |= (int)b); }
template<class T> inline T& operator&= (T& a, T b) { return (T&)((int&)a &= (int)b); }
template<class T> inline T& operator^= (T& a, T b) { return (T&)((int&)a ^= (int)b); }
}
@@ -0,0 +1,37 @@
#define _USE_MATH_DEFINES
#include "Math.h"
namespace TLAC::Utilities
{
float ToDegrees(float radians)
{
return radians * (180.0f / M_PI);
}
float ToRadians(float degrees)
{
return (degrees * M_PI) / 180.0f;
}
Vec2 GetDirection(float degrees)
{
float radians = ToRadians(degrees);
return Vec2(cos(radians), sin(radians));
}
Vec2 PointFromAngle(float degrees, float distance)
{
float radians = ToRadians(degrees + 90.0f);
return Vec2(-1 * std::cos(radians) * distance, -1 * std::sin(radians) * distance);
}
float AngleFromPoints(Vec2 p0, Vec2 p1)
{
return (float)(std::atan2(p1.Y - p0.Y, p1.X - p0.X) * 180.0 / M_PI) + 90.0f;
}
float ConvertRange(float originalStart, float originalEnd, float newStart, float newEnd, float value)
{
return newStart + ((value - originalStart) * (newEnd - newStart) / (originalEnd - originalStart));
}
}
@@ -0,0 +1,15 @@
#pragma once
#include <cmath>
#include "Vec2.h"
#include "Vec3.h"
namespace TLAC::Utilities
{
float ToDegrees(float radians);
float ToRadians(float degrees);
Vec2 GetDirection(float degrees);
Vec2 PointFromAngle(float degrees, float distance);
float AngleFromPoints(Vec2 p0, Vec2 p1);
float ConvertRange(float originalStart, float originalEnd, float newStart, float newEnd, float value);
}
@@ -0,0 +1,60 @@
#pragma once
#include "Operations.h"
namespace TLAC::Utilities
{
std::vector<std::string> Split(const std::string& str, const std::string& delim)
{
std::vector<std::string> tokens;
size_t prev = 0, pos = 0;
do
{
pos = str.find(delim, prev);
if (pos == std::string::npos)
pos = str.length();
std::string token = str.substr(prev, pos - prev);
if (!token.empty())
tokens.push_back(token);
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
return tokens;
}
void LeftTrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch)
{
return !std::isspace(ch);
}));
}
void RightTrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch)
{
return !std::isspace(ch);
}).base(), s.end());
}
void Trim(std::string &s)
{
s = trim(s);
}
std::string trim(const std::string& str, const std::string& whitespace)
{
const size_t strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return "";
const size_t strEnd = str.find_last_not_of(whitespace);
const size_t strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
}
@@ -0,0 +1,16 @@
#pragma once
#include <algorithm>
#include <cctype>
#include <locale>
#include <vector>
namespace TLAC::Utilities
{
std::vector<std::string> Split(const std::string& str, const std::string& delim);
void LeftTrim(std::string &s);
void RightTrim(std::string &s);
void Trim(std::string &s);
std::string trim(const std::string& str, const std::string& whitespace = " \t");
}
@@ -0,0 +1,36 @@
#include "Stopwatch.h"
namespace TLAC::Utilities
{
Stopwatch::Stopwatch()
{
}
Stopwatch::~Stopwatch()
{
}
void Stopwatch::Start()
{
start = high_resolution_clock::now();
}
float Stopwatch::Stop()
{
end = high_resolution_clock::now();
return GetElapsed();
}
float Stopwatch::Restart()
{
float elapsed = Stop();
Start();
return elapsed;
}
float Stopwatch::GetElapsed()
{
return (float)(chrono::duration_cast<std::chrono::microseconds>(end - start).count() / TIME_FACTOR);
}
}
@@ -0,0 +1,30 @@
#pragma once
#include <chrono>
namespace chrono = std::chrono;
typedef chrono::time_point<chrono::steady_clock> steady_clock;
typedef chrono::high_resolution_clock high_resolution_clock;
namespace TLAC::Utilities
{
class Stopwatch
{
const float TIME_FACTOR = 1000.0f;
public:
Stopwatch();
~Stopwatch();
void Start();
float Stop();
float Restart();
float GetElapsed();
private:
steady_clock start;
steady_clock end;
};
}
@@ -0,0 +1,34 @@
#include "Vec2.h"
namespace TLAC::Utilities
{
Vec2::Vec2()
{
};
Vec2::Vec2(float x, float y) : X(x), Y(y)
{
};
Vec2 Vec2::operator+(Vec2 value)
{
return Vec2(X + value.X, Y + value.Y);
}
void Vec2::operator+=(const Vec2 &value)
{
X += value.X;
Y += value.Y;
}
Vec2 Vec2::operator-(Vec2 value)
{
return Vec2(X - value.X, Y - value.Y);
}
void Vec2::operator-=(const Vec2 &value)
{
X -= value.X;
Y -= value.Y;
}
}
@@ -0,0 +1,18 @@
#pragma once
namespace TLAC::Utilities
{
struct Vec2
{
float X, Y;
Vec2();
Vec2(float x, float y);
Vec2 operator+(Vec2 value);
void operator+=(const Vec2 &value);
Vec2 operator-(Vec2 value);
void operator-=(const Vec2 &value);
};
}
@@ -0,0 +1,58 @@
#include "Vec3.h"
namespace TLAC::Utilities
{
Vec3::Vec3()
{
};
Vec3::Vec3(float x, float y, float z) : X(x), Y(y), Z(z)
{
};
Vec3 Vec3::operator+(Vec2 value)
{
return Vec3(X + value.X, Y, Z + value.Y);
}
void Vec3::operator+=(const Vec2 &value)
{
X += value.X;
Z += value.Y;
}
Vec3 Vec3::operator-(Vec2 value)
{
return Vec3(X - value.X, Y, Z - value.Y);
}
void Vec3::operator-=(const Vec2 &value)
{
X -= value.X;
Z -= value.Y;
}
Vec3 Vec3::operator+(Vec3 value)
{
return Vec3(X + value.X, Y + value.Y, Z + value.Z);
}
void Vec3::operator+=(const Vec3 &value)
{
X += value.X;
Y += value.Y;
Z += value.Z;
}
Vec3 Vec3::operator-(Vec3 value)
{
return Vec3(X - value.X, Y - value.Y, Z - value.Z);
}
void Vec3::operator-=(const Vec3 &value)
{
X -= value.X;
Y -= value.Y;
Z -= value.Z;
}
}
@@ -0,0 +1,27 @@
#pragma once
#include "Vec2.h"
namespace TLAC::Utilities
{
struct Vec3
{
float X, Y, Z;
Vec3();
Vec3(float x, float y, float z);
// Vec2
Vec3 operator+(Vec2 value);
void operator+=(const Vec2 &value);
Vec3 operator-(Vec2 value);
void operator-=(const Vec2 &value);
// Vec3
Vec3 operator+(Vec3 value);
void operator+=(const Vec3 &value);
Vec3 operator-(Vec3 value);
void operator-=(const Vec3 &value);
};
}