Skip to content

Commit

Permalink
refactor task management
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 16, 2024
1 parent ea5e873 commit 304a69b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ set(privateHeaders
"threepp/renderers/gl/UniformUtils.hpp"

"threepp/utils/RegexUtil.hpp"
"threepp/utils/TaskManager.hpp"

)

Expand Down
26 changes: 5 additions & 21 deletions src/threepp/canvas/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,12 +23,6 @@ using namespace threepp;

namespace {

typedef std::pair<std::function<void()>, float> Task;

struct CustomComparator {
bool operator()(const Task& l, const Task& r) const { return l.second > r.second; }
};

#if EMSCRIPTEN
struct FunctionWrapper {
std::function<void()> loopFunction;
Expand Down Expand Up @@ -173,7 +168,8 @@ struct Canvas::Impl {
bool close_{false};
bool exitOnKeyEscape_;

std::priority_queue<Task, std::vector<Task>, CustomComparator> tasks_;
utils::TaskManager taskManager;

std::optional<std::function<void(WindowSize)>> resizeListener;

explicit Impl(Canvas& scope, const Canvas::Parameters& params)
Expand Down Expand Up @@ -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<void()>& f) {

if (close_ || glfwWindowShouldClose(window)) {
return false;
}

handleTasks();
taskManager.handleTasks(static_cast<float>(glfwGetTime()));

f();

Expand All @@ -290,7 +274,7 @@ struct Canvas::Impl {
}

void invokeLater(const std::function<void()>& f, float t) {
tasks_.emplace(f, static_cast<float>(glfwGetTime()) + t);
taskManager.invokeLater(f, static_cast<float>(glfwGetTime()) + t);
}

void close() {
Expand Down
43 changes: 43 additions & 0 deletions src/threepp/utils/TaskManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#ifndef THREEPP_TASKMANAGER_HPP
#define THREEPP_TASKMANAGER_HPP

#include <functional>
#include <queue>
#include <utility>

namespace threepp::utils {

using Task = std::pair<std::function<void()>, 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<void()>& 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<Task, std::vector<Task>, CustomComparator> tasks_;
};

}// namespace threepp::utils

#endif//THREEPP_TASKMANAGER_HPP

0 comments on commit 304a69b

Please sign in to comment.