Cocos2d-x setAnimationInterval 在 Android 上不起作用
Cocos2d-x setAnimationInterval doesn't work on Android
我尝试在Cocos2d-x中使用以下代码设置应用程序中的最大FPS:
CCDirector::sharedDirector()->setAnimationInterval(1.0 / 30);
它在iOS上工作,但当我在三个Android设备上测试它时,它被忽略了,并以标准间隔(1/60)渲染帧。
如何使用cocos2d-x正确设置Android上的最大FPS ?
所以我实际上已经设法实现了它。你必须编辑Cocos2dxRenderer.java文件,然后清理和重建Cocos2d-x。
代码如下:
public void onDrawFrame(final GL10 gl) {
// FPS controlling algorithm is not accurate, and it will slow down FPS
// on some devices. So comment FPS controlling code.
try {
if (loopRuntime < 40) {
Log.wtf("RENDERER", "Sleeping for == " + (40 - loopRuntime));
Thread.sleep(40 - loopRuntime);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
//final long nowInNanoSeconds = System.nanoTime();
//final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds;
loopStart = System.currentTimeMillis();
// should render a frame when onDrawFrame() is called or there is a
// "ghost"
Cocos2dxRenderer.nativeRender();
loopEnd = System.currentTimeMillis();
loopRuntime = (loopEnd - loopStart);
Log.wtf("RENDERER", "loopRunTime == " + loopRuntime);
// fps controlling
/*if (interval < Cocos2dxRenderer.sAnimationInterval) {
try {
// because we render it before, so we should sleep twice time interval
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
} catch (final Exception e) {
}
}*/
//this.mLastTickInNanoSeconds = nowInNanoSeconds;
}
奇怪的是,当我取消fps控制部分的注释时,它什么也没做,当我写我的版本时,它做了…无论如何,这里的"魔法"值40给出了大约35fps,但你当然可以很容易地改变它,使其与setAnimationInterval();
传递的值一起工作。编辑:我将loopStart行移动到sleep ->之后,sleep时间不应该包含在loopTime中。
这是一件很奇怪的事情。但是如果你有两个场景一起播放,并且你将两个场景都设置为30 fps,尽管pDirector->getAnimationInterval()
返回1/30的间隔,但你仍然可以获得60+ fps。
代码运行正常…
found from http://discuss.cocos2d-x.org/t/setanimationinterval-does-nothing-on-android/6419/4
private long renderingElapsedTime;
@Override
public void onDrawFrame(final GL10 gl)
{
/*
* FPS controlling algorithm is not accurate, and it will slow down FPS
* on some devices. So comment FPS controlling code.
*/
try {
if (renderingElapsedTime * NANOSECONDSPERMICROSECOND < Cocos2dxRenderer.sAnimationInterval) {
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - renderingElapsedTime * NANOSECONDSPERMICROSECOND) / NANOSECONDSPERMICROSECOND);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
/*
final long nowInNanoSeconds = System.nanoTime();
final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds;
*/
// Get the timestamp when rendering started
long renderingStartedTimestamp = System.currentTimeMillis();
// should render a frame when onDrawFrame() is called or there is a
// "ghost"
Cocos2dxRenderer.nativeRender();
// Calculate the elapsed time during rendering
renderingElapsedTime = (System.currentTimeMillis() - renderingStartedTimestamp);
/*
// fps controlling
if (interval < Cocos2dxRenderer.sAnimationInterval) {
try {
// because we render it before, so we should sleep twice time interval
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
} catch (final Exception e) {
}
}
this.mLastTickInNanoSeconds = nowInNanoSeconds;
*/
}
希望有所帮助
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- QT QML:使用.qrc文件将SQLite数据库部署到Android不起作用
- QNetworkReply https API 在 Android 设备上不起作用 - QT
- Android.mk 添加LOCAL_CPPFLAGS不起作用
- cocos2d android编译时链接不起作用
- Android Studio+NDK:C++编辑器不起作用
- Android NDK 中的某些 c++ 包含文件不起作用
- opencv中的Core.add方法在Android上不起作用
- C++OpenCV imread在Android中不起作用
- 使用编译的lua脚本的Cocos2d-x在Android上不起作用
- Cocos2d-x setAnimationInterval 在 Android 上不起作用