diff --git a/linux/main.cpp b/linux/main.cpp index 1576a5be7..1e6c409a0 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -3,9 +3,18 @@ #include #include #include "settings.hpp" +#include +#include +#include #include +std::list> futures; +std::list>::iterator f_it; +std::list> callbacks; +std::list>::iterator c_it; +std::chrono::milliseconds zero (0); + class MapView { public: MapView() : @@ -150,6 +159,22 @@ public: int run() { while (!glfwWindowShouldClose(window)) { + + f_it = futures.begin(); + c_it = callbacks.begin(); + + while (f_it != futures.end()) { + if (f_it->wait_for(zero) == std::future_status::ready) { + std::function cb = *c_it; + futures.erase(f_it++); + callbacks.erase(c_it++); + cb(); + } else { + f_it++; + c_it++; + } + } + if (dirty) { try { dirty = render(); @@ -160,7 +185,7 @@ public: fps(); } - if (dirty) { + if (dirty || futures.size()) { glfwPollEvents(); } else { glfwWaitEvents(); @@ -225,7 +250,7 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *use return size * nmemb; } -void request_http(std::string url, std::function func, std::function cb) { +void request_http_async(std::string url, std::function func) { Response res; @@ -244,7 +269,11 @@ void request_http(std::string url, std::function func, std::fun } func(res); - cb(); +} + +void request_http(std::string url, std::function func, std::function cb) { + futures.emplace_back(std::async(std::launch::async, request_http_async, url, func)); + callbacks.push_back(cb); }