Scheduler is responsible for performing each frame at a fixed frequency, and the clock required is provided by Clock, and Scheduler can calculate the time and CPU usage of each frame, so that it may dynamically adjust the task.
The following Scheduler is implemented with reference from the C code in Marshall "Game Programming Gems 3":
Package game.Engine.core;
Public class scheduler {
// clock: private clock clock = new clock (); // Starting Scheduler: public void start () {clock.start ();}
// Stop Scheduler: public void stop () {clock.stop ();
Public int getSystemTime () {return clock.getsystemtime ();
Public int getVirtualTime () {return clock.getvirtualtime ();} // Perform full frame: public void executeframe () {system.out.println ("- start execute frame -"); clock.beginframe () Int Started = clock.getsystemtime (); // do time task: system.out.println ("doing time tasks ..."); try {thread.sleep (500);} catch (interruptedException IE) {} clock .advancetoend (); // do frame task: system.out.println ("doing frame tasks ..."); try {thread.sleep (200);} catch (interruptedException IE {} // do render task: int end = clock.getSystemTime (); int elapsed = end - started; int frameLength = clock.getFrameEnd () - clock.getFrameStart (); System.out.println ( "elapsed:" elapsed ", frame:" Framelength; system.out.println ("CPSED * 100 / framelength) "% "); // cleanup: system.out.println (" - end execute frame - / n ") ; }
Public void Waituntil (int Time) {while (clock.getsystemtime ()
Thread.sleep (1);
}
} catch (InterruptedException IE) {}}
public static void main (String [] args) {Scheduler scheduler = new Scheduler (); scheduler.start (); int time = 1000; do {scheduler.waitUntil (time); time = 1000; scheduler.executeFrame (); } while (scheduler.getsystemtime () <10000);}}
We have not yet true tasks to perform, so I have to use two thread.sleep () to indicate the execution task, which is 500ms and 200ms, respectively, and execute an ExecuteFrame () method per 1S in the main () method, you can see the following output :
- start execute frame - [beginFrame] virtual time = 0, systemTime = 1002doing time tasks ... [advanceToEnd] virtual time = 992doing frame tasks ... elapsed: 701, frame: 992cpu usage: 70% - end execute frame ---- start execute frame - [beginFrame] virtual time = 992, systemTime = 2003doing time tasks ... [advanceToEnd] virtual time = 1993doing frame tasks ... elapsed: 701, frame: 1001cpu usage: 70% - - End Execute Frame --...
The CPU usage is 70%. If the task time is more than 1s, such as 700 ms and 500ms, the virtual time is slow, because the CPU does not process the task within 1s, the output CPU usage is 100% (except for the first calculation) :
- start execute frame - [beginFrame] virtual time = 0, systemTime = 1002doing time tasks ... [advanceToEnd] virtual time = 992doing frame tasks ... elapsed: 1201, frame: 992cpu usage: 121% - end execute Frame -
- start execute frame - [beginFrame] virtual time = 992, systemTime = 2203doing time tasks ... [advanceToEnd] virtual time = 2193doing frame tasks ... elapsed: 1202, frame: 1201cpu usage: 100% - end execute Frame -
- start execute frame - [beginFrame] virtual time = 2193, systemTime = 3415doing time tasks ... [advanceToEnd] virtual time = 3405doing frame tasks ... elapsed: 1202, frame: 1212cpu usage: 99% - end execute Frame - Have some minor error, because the scheduler's own code will take a little time, and currentTimeMillies () can only be accurate to milliseconds, but the user can't feel, only when the CPU is used up to 100% When the game will slow down.
to be continued...