• cocos2d-x在android真机上设置帧率无效的问题


    通过setAnimationInterval设置帧频时,发现在android下没有效果的

    在Cocos2dxRenderer .java文件里面找到了onDrawFrame这个函数。里面有句注释 :

    FPS controlling algorithm is not accurate, and it will slow down FPS on some devices. So comment FPS controlling code.

    这个函数里的大部分代码都被注释掉了

     1 public void onDrawFrame(final GL10 gl) {
     2 
     3 /*
     4  * FPS controlling algorithm is not accurate, and it will slow down FPS
     5  * on some devices. So comment FPS controlling code.
     6  */
     7 
     8  /*
     9  final long nowInNanoSeconds = System.nanoTime();
    10  final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds; */
    11 
    12  // should render a frame when onDrawFrame() is called or there is a
    13  // "ghost" 
    14 Cocos2dxRenderer.nativeRender();
    15 
    16  /* 
    17 // fps controlling if (interval < Cocos2dxRenderer.sAnimationInterval) { try { 
    18 // because we render it before, so we should sleep twice time interval
    19 Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) /Cocos2dxRenderer.NANOSECONDSPERMICROSECOND); 
    20 } catch (final Exception e) { }
    21  }
    22 
    23 this.mLastTickInNanoSeconds = nowInNanoSeconds;
    24 */
    25 
    26 }

    经过google后暂时找到了解决方案,是否有问题需要待测试:

     1 private long renderingElapsedTime;
     2 
     3 @Override
     4 public void onDrawFrame(final GL10 gl) {
     5     /*
     6      * FPS controlling algorithm is not accurate, and it will slow down FPS
     7      * on some devices. So comment FPS controlling code.
     8      */
     9 
    10     try {
    11         if (renderingElapsedTime< Cocos2dxRenderer.sAnimationInterval) {
    12             Thread.sleep((Cocos2dxRenderer.sAnimationInterval - renderingElapsedTime) / NANOSECONDSPERMICROSECOND);
    13         }
    14     } catch (InterruptedException e) {
    15         e.printStackTrace();
    16     }
    17 
    18     // Get the timestamp when rendering started
    19     long renderingStartedTimestamp = System.nanoTime();
    20 
    21     // should render a frame when onDrawFrame() is called or there is a
    22     // "ghost" 
    23     Cocos2dxRenderer.nativeRender();
    24 
    25     // Calculate the elapsed time during rendering
    26     renderingElapsedTime = (System.nanoTime() - renderingStartedTimestamp);
    27 }
  • 相关阅读:
    占位
    JavaScript(13):用jQuery实现复选框的全、反、取选
    JavaScript(12):jQuery(1)
    JavaScript(11):词法分析
    JavaScript(10):行为(HTML)、结构(CSS)、样式(JS)相分离的页面
    JavaScript(9):非常规form表单提交、弹出框、URL和刷新、定时器
    JavaScript(8):搜索框示例实现、样式操作、属性操作、标签操作
    JavaScript(7):DOM直接查找与间接查找的补充
    JavaScript(6):作用域、面向对象
    开篇杂谈
  • 原文地址:https://www.cnblogs.com/SmileYG/p/3418742.html
Copyright © 2020-2023  润新知