Muto 0.0.1
Cross-Platform Application Template
GitHub Repo | Muto Updates | Muto's Author
Loading...
Searching...
No Matches
Random.h
Go to the documentation of this file.
1#pragma once
2/// @file Random.h
3/// @author Damon S. Green II
4/// @brief Provides random number generation utilities.
5
6#include <random>
7#include <algorithm>
8#include <cstdint>
9#include <glm/glm.hpp>
10#include "Debug/Instrumentor.h"
11
12
13namespace Muto {
14
15 /// @namespace Random
16 /// @brief Provides various functions and helpers related to randomization
17 namespace Random {
18
19 inline std::mt19937& GetRNG() {
20 static thread_local std::mt19937 rng{ std::random_device{}() };
21 return rng;
22 }
23
24 inline void Seed(uint32_t seed) {
25 GetRNG().seed(seed);
26 }
27
28 inline uint32_t UInt1(uint32_t max) {
30 std::uniform_int_distribution<uint32_t> dist(0, max - 1);
31 return dist(GetRNG());
32 }
33
34 inline bool Bool1(float trueChance) {
36 std::uniform_real_distribution<float> dist(0.0f, 1.0f);
37 return dist(GetRNG()) < trueChance;
38 }
39
40 inline unsigned char Char() {
42 std::uniform_int_distribution<int> dist(0, 255);
43 return static_cast<unsigned char>(dist(GetRNG()));
44 }
45
46 inline std::string String(size_t length) {
48 const char charset[] =
49 "0123456789"
50 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
51 "abcdefghijklmnopqrstuvwxyz";
52 const size_t max_index = (sizeof(charset) - 1);
53 std::string str(length, 0);
54 for (size_t i = 0; i < length; ++i) {
55 str[i] = charset[UInt1(static_cast<uint32_t>(max_index))];
56 }
57 return str;
58 }
59
60 inline std::string HexString(size_t length) {
62 const char charset[] =
63 "0123456789"
64 "ABCDEF";
65 const size_t max_index = (sizeof(charset) - 1);
66 std::string str(length, 0);
67 for (size_t i = 0; i < length; ++i) {
68 str[i] = charset[UInt1(static_cast<uint32_t>(max_index))];
69 }
70 return str;
71 }
72
73 inline std::string UUID() {
75 return HexString(8) + "-" + HexString(4) + "-" + HexString(4) + "-" + HexString(4) + "-" + HexString(12);
76 }
77
78 // Returns a float in the range [0.0, 1.0] = [inclusive, exclusive]
79 inline float Float1() {
81 static thread_local std::uniform_real_distribution<float> dist(0.0f, 1.0f);
82 return dist(GetRNG());
83 }
84
85 // Returns a float in the range [min, max]
86 // If you need max to be possible, use the "_Inclusive" variant which advances the upper bound
87 inline float RangeF1(float min, float max) {
89 if (min > max) std::swap(min, max);
90 std::uniform_real_distribution<float> dist(min, max);
91 return dist(GetRNG());
92 }
93
94 // Inclusive-upper variant: returns value in [min, max] (max possible)
95 inline float RangeF1_Inclusive(float min, float max) {
96 if (min > max) std::swap(min, max);
97 float upper = std::nextafter(max, std::numeric_limits<float>::infinity());
98 std::uniform_real_distribution<float> dist(min, upper);
99 return dist(GetRNG());
100 }
101
102 // Returns a 2D float vector with each component in the range [0.0, 1.0] = [inclusive, exclusive]
103 inline glm::vec2 Float2() {
104 return glm::vec2{ Float1(), Float1() };
105 }
106
107 // Returns a 2D float vector with each component in the range [min, max]
108 inline glm::vec2 RangeF2(float min, float max) {
109 return glm::vec2{ RangeF1(min, max), RangeF1(min, max) };
110 }
111
112 // Returns a 2D float vector with each component in the range defined by the components of the given vector
113 inline glm::vec2 RangeF2(float min1, float max1, float min2, float max2) {
114 return glm::vec2{ RangeF1(min1, max1), RangeF1(min2, max2) };
115 }
116
117 // Returns a 2D float vector with each component in the specified ranges
118 inline glm::vec2 RangeF2(const glm::vec2& minRange, const glm::vec2& maxRange) {
119 return glm::vec2{ RangeF1(minRange.x, maxRange.x), RangeF1(minRange.y, maxRange.y) };
120 }
121
122
123 // Returns a 3D float vector with each component in the range [0.0, 1.0] = [inclusive, exclusive]
124 inline glm::vec3 Float3() {
125 return glm::vec3{ Float1(), Float1(), Float1() };
126 }
127
128 // Returns a 3D float vector with each component in the range [min, max]
129 inline glm::vec3 RangeF3(float min, float max) {
130 return glm::vec3{ RangeF1(min, max), RangeF1(min, max), RangeF1(min, max) };
131 }
132
133 // Returns a 3D float vector with each component in the specified ranges
134 inline glm::vec3 RangeF3(float min1, float max1, float min2, float max2, float min3, float max3) {
135 return glm::vec3{ RangeF1(min1, max1), RangeF1(min2, max2), RangeF1(min3, max3) };
136 }
137
138 // Returns a 3D float vector with each component in the specified ranges
139 inline glm::vec3 RangeF3(const glm::vec2& range1, const glm::vec2& range2, const glm::vec2& range3) {
140 return glm::vec3{ RangeF1(range1.x, range1.y), RangeF1(range2.x, range2.y), RangeF1(range3.x, range3.y) };
141 }
142
143 // Returns a 4D float vector with each component in the range [0.0, 1.0] = [inclusive, exclusive]
144 inline glm::vec4 Float4() {
145 return glm::vec4{ Float1(), Float1(), Float1(), Float1() };
146 }
147
148 // Returns a 4D float vector with each component in the range [min, max]
149 inline glm::vec4 RangeF4(float min, float max) {
150 return glm::vec4{ RangeF1(min, max), RangeF1(min, max), RangeF1(min, max), RangeF1(min, max) };
151 }
152
153
154
155
156 }
157}
#define MU_CORE_ASSERT(x,...)
Definition Asserts.h:32
#define MU_BIND_EVENT_FN(fn)
Binds a member function as an event handler.
Definition Defines_Macros.h:17
static const uint32_t s_MapHeight
Definition EditorLayer.cpp:14
static const char * s_MapTiles
Definition EditorLayer.cpp:15
static const uint32_t s_MapWidth
Definition EditorLayer.cpp:13
#define MU_PROFILE_SCOPE(name)
Definition Instrumentor.h:239
#define MU_PROFILE_FUNCTION()
Definition Instrumentor.h:240
#define MU_CORE_ERROR(...)
error: indicates an error that has occurred
Definition Log.h:42
#define MU_CORE_INFO(...)
info: general information about application flow
Definition Log.h:38
The core application class that manages the main loop, window, layers, and event handling.
Definition Application.h:70
void Close()
Closes the application.
Definition Application.cpp:103
ImGuiLayer * GetImGuiLayer()
Retrieves the ImGui layer.
Definition Application.h:104
static Application & Get()
Retrieves the singleton instance of the Application.
Definition Application.h:107
Definition EditorCamera.h:20
Definition EditorLayer.h:13
bool useEntityScene
Definition EditorLayer.h:90
OrthographicCameraController m_CameraController
Definition EditorLayer.h:52
bool m_PrimaryCamera
Definition EditorLayer.h:46
virtual void OnAttach() override
Called when the layer is attached to the application.
Definition EditorLayer.cpp:46
bool scene3
Definition EditorLayer.h:89
glm::vec2 m_ViewportBounds[2]
Definition EditorLayer.h:45
Ref< Scene > m_ActiveScene
Definition EditorLayer.h:34
Ref< Scene > m_EditorScene
Definition EditorLayer.h:35
ParticleProps m_ParticleProps
Definition EditorLayer.h:86
glm::vec4 m_TextureTintColor2
Definition EditorLayer.h:94
Ref< SubTexture2D > m_SubTextureSmoke
Definition EditorLayer.h:70
void OpenScene()
Definition EditorLayer.cpp:539
glm::vec4 m_SpecialQuadColor
Definition EditorLayer.h:97
bool scene2
Definition EditorLayer.h:89
EditorCamera m_EditorCamera
Definition EditorLayer.h:78
bool scene1
Definition EditorLayer.h:89
int m_GizmoType
Definition EditorLayer.h:49
Ref< Shader > m_FlatColorShader
Definition EditorLayer.h:59
bool OnKeyPressed(KeyPressedEvent &e)
Definition EditorLayer.cpp:480
Entity m_CameraEntity
Definition EditorLayer.h:47
EditorLayer()
Definition EditorLayer.cpp:38
void NewScene()
Definition EditorLayer.cpp:532
Ref< Framebuffer > m_Framebuffer
Definition EditorLayer.h:76
Entity m_FireEntity
Definition EditorLayer.h:55
float m_textureScale
Definition EditorLayer.h:80
void SaveSceneAs()
Definition EditorLayer.cpp:555
Ref< Texture2D > m_SpriteSheetFire
Definition EditorLayer.h:62
Ref< SubTexture2D > m_SubTextureFire
Definition EditorLayer.h:69
bool scene4
Definition EditorLayer.h:89
std::unordered_map< char, Ref< SubTexture2D > > s_TextureMap
Definition EditorLayer.h:100
virtual void OnDetach() override
Called when the layer is detached from the application.
Definition EditorLayer.cpp:230
SceneHierarchyPanel m_SceneHierarchyPanel
Definition EditorLayer.h:32
float m_squareRotation
Definition EditorLayer.h:81
bool m_UseSpecialQuadColor
Definition EditorLayer.h:98
glm::vec4 m_ClearColor
Definition EditorLayer.h:96
float lastFrameTime
Definition EditorLayer.h:54
virtual void OnEvent(Event &e) override
Called when an event is dispatched to the layer.
Definition EditorLayer.cpp:468
virtual ~EditorLayer()=default
glm::vec4 m_SquareColor
Definition EditorLayer.h:92
virtual void OnUpdate(Timestep ts) override
Called every frame to update the layer with the given timestep.
Definition EditorLayer.cpp:235
ParticleSystem m_ParticleSystem
Definition EditorLayer.h:85
glm::vec4 m_TextureTintColor1
Definition EditorLayer.h:93
void ResetScene()
Definition EditorLayer.cpp:566
glm::vec2 m_ViewportSize
Definition EditorLayer.h:44
float m_TranslationSnap
Definition EditorLayer.h:50
SceneState m_SceneState
Definition EditorLayer.h:41
float m_ScaleSnap
Definition EditorLayer.h:50
Ref< Texture2D > m_SpriteSheetTown
Definition EditorLayer.h:64
Ref< Texture2D > m_CheckerboardTexture
Definition EditorLayer.h:60
bool m_ViewportFocused
Definition EditorLayer.h:43
Ref< Texture2D > m_SpriteSheetRocks
Definition EditorLayer.h:66
Ref< VertexArray > m_SquareVA
Definition EditorLayer.h:58
float m_RotationSnap
Definition EditorLayer.h:50
int m_ParticleEmitCount
Definition EditorLayer.h:83
glm::vec4 m_BackgroundColor
Definition EditorLayer.h:95
Ref< Texture2D > m_SpriteSheetSmoke
Definition EditorLayer.h:63
virtual void OnImGuiRender() override
Called when the layer should render its ImGui components.
Definition EditorLayer.cpp:281
SceneState
Definition EditorLayer.h:38
Entity m_SmokeEntity
Definition EditorLayer.h:55
bool m_ViewportHovered
Definition EditorLayer.h:43
float m_specialQuadRotation
Definition EditorLayer.h:82
Ref< SubTexture2D > m_SubTextureTown
Definition EditorLayer.h:71
Ref< Texture2D > m_SpriteSheetCursedLands
Definition EditorLayer.h:67
Ref< Texture2D > m_SpriteSheetCrystals
Definition EditorLayer.h:65
Represents an entity in a scene.
Definition Entity.h:17
Stack-based templated event dispatcher.
Definition Event.h:66
EventDispatcher(Event &event)
Construct an EventDispatcher for a specific event.
Definition Event.h:76
Abstract base class for all events.
Definition Event.h:35
Cross-platform file dialog utilities.
Definition PlatformUtils.h:13
static std::string SaveFile(const char *filter)
Opens a file dialog to select a location to save a file.
Definition LinuxPlatformUtils.cpp:165
static std::string OpenFile(const char *filter)
Opens a file dialog to select a file to open.
Definition LinuxPlatformUtils.cpp:160
WIP.
Definition PlatformUtils.h:31
static std::string m_RootEditorDirectory
Definition PlatformUtils.h:41
static std::string m_RootEngineDirectory
Definition PlatformUtils.h:40
static std::string m_ProjectsDirectory
Definition PlatformUtils.h:44
static std::string m_AssetsDirectory
Definition PlatformUtils.h:43
static bool IsInitialized()
Definition PlatformUtils.h:37
static void Initialize()
Definition FileSystem.cpp:16
static bool m_Initialized
Definition PlatformUtils.h:39
static std::string GetAbsolutePath(const std::string &relativePath)
Definition FileSystem.cpp:38
static std::string GetCurrentWorkingDirectory()
Definition FileSystem.cpp:33
static std::string m_ResourcesDirectory
Definition PlatformUtils.h:42
static std::string m_CurrentProjectDirectory
Definition PlatformUtils.h:45
static std::string GetTravelingUpPath(const std::string &path)
Definition FileSystem.cpp:43
virtual void SetBlockEvents(bool block)
Definition ImGuiLayer.h:29
Base input class for querying input states.
Definition Input.h:15
static bool IsKeyPressed(int keycode)
Checks if the specified key is currently pressed.
Definition WindowsInput.cpp:9
int GetKeyCode() const
Get the key code associated with the event.
Definition KeyEvent.h:20
Event for registering key press.
Definition KeyEvent.h:37
int GetRepeatCount() const
Get the repeat count of the key press event.
Definition KeyEvent.h:48
Represents a reusable application layer that receives lifecycle callbacks (attach,...
Definition Layer.h:17
Definition OrthographicCameraController.h:30
A simple particle system for stress testing the renderer. This is a temporary starter particle system...
Definition ParticleSystem.h:45
A static class that provides an interface for issuing rendering commands.
Definition RenderCommand.h:13
static void Clear()
Clears the rendering buffers.
Definition RenderCommand.h:35
A 2D renderer for drawing quads and sprites.
Definition Renderer2D.h:21
static void ResetStats()
Resets the rendering statistics.
Definition Renderer2D.cpp:516
static Statistics GetStats()
Retrieves the current rendering statistics.
Definition Renderer2D.cpp:521
Definition SceneHierarchyPanel.h:12
Handles serialization and deserialization of scenes.
Definition SceneSerializer.h:16
void Serialize(const std::string &filepath)
Definition SceneSerializer.cpp:179
bool Deserialize(const std::string &filepath)
Definition SceneSerializer.cpp:206
Represents a time step in seconds.
Definition Timestep.h:11
@ RightControl
Definition KeyCodes.h:147
@ LeftControl
Definition KeyCodes.h:143
@ S
Definition KeyCodes.h:59
@ N
Definition KeyCodes.h:54
@ Q
Definition KeyCodes.h:57
@ O
Definition KeyCodes.h:55
@ LeftShift
Definition KeyCodes.h:142
@ RightShift
Definition KeyCodes.h:146
Definition Random.h:17
glm::vec3 RangeF3(float min, float max)
Definition Random.h:129
glm::vec3 Float3()
Definition Random.h:124
glm::vec2 RangeF2(float min1, float max1, float min2, float max2)
Definition Random.h:113
float RangeF1(float min, float max)
Definition Random.h:87
glm::vec3 RangeF3(const glm::vec2 &range1, const glm::vec2 &range2, const glm::vec2 &range3)
Definition Random.h:139
std::string String(size_t length)
Definition Random.h:46
glm::vec2 Float2()
Definition Random.h:103
std::mt19937 & GetRNG()
Definition Random.h:19
uint32_t UInt1(uint32_t max)
Definition Random.h:28
bool Bool1(float trueChance)
Definition Random.h:34
glm::vec2 RangeF2(float min, float max)
Definition Random.h:108
std::string HexString(size_t length)
Definition Random.h:60
void Seed(uint32_t seed)
Definition Random.h:24
glm::vec3 RangeF3(float min1, float max1, float min2, float max2, float min3, float max3)
Definition Random.h:134
float Float1()
Definition Random.h:79
unsigned char Char()
Definition Random.h:40
float RangeF1_Inclusive(float min, float max)
Definition Random.h:95
glm::vec4 Float4()
Definition Random.h:144
std::string UUID()
Definition Random.h:73
glm::vec2 RangeF2(const glm::vec2 &minRange, const glm::vec2 &maxRange)
Definition Random.h:118
glm::vec4 RangeF4(float min, float max)
Definition Random.h:149
The main namespace for the Muto engine.
Definition Application.cpp:15
uint32_t Height
Definition Framebuffer.h:15
uint32_t Width
Definition Framebuffer.h:15
Specification for creating a Framebuffer.
Definition Framebuffer.h:14
Definition ParticleSystem.h:12
Component that holds the transform of an entity.
Definition Components.h:68