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