Botcraft 26.3
Loading...
Searching...
No Matches
RenderingManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <any>
4#include <array>
5#include <atomic>
6#include <condition_variable>
7#include <functional>
8#include <memory>
9#include <mutex>
10#include <thread>
11#include <unordered_set>
12
14
16
17struct GLFWwindow;
18
19namespace Botcraft
20{
21 class World;
22 class InventoryManager;
23 class EntityManager;
24 class LocalPlayer;
25 class BaseNode;
26
27 namespace Renderer
28 {
29 class Shader;
30 class Atlas;
31 class WorldRenderer;
32 class BehaviourRenderer;
33
34 // Key that can be used in KeyboardCallback when set
35 enum class KEY_CODE
36 {
37 FORWARD = 0,
39 RIGHT,
40 LEFT,
41 SPACE,
42 SHIFT,
43 CTRL,
44 ESC,
49 };
50
52 {
53 public:
54 static std::shared_ptr<RenderingManager> CreateRenderingManagerIfPossible(
55 std::shared_ptr<World> world_,
56 std::shared_ptr<InventoryManager> inventory_manager_,
57 std::shared_ptr<EntityManager> entity_manager_
58 );
59
60 private:
61 // Private constructor, use the static function above to get the instance instead
62 // Window can be resized at runtime
63 // Chunks in renderer are independant of chunks in the corresponding world.
64 // Set headless_ to true to run without opening a window (rendering is still done)
65 RenderingManager(std::shared_ptr<World> world_, std::shared_ptr<InventoryManager> inventory_manager_,
66 std::shared_ptr<EntityManager> entity_manager_
67 );
68
69 static std::weak_ptr<RenderingManager> current_instance;
70 static std::mutex instance_mutex;
71
72 public:
74
75 // Set a flag to terminate the rendering loop after the current frame
76 void Close();
77
78 // Set mouse and keyboard callbacks to handle user inputs
79 void SetMouseCallback(std::function<void(double, double)> callback);
80 void SetKeyboardCallback(std::function<void(std::array<bool, static_cast<int>(KEY_CODE::NUMBER_OF_KEYS)>, double)> callback);
81 void AddChunkToUpdate(const int x, const int z);
82 void AddEntityToUpdate(const int id);
83
84 void SetCurrentBehaviourTree(const BaseNode* root) const;
85 void ResetBehaviourState() const;
86 void BehaviourStartTick() const;
87 void BehaviourEndTick(const bool b) const;
88 void BehaviourTickChild(const size_t i) const;
89 bool IsBehaviourGUIPaused() const;
90
91 void ResetBlackboard() const;
92 void UpdateBlackboardValue(const std::string& key, const std::any& value) const;
93 void RemoveBlackboardValue(const std::string& key) const;
94
95
96 protected:
98
99
100 // Chunk stuff
101 virtual void Handle(ProtocolCraft::ClientboundBlockUpdatePacket& msg) override;
104#if PROTOCOL_VERSION < 757 /* < 1.18 */
105 virtual void Handle(ProtocolCraft::ClientboundLevelChunkPacket& msg) override;
106#else
108#endif
109
110 // Entity stuff
111 virtual void Handle(ProtocolCraft::ClientboundAddEntityPacket& msg) override;
112#if PROTOCOL_VERSION < 759 /* < 1.19 */
113 virtual void Handle(ProtocolCraft::ClientboundAddMobPacket& msg) override;
114#endif
115#if PROTOCOL_VERSION < 721 /* < 1.16 */
116 virtual void Handle(ProtocolCraft::ClientboundAddGlobalEntityPacket& msg) override;
117#endif
118#if PROTOCOL_VERSION < 764 /* < 1.20.2 */
119 virtual void Handle(ProtocolCraft::ClientboundAddPlayerPacket& msg) override;
120#endif
121 virtual void Handle(ProtocolCraft::ClientboundTeleportEntityPacket& msg) override;
122#if PROTOCOL_VERSION < 755 /* < 1.17 */
123 virtual void Handle(ProtocolCraft::ClientboundMoveEntityPacket& msg) override;
124#endif
125 virtual void Handle(ProtocolCraft::ClientboundMoveEntityPacketPos& msg) override;
127 virtual void Handle(ProtocolCraft::ClientboundMoveEntityPacketRot& msg) override;
128 virtual void Handle(ProtocolCraft::ClientboundSetEntityDataPacket& msg) override;
129#if PROTOCOL_VERSION == 755 /* 1.17 */
130 virtual void Handle(ProtocolCraft::ClientboundRemoveEntityPacket& msg) override;
131#else
132 virtual void Handle(ProtocolCraft::ClientboundRemoveEntitiesPacket& msg) override;
133#endif
134
135 // Misc stuff
136 virtual void Handle(ProtocolCraft::ClientboundSetTimePacket& msg) override;
137 virtual void Handle(ProtocolCraft::ClientboundRespawnPacket& msg) override;
138
139
140
141 private:
142 // Initialize all the stuff
143 bool Init();
144
145 // Main rendering loop
146 void Run();
147
148 // Callbacks called by glfw
149 static void ResizeCallback(GLFWwindow* window, int width, int height);
150 static void InternalMouseCallback(GLFWwindow *window, double xpos, double ypos);
151
152 // Get GLFW keyboard inputs and pass them to the callback
153 void InternalProcessInput(GLFWwindow *window);
154
155 private:
156 // External modules
157 std::shared_ptr<World> world;
158 std::shared_ptr<InventoryManager> inventory_manager;
159 std::shared_ptr<EntityManager> entity_manager;
160 std::shared_ptr<LocalPlayer> local_player;
161
162 std::array<bool, static_cast<int>(KEY_CODE::NUMBER_OF_KEYS)> is_key_pressed{};
163
166
167 std::thread rendering_thread;// OpenGL thread
168
172
173 double deltaTime;
175
176 GLFWwindow *window;
182
183 // 0.5 is noon, 0 and 1 are midnight
184 std::atomic<float> day_time;
185
186 std::unique_ptr<Shader> my_shader;
187 std::unique_ptr<WorldRenderer> world_renderer;
188 std::unique_ptr<BehaviourRenderer> behaviour_renderer;
189
190 std::function<void(double, double)> MouseCallback;
191 std::function<void(std::array<bool, static_cast<int>(KEY_CODE::NUMBER_OF_KEYS)>, double)> KeyboardCallback;
192 std::mutex callbacks_mutex;
193
194 std::atomic<bool> running;
195
196 std::unordered_set<Position> chunks_to_udpate;
197 std::unordered_set<int> entities_to_update;
198 std::mutex mutex_updating;
199 std::condition_variable condition_update;
200 // Thread used to update the rendered data with current data
202 };
203 } // Renderer
204} // Botcraft
void BehaviourTickChild(const size_t i) const
static std::shared_ptr< RenderingManager > CreateRenderingManagerIfPossible(std::shared_ptr< World > world_, std::shared_ptr< InventoryManager > inventory_manager_, std::shared_ptr< EntityManager > entity_manager_)
std::unordered_set< int > entities_to_update
std::unique_ptr< BehaviourRenderer > behaviour_renderer
void InternalProcessInput(GLFWwindow *window)
void SetKeyboardCallback(std::function< void(std::array< bool, static_cast< int >(KEY_CODE::NUMBER_OF_KEYS)>, double)> callback)
std::function< void(double, double)> MouseCallback
std::shared_ptr< LocalPlayer > local_player
void AddChunkToUpdate(const int x, const int z)
void SetCurrentBehaviourTree(const BaseNode *root) const
static void ResizeCallback(GLFWwindow *window, int width, int height)
std::unique_ptr< WorldRenderer > world_renderer
std::unordered_set< Position > chunks_to_udpate
std::shared_ptr< EntityManager > entity_manager
void RemoveBlackboardValue(const std::string &key) const
static void InternalMouseCallback(GLFWwindow *window, double xpos, double ypos)
void UpdateBlackboardValue(const std::string &key, const std::any &value) const
void SetMouseCallback(std::function< void(double, double)> callback)
void BehaviourEndTick(const bool b) const
virtual void Handle(ProtocolCraft::ClientboundBlockUpdatePacket &msg) override
std::function< void(std::array< bool, static_cast< int >(KEY_CODE::NUMBER_OF_KEYS)>, double)> KeyboardCallback
std::shared_ptr< InventoryManager > inventory_manager
static std::weak_ptr< RenderingManager > current_instance
std::array< bool, static_cast< int >(KEY_CODE::NUMBER_OF_KEYS)> is_key_pressed