Mountain  1.0.0
Simple C++ 2D Game Framework
utils.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <filesystem>
4 #include <functional>
5 #include <thread>
6 
7 #include <magic_enum/magic_enum.hpp>
8 
9 #include <Maths/quaternion.hpp>
10 #include <Maths/vector2.hpp>
11 #include <Maths/vector3.hpp>
12 
13 #include "Mountain/core.hpp"
14 
18 
21 
22 #define TO_STRING(x) #x
23 #define STRINGIFY(x) TO_STRING(x)
24 
27 namespace Mountain::Utils
28 {
29  enum class TrimOptions : uint8_t
30  {
31  None = 0,
32  Start = 1 << 0,
33  End = 1 << 1,
34  Both = Start | End
35  };
36 
37  template <typename T>
38  using ProjectionFunc = T(*)(T);
39 
40  template <typename T>
41  constexpr ProjectionFunc<T> Identity = [](T t) { return t; };
42 
48  template <typename PtrT, Concepts::IntegralT IntT>
49  [[nodiscard]]
50  constexpr PtrT* IntToPointer(IntT number);
51 
55  template <typename T>
56  [[nodiscard]]
57  size_t GetTypeHash();
58 
63  template <typename T>
64  [[nodiscard]]
65  size_t GetTypeHash(const T* ptr);
66 
70  MOUNTAIN_API void AlignImGuiCursor(float_t objectWidth, float_t alignment = 0.5f);
71 
79  [[nodiscard]]
80  MOUNTAIN_API std::string HumanizeString(const std::string& str);
81 
89  [[nodiscard]]
90  MOUNTAIN_API std::string HumanizeVariableName(const std::string& str);
91 
98  [[nodiscard]]
99  MOUNTAIN_API constexpr std::string RemoveNamespaces(const std::string& str);
100 
101 #ifndef SWIG
102  [[nodiscard]]
109  MOUNTAIN_API constexpr const char_t* RemoveNamespaces(const char_t* str);
110 #endif
111 
119  [[nodiscard]]
120  MOUNTAIN_API constexpr float_t RemapValue(float_t oldValue, Vector2 oldRange, Vector2 newRange);
121 
129  [[nodiscard]]
130  MOUNTAIN_API constexpr size_t RemapValue(size_t oldValue, Vector2i oldRange, Vector2i newRange);
131 
135  MOUNTAIN_API float_t NormalizeAngle(float_t angle);
136 
140  MOUNTAIN_API Vector3 NormalizeAngles(Vector3 angles);
141 
145  MOUNTAIN_API Vector3 GetQuaternionEulerAngles(const Quaternion& rot);
146 
157  template <typename T, typename U>
158  [[nodiscard]]
159  Pointer<T> DynamicPointerCast(const Pointer<U>& value);
160 
163  MOUNTAIN_API void OpenInExplorer(const std::filesystem::path& path);
164 
168  MOUNTAIN_API void OpenInExplorer(const std::filesystem::path& path, bool_t isFile);
169 
172  MOUNTAIN_API void OpenFile(const std::filesystem::path& filepath);
173 
175  template <std::ranges::input_range Container, typename T>
176  [[nodiscard]]
177  bool_t ArrayContains(const Container& container, T element);
178 
180  template <std::ranges::input_range Container>
181  [[nodiscard]]
182  bool_t StringArrayContains(const Container& container, const std::string& element);
183 
185  [[nodiscard]]
186  MOUNTAIN_API bool_t StringEqualsIgnoreCase(std::string_view a, std::string_view b);
187 
189  [[nodiscard]]
190  MOUNTAIN_API bool_t StringContainsIgnoreCase(std::string_view a, std::string_view b);
191 
197  template <typename Ret, typename... Args>
198  [[nodiscard]]
199  constexpr size_t FunctionAddress(std::function<Ret(Args...)> f);
200 
201  MOUNTAIN_API int32_t TerminalCommand(const std::string& command, bool_t asynchronous = true);
202 
203  MOUNTAIN_API void CreateEmptyFile(const std::filesystem::path& path);
204 
205  MOUNTAIN_API void SetThreadName(std::thread& thread, const std::wstring& name);
206 
207  template <typename R, typename... Args>
208  R CallSafe(const std::function<R(Args...)>& function, Args&&... args);
209 
210  MOUNTAIN_API std::wstring NarrowToWide(std::string_view str);
211 
212  MOUNTAIN_API std::string WideToNarrow(std::wstring_view str);
213 
214  MOUNTAIN_API std::string ToLower(std::string_view str);
215 
216  MOUNTAIN_API std::string ToUpper(std::string_view str);
217 
218  MOUNTAIN_API std::pair<float_t, std::string_view> ByteSizeUnit(int64_t size);
219 
220  MOUNTAIN_API std::string GetBuiltinShadersPath();
221 
222  MOUNTAIN_API std::string GetBuiltinAssetsPath();
223 
224  MOUNTAIN_API std::string Trim(std::string_view str, TrimOptions options = TrimOptions::Both);
225 
226  MOUNTAIN_API std::string GetLine(const std::string& str, size_t lineIndex);
227 
228  MOUNTAIN_API uint16_t Concat16(uint8_t right, uint8_t left);
229 
230  MOUNTAIN_API uint32_t Concat32(uint8_t right0, uint8_t right1, uint8_t left0, uint8_t left1);
231 
232  template <uint64_t Offset, uint64_t Count>
233  constexpr uint64_t GetBits(const uint64_t value);
234 
235  template <Concepts::EnumT T>
236  constexpr Meta::Flags<T> ToFlags(T enumValue);
237 }
238 
239 namespace Easing
240 {
241  enum class Type : uint8_t
242  {
243  Linear,
244 
245  SineIn,
246  SineOut,
247  SineInOut,
248 
249  QuadIn,
250  QuadOut,
251  QuadInOut,
252 
253  CubicIn,
254  CubicOut,
255  CubicInOut,
256 
257  QuartIn,
258  QuartOut,
259  QuartInOut,
260 
261  QuintIn,
262  QuintOut,
263  QuintInOut,
264 
265  ExpoIn,
266  ExpoOut,
267  ExpoInOut,
268 
269  CircIn,
270  CircOut,
271  CircInOut,
272 
273  BackIn,
274  BackOut,
275  BackInOut,
276 
277  ElasticIn,
278  ElasticOut,
279  ElasticInOut,
280 
281  BounceIn,
282  BounceOut,
284  };
285 }
286 
287 ENUM_FLAGS(Mountain::Utils::TrimOptions)
288 
289 #include "Mountain/utils/utils.inl"
constexpr float_t BackInOut(float_t t)
constexpr float_t CubicInOut(float_t t)
constexpr float_t QuadIn(float_t t)
MATH_TOOLBOX float_t ElasticIn(float_t t)
MATH_TOOLBOX float_t BounceIn(float_t t)
MOUNTAIN_API constexpr std::string RemoveNamespaces(const std::string &str)
Removes the namespaces indicators from the provided string.
constexpr float_t BackIn(float_t t)
MOUNTAIN_API bool_t StringEqualsIgnoreCase(std::string_view a, std::string_view b)
Checks if two strings are equal, case-insensitive.
constexpr float_t QuartOut(float_t t)
MATH_TOOLBOX float_t BounceInOut(float_t t)
MOUNTAIN_API void AlignImGuiCursor(float_t objectWidth, float_t alignment=0.5f)
Horizontally aligns the cursor of ImGui to be centered around a specific portion of the available spa...
MOUNTAIN_API float_t NormalizeAngle(float_t angle)
Normalizes an angle (clamps its value between 0 and 2 * PI)
Pointer< T > DynamicPointerCast(const Pointer< U > &value)
Equivalent of a dynamic_cast for Pointer "Pointers".
bool_t StringArrayContains(const Container &container, const std::string &element)
Returns whether a string array contains an element using Utils::StringEqualsIgnoreCase.
constexpr float_t CubicIn(float_t t)
MATH_TOOLBOX float_t ElasticInOut(float_t t)
MATH_TOOLBOX float_t CircOut(float_t t)
MOUNTAIN_API void OpenInExplorer(const std::filesystem::path &path)
Opens the specified path in the file explorer.
bool_t ArrayContains(const Container &container, T element)
Returns whether an array contains an element.
MATH_TOOLBOX float_t ExpoInOut(float_t t)
constexpr float_t QuadOut(float_t t)
constexpr float_t CubicOut(float_t t)
MOUNTAIN_API std::string HumanizeVariableName(const std::string &str)
Humanizes the provided variable name.
constexpr size_t FunctionAddress(std::function< Ret(Args...)> f)
Gets the address of a function.
MATH_TOOLBOX float_t SineOut(float_t t)
MOUNTAIN_API Vector3 GetQuaternionEulerAngles(const Quaternion &rot)
Converts a quaternion to its euler angle representation.
constexpr float_t QuartIn(float_t t)
MATH_TOOLBOX float_t SineIn(float_t t)
Defines utilities for meta programming and template manipulation.
MATH_TOOLBOX float_t ElasticOut(float_t t)
constexpr float_t QuintIn(float_t t)
MATH_TOOLBOX float_t ExpoOut(float_t t)
MATH_TOOLBOX float_t BounceOut(float_t t)
constexpr float_t QuartInOut(float_t t)
Defines the Mountain::Pointer class.
MOUNTAIN_API constexpr float_t RemapValue(float_t oldValue, Vector2 oldRange, Vector2 newRange)
Remaps a value from one range to another.
Defines the Mountain::Concepts namespace which contains useful concepts used in the engine...
constexpr float_t BackOut(float_t t)
MATH_TOOLBOX float_t SineInOut(float_t t)
MOUNTAIN_API void OpenFile(const std::filesystem::path &filepath)
Opens the specified file on the user&#39;s computer.
constexpr PtrT * IntToPointer(IntT number)
Converts a integral number to a valid pointer without illegal size operations.
MOUNTAIN_API std::string HumanizeString(const std::string &str)
Humanizes the provided string.
MOUNTAIN_API Vector3 NormalizeAngles(Vector3 angles)
Normalizes a set of 3 angles in a Vector3 (clamps their value between 0 and 2 * PI) ...
MATH_TOOLBOX float_t ExpoIn(float_t t)
MATH_TOOLBOX float_t CircIn(float_t t)
constexpr float_t QuintOut(float_t t)
constexpr float_t QuadInOut(float_t t)
MOUNTAIN_API bool_t StringContainsIgnoreCase(std::string_view a, std::string_view b)
Checks if a string contains another one, case-insensitive.
size_t GetTypeHash()
Gets the hash code of a specified type.
constexpr float_t QuintInOut(float_t t)
MATH_TOOLBOX float_t CircInOut(float_t t)