Botcraft 1.21.11
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 const unsigned int section_height_ = 16
68 );
69
70 static std::weak_ptr<RenderingManager> current_instance;
71 static std::mutex instance_mutex;
72
73 public:
75
76 // Set a flag to terminate the rendering loop after the current frame
77 void Close();
78
79 // Set mouse and keyboard callbacks to handle user inputs
80 void SetMouseCallback(std::function<void(double, double)> callback);
81 void SetKeyboardCallback(std::function<void(std::array<bool, static_cast<int>(KEY_CODE::NUMBER_OF_KEYS)>, double)> callback);
82 void AddChunkToUpdate(const int x, const int z);
83 void AddEntityToUpdate(const int id);
84
85 // Set world renderer's camera position and orientation
86 void SetPosOrientation(const double x_, const double y_, const double z_, const float yaw_, const float pitch_);
87
88 void SetCurrentBehaviourTree(const BaseNode* root) const;
89 void ResetBehaviourState() const;
90 void BehaviourStartTick() const;
91 void BehaviourEndTick(const bool b) const;
92 void BehaviourTickChild(const size_t i) const;
93 bool IsBehaviourGUIPaused() const;
94
95 void ResetBlackboard() const;
96 void UpdateBlackboardValue(const std::string& key, const std::any& value) const;
97 void RemoveBlackboardValue(const std::string& key) const;
98
99
100 protected:
102
103
104 // Chunk stuff
105 virtual void Handle(ProtocolCraft::ClientboundBlockUpdatePacket& msg) override;
108#if PROTOCOL_VERSION < 757 /* < 1.18 */
109 virtual void Handle(ProtocolCraft::ClientboundLevelChunkPacket& msg) override;
110#else
112#endif
113
114 // Entity stuff
115 virtual void Handle(ProtocolCraft::ClientboundAddEntityPacket& msg) override;
116#if PROTOCOL_VERSION < 759 /* < 1.19 */
117 virtual void Handle(ProtocolCraft::ClientboundAddMobPacket& msg) override;
118#endif
119#if PROTOCOL_VERSION < 721 /* < 1.16 */
120 virtual void Handle(ProtocolCraft::ClientboundAddGlobalEntityPacket& msg) override;
121#endif
122#if PROTOCOL_VERSION < 764 /* < 1.20.2 */
123 virtual void Handle(ProtocolCraft::ClientboundAddPlayerPacket& msg) override;
124#endif
125 virtual void Handle(ProtocolCraft::ClientboundTeleportEntityPacket& msg) override;
126#if PROTOCOL_VERSION < 755 /* < 1.17 */
127 virtual void Handle(ProtocolCraft::ClientboundMoveEntityPacket& msg) override;
128#endif
129 virtual void Handle(ProtocolCraft::ClientboundMoveEntityPacketPos& msg) override;
131 virtual void Handle(ProtocolCraft::ClientboundMoveEntityPacketRot& msg) override;
132 virtual void Handle(ProtocolCraft::ClientboundSetEntityDataPacket& msg) override;
133#if PROTOCOL_VERSION == 755 /* 1.17 */
134 virtual void Handle(ProtocolCraft::ClientboundRemoveEntityPacket& msg) override;
135#else
136 virtual void Handle(ProtocolCraft::ClientboundRemoveEntitiesPacket& msg) override;
137#endif
138
139 // Misc stuff
140 virtual void Handle(ProtocolCraft::ClientboundSetTimePacket& msg) override;
141 virtual void Handle(ProtocolCraft::ClientboundRespawnPacket& msg) override;
142
143
144
145 private:
146 // Initialize all the stuff
147 bool Init();
148
149 // Main rendering loop
150 void Run();
151
152 // Callbacks called by glfw
153 static void ResizeCallback(GLFWwindow* window, int width, int height);
154 static void InternalMouseCallback(GLFWwindow *window, double xpos, double ypos);
155
156 // Get GLFW keyboard inputs and pass them to the callback
157 void InternalProcessInput(GLFWwindow *window);
158
159 private:
160 // External modules
161 std::shared_ptr<World> world;
162 std::shared_ptr<InventoryManager> inventory_manager;
163 std::shared_ptr<EntityManager> entity_manager;
164 std::shared_ptr<LocalPlayer> local_player;
165
166 std::array<bool, static_cast<int>(KEY_CODE::NUMBER_OF_KEYS)> is_key_pressed{};
167
170
171 std::thread rendering_thread;// OpenGL thread
172
176
177 double deltaTime;
179
180 GLFWwindow *window;
186
187 // 0.5 is noon, 0 and 1 are midnight
188 float day_time;
189
190 std::unique_ptr<Shader> my_shader;
191 std::unique_ptr<WorldRenderer> world_renderer;
192 std::unique_ptr<BehaviourRenderer> behaviour_renderer;
193
194 std::function<void(double, double)> MouseCallback;
195 std::function<void(std::array<bool, static_cast<int>(KEY_CODE::NUMBER_OF_KEYS)>, double)> KeyboardCallback;
196
198
199 std::unordered_set<Position> chunks_to_udpate;
200 std::unordered_set<int> entities_to_update;
201 std::mutex mutex_updating;
202 std::condition_variable condition_update;
203 // Thread used to update the rendered data with current data
205 };
206 } // Renderer
207} // 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
void SetPosOrientation(const double x_, const double y_, const double z_, const float yaw_, const float pitch_)