使用MIDP2.0开发游戏(7)设计Scheduler
2008-01-05 19:37:18 来源:WEB开发网Scheduler负责以固定的频率执行每一帧,所需的时钟由Clock提供,Scheduler还可以计算每帧所需时间和CPU占用率,以便可能的话动态调整任务。
以下的Scheduler的实现参考自Marshall "Game PRogramming Gems 3"中的C++代码:
package game.engine.core;
public class Scheduler {
// clock:
private Clock clock = new Clock();
// 启动Scheduler:
public void start() {
clock.start();
}// 停止Scheduler:
public void stop() {
clock.stop();
}public int getSystemTime() {
return clock.getSystemTime();
}public int getVirtualTime() {
return clock.getVirtualTime();
}
// 执行完整的一帧:
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("cpu usage: " + (elapsed * 100 / frameLength) + "%");
// cleanup:
System.out.println("-- end execute frame --\n");
}
更多精彩
赞助商链接