From 304a69b676239e6074d857c841b50435a42e51eb Mon Sep 17 00:00:00 2001 From: Lars Ivar Hatledal Date: Sat, 16 Mar 2024 20:32:30 +0100 Subject: [PATCH] refactor task management --- src/CMakeLists.txt | 1 + src/threepp/canvas/Canvas.cpp | 26 ++++--------------- src/threepp/utils/TaskManager.hpp | 43 +++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 src/threepp/utils/TaskManager.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 05665a6f..e2a2ece4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -233,6 +233,7 @@ set(privateHeaders "threepp/renderers/gl/UniformUtils.hpp" "threepp/utils/RegexUtil.hpp" + "threepp/utils/TaskManager.hpp" ) diff --git a/src/threepp/canvas/Canvas.cpp b/src/threepp/canvas/Canvas.cpp index 53a9505c..3bbc7d36 100644 --- a/src/threepp/canvas/Canvas.cpp +++ b/src/threepp/canvas/Canvas.cpp @@ -4,6 +4,7 @@ #include "threepp/favicon.hpp" #include "threepp/loaders/ImageLoader.hpp" #include "threepp/utils/StringUtils.hpp" +#include "threepp/utils/TaskManager.hpp" #ifndef EMSCRIPTEN #include "threepp/utils/LoadGlad.hpp" @@ -22,12 +23,6 @@ using namespace threepp; namespace { - typedef std::pair, float> Task; - - struct CustomComparator { - bool operator()(const Task& l, const Task& r) const { return l.second > r.second; } - }; - #if EMSCRIPTEN struct FunctionWrapper { std::function loopFunction; @@ -173,7 +168,8 @@ struct Canvas::Impl { bool close_{false}; bool exitOnKeyEscape_; - std::priority_queue, CustomComparator> tasks_; + utils::TaskManager taskManager; + std::optional> resizeListener; explicit Impl(Canvas& scope, const Canvas::Parameters& params) @@ -248,25 +244,13 @@ struct Canvas::Impl { glfwSetWindowSize(window, size.width, size.height); } - inline void handleTasks() { - while (!tasks_.empty()) { - auto& task = tasks_.top(); - if (task.second < glfwGetTime()) { - task.first(); - tasks_.pop(); - } else { - break; - } - } - } - bool animateOnce(const std::function& f) { if (close_ || glfwWindowShouldClose(window)) { return false; } - handleTasks(); + taskManager.handleTasks(static_cast(glfwGetTime())); f(); @@ -290,7 +274,7 @@ struct Canvas::Impl { } void invokeLater(const std::function& f, float t) { - tasks_.emplace(f, static_cast(glfwGetTime()) + t); + taskManager.invokeLater(f, static_cast(glfwGetTime()) + t); } void close() { diff --git a/src/threepp/utils/TaskManager.hpp b/src/threepp/utils/TaskManager.hpp new file mode 100644 index 00000000..6dbdf595 --- /dev/null +++ b/src/threepp/utils/TaskManager.hpp @@ -0,0 +1,43 @@ + +#ifndef THREEPP_TASKMANAGER_HPP +#define THREEPP_TASKMANAGER_HPP + +#include +#include +#include + +namespace threepp::utils { + + using Task = std::pair, float>; + + class TaskManager { + + public: + inline void handleTasks(float currentTime) { + while (!tasks_.empty()) { + auto& task = tasks_.top(); + if (task.second < currentTime) { + task.first(); + tasks_.pop(); + } else { + break; + } + } + } + + void invokeLater(const std::function& f, float tNow, float tPassed = 0) { + + tasks_.emplace(f, tNow + tPassed); + } + + private: + struct CustomComparator { + bool operator()(const Task& l, const Task& r) const { return l.second > r.second; } + }; + + std::priority_queue, CustomComparator> tasks_; + }; + +}// namespace threepp::utils + +#endif//THREEPP_TASKMANAGER_HPP