使用MIDP2.0开发游戏(7)设计Scheduler

祖山汉子

祖山汉子

2016-02-19 13:00

下面是个简单易学的使用MIDP2.0开发游戏(7)设计Scheduler教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

  Scheduler负责以固定的频率执行每一帧,所需的时钟由Clock提供,Scheduler还可以计算每帧所需时间和CPU占用率,以便可能的话动态调整任务。

  以下的Scheduler的实现参考自Marshall "Game Programming Gems 3"中的C++代码:

  package game.engine.core;

  public class Scheduler {

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

      // clock:
      private Clock clock = new Clock();
      
      // 启动Scheduler:
      public void start() {
          clock.start();
      }

      // 停止Scheduler:
      public void stop() {
          clock.stop();
      }

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

      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 --");
      }

  

展开更多 50%)
分享

猜你喜欢

使用MIDP2.0开发游戏(7)设计Scheduler

编程语言 网络编程
使用MIDP2.0开发游戏(7)设计Scheduler

使用MIDP2.0开发游戏(6)设计Clock

编程语言 网络编程
使用MIDP2.0开发游戏(6)设计Clock

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

使用MIDP2.0开发游戏(5)游戏内核

编程语言 网络编程
使用MIDP2.0开发游戏(5)游戏内核

使用MIDP2.0开发游戏(2)使用Sprite

编程语言 网络编程
使用MIDP2.0开发游戏(2)使用Sprite

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

使用MIDP2.0开发游戏(3)添加背景和前景

编程语言 网络编程
使用MIDP2.0开发游戏(3)添加背景和前景

开发经验谈:贪吃蛇游戏的MIDP实现核心

编程语言 网络编程
开发经验谈:贪吃蛇游戏的MIDP实现核心

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

win10桌面图标变大怎么办

win10桌面图标变大怎么办

经典Java开发工具介绍(3):WebSphere Studio with VisualAge

经典Java开发工具介绍(3):WebSphere Studio with VisualAge
下拉加载更多内容 ↓