diff options
| author | Mel <einebeere@gmail.com> | 2024-02-12 12:55:11 +0100 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2024-02-12 12:55:11 +0100 |
| commit | d2b5fc5b3bc648afffa42375706429685ac63794 (patch) | |
| tree | a2dfbb241e1d46e5616c5884e5f3d685de2a2cb6 /src/Render.hpp | |
| parent | 588c7e87b7cab270698d43ca5c22d67793ae5fc4 (diff) | |
| download | meowcraft-d2b5fc5b3bc648afffa42375706429685ac63794.tar.zst meowcraft-d2b5fc5b3bc648afffa42375706429685ac63794.zip | |
Split rendering into own thread and sync through render action lists
Diffstat (limited to 'src/Render.hpp')
| -rw-r--r-- | src/Render.hpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/Render.hpp b/src/Render.hpp new file mode 100644 index 0000000..6b7cb65 --- /dev/null +++ b/src/Render.hpp @@ -0,0 +1,88 @@ +#pragma once + +#include <condition_variable> +#include "Defines.hpp" +#include "Common/Sizes.hpp" +#include "GFX/Actions.hpp" +#include "GFX/Camera.hpp" +#include "GFX/Texture.hpp" +#include "GFX/Window.hpp" + +namespace MC { + +class Render { +public: + struct Scene { + GFX::Actions actions; + GFX::Camera camera; + }; + + struct Control { + std::mutex mutex; + std::condition_variable cv; + Bool logic_done = false; + Bool render_done = false; + + Scene scene; + + void send_render_data(Scene new_scene) { + { + std::scoped_lock lock(mutex); + scene = std::move(new_scene); + logic_done = true; + } + cv.notify_one(); + } + + Scene wait_for_render_data() { + Scene new_scene; + { + std::unique_lock lock(mutex); + cv.wait(lock, [&]{ + return logic_done; + }); + new_scene = scene; + logic_done = false; + render_done = false; + } + cv.notify_one(); + return new_scene; + } + + void wait_for_render_finish() { + { + std::unique_lock lock(mutex); + cv.wait(lock, [&]{ + return render_done; + }); + logic_done = false; + render_done = false; + } + cv.notify_one(); + } + + void finish_render() { + { + std::scoped_lock lock(mutex); + render_done = true; + } + cv.notify_one(); + } + }; + + explicit Render(GFX::Window& window, std::shared_ptr<Control> control) + : m_window(window) + , m_control(std::move(control)) {} + + void run(); + +private: + void render_scene(Scene const& actions, GFX::Texture const& texture) const; + static void setup_gl(); + + GFX::Resources m_resources; + GFX::Window& m_window; + std::shared_ptr<Control> m_control; +}; + +} |
