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

本文讨论了在Android平台上使用Cocos2d-x引擎遇到的帧频控制不准确的问题,并提供了调整代码实现更稳定帧率的方案。包括通过调整渲染间隔和使用额外的计时逻辑来优化渲染效率,确保游戏或应用在不同设备上都能保持流畅运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过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 }

 

转载于:https://www.cnblogs.com/SmileYG/p/3418742.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值