Original Post
edit: Solved why it was so slow, read after original post to see.
All of the systems in my engine (graphics, sound, physics etc) are going to be encapsulated in tasks, which are then called by a singleton Kernel object. All each task has to specify is its priority, and the Kernel is then supposed to give each task how much time it's supposed to get and whatnot. Obviously, it's not going to be preemptive so each task is going to finish when it will.
When the Task is updated, it's given the amount of time that has passed since the last time it was called, to make the physics and whatnot time-independent.
Kernel::Run()
while( TaskList.size() > 0 ) {
// for each task
for( iterator it, it != end(), it++ ) {
// check if we have taken too much time this frame
if( ElapsedTime() > (1/60) ) {
// if so, check whether we can skip current task
if( (*it)->CanSkip() ) continue;
// if not, update it
else (*it)->Run( ElapsedTime() - LastTime() );
}
// otherwise we have plenty of time
(*it)->Run( ElapsedTime() - LastTime() );
}
}
The problem with this algorithm is that it checks the elapsed time far too often; for each task, it checks it once to see whether we can maintain a constant framerate, then when it updates the task it checks it another time to see what time it is now. LastTime() has to be provided for each task, so I have a map<Task*,double>.
It's nice to be able to give every task the amount of time elapsed, but so far it's proved incredibly slow. A simple task whose only purpose is to output 1/DeltaTime (in other words, the FPS) says it's about 61, which is completely unacceptable. Is there a more elegant way for me to implement this?
edit: As it turns out, in my original version I was calling system( "cls" ) each time. Removing it boosted framerate to about 400. Then changing the function to output the FPS only every second boosted the FPS to about 40k, which is what I thought it would be. It seems that calling std::wcout so often isn't very efficient.
[edited by - psykr on January 29, 2004 1:30:47 PM]