Browse Source

make firing a little fuzzy to improve correct firings

Odilitime 5 years ago
parent
commit
c6784e1029
  1. 18
      src/scheduler.cpp

18
src/scheduler.cpp

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
#include <GLFW/glfw3.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
std::shared_ptr<timer_handle> Scheduler::setTimeout(std::function<void()> callback, unsigned long delay) {
double now = glfwGetTime() * 1000;
@ -56,15 +57,28 @@ bool Scheduler::fireTimer(std::shared_ptr<timer_handle> timer, double now) { @@ -56,15 +57,28 @@ bool Scheduler::fireTimer(std::shared_ptr<timer_handle> timer, double now) {
void Scheduler::fireTimers() {
double now = glfwGetTime() * 1000;
size_t unfired = 0;
size_t next = INT_MAX;
for(std::vector<std::shared_ptr<timer_handle>>::iterator it=this->timers.begin(); it!=this->timers.end(); ++it) {
// if it's the timer time to fire
if (it->get()->nextAt < now) {
size_t left = abs(static_cast<int>(it->get()->nextAt - now)); // maybe remove the ABS
if (left <= 10 || it->get()->nextAt < now) {
// fire it, do we need to clean it up
if (this->fireTimer(*it, now)) {
// clena up
// clean up
it = this->timers.erase(it);
if (it == this->timers.end()) break;
}
} else {
//std::cout << "left" << left << std::endl;
next = std::min(left, next);
unfired++;
}
}
if (unfired) {
if (next < 10) {
std::cout << "There are " << unfired << "/" << this->timers.size() << " unfired timers left, next at " << (int)next << std::endl;
std::cout << "Scheduler::fireTimers - failed" << std::endl;
}
}
}

Loading…
Cancel
Save