void CCobEngine::Tick(int deltaTime) { SCOPED_TIMER("Scripts"); GCurrentTime += deltaTime; #if COB_DEBUG > 0 logOutput.Print("----"); #endif // Advance all running threads for (std::list<CCobThread *>::iterator i = running.begin(); i != running.end(); ++i) { //logOutput.Print("Now 1running %d: %s", GCurrentTime, (*i)->GetName().c_str()); #ifdef _CONSOLE printf("----\n"); #endif TickThread(deltaTime, *i); } // A thread can never go from running->running, so clear the list // note: if preemption was to be added, this would no longer hold // however, ta scripts can not run preemptively anyway since there // isn't any synchronization methods available running.clear(); // The threads that just ran may have added new threads that should run next tick for (std::list<CCobThread *>::iterator i = wantToRun.begin(); i != wantToRun.end(); ++i) { running.push_front(*i); } wantToRun.clear(); //Check on the sleeping threads if (!sleeping.empty()) { CCobThread *cur = sleeping.top(); while ((cur != NULL) && (cur->GetWakeTime() < GCurrentTime)) { // Start with removing the executing thread from the queue sleeping.pop(); //Run forward again. This can quite possibly readd the thread to the sleeping array again //But it will not interfere since it is guaranteed to sleep > 0 ms //logOutput.Print("Now 2running %d: %s", GCurrentTime, cur->GetName().c_str()); #ifdef _CONSOLE printf("+++\n"); #endif if (cur->state == CCobThread::Sleep) { cur->state = CCobThread::Run; TickThread(deltaTime, cur); } else if (cur->state == CCobThread::Dead) { delete cur; } else { logOutput.Print("CobError: Sleeping thread strange state %d", cur->state); } if (!sleeping.empty()) cur = sleeping.top(); else cur = NULL; } } }
void CCobEngine::Tick(int deltaTime) { START_TIME_PROFILE GCurrentTime += deltaTime; #if COB_DEBUG > 0 logOutput.Print("----"); #endif //Advance all running threads for (list<CCobThread *>::iterator i = running.begin(); i != running.end(); ++i) { //logOutput.Print("Now 1running %d: %s", GCurrentTime, (*i)->GetName().c_str()); #ifdef _CONSOLE printf("----\n"); #endif int res = (*i)->Tick(deltaTime); (*i)->CommitAnims(deltaTime); if (res == -1) { delete *i; } } //A thread can never go from running->running, so clear the list //note: if preemption was to be added, this would no longer hold //however, ta scripts can not run preemptively anyway since there isn't any synchronization methods available running.clear(); //The threads that just ran may have added new threads that should run next tick for (list<CCobThread *>::iterator i = wantToRun.begin(); i != wantToRun.end(); ++i) { running.push_front(*i); } wantToRun.clear(); //Check on the sleeping threads if (sleeping.size() > 0) { CCobThread *cur = sleeping.top(); while ((cur != NULL) && (cur->GetWakeTime() < GCurrentTime)) { // Start with removing the executing thread from the queue sleeping.pop(); //Run forward again. This can quite possibly readd the thread to the sleeping array again //But it will not interfere since it is guaranteed to sleep > 0 ms //logOutput.Print("Now 2running %d: %s", GCurrentTime, cur->GetName().c_str()); #ifdef _CONSOLE printf("+++\n"); #endif if (cur->state == CCobThread::Sleep) { cur->state = CCobThread::Run; int res = cur->Tick(deltaTime); cur->CommitAnims(deltaTime); if (res == -1) delete cur; } else if (cur->state == CCobThread::Dead) { delete cur; } else { logOutput.Print("CobError: Sleeping thread strange state %d", cur->state); } if (sleeping.size() > 0) cur = sleeping.top(); else cur = NULL; } } //Tick all instances that have registered themselves as animating list<CCobInstance *>::iterator it = animating.begin(); list<CCobInstance *>::iterator curit; while (it != animating.end()) { curit = it++; if ((*curit)->Tick(deltaTime) == -1) animating.erase(curit); } END_TIME_PROFILE("Scripts"); }