mirror of https://github.com/axmolengine/axmol.git
Merge pull request #7078 from reckhou/v3
Optimize FPS control algorithm under Android.
This commit is contained in:
commit
191f7a57e1
|
@ -68,7 +68,16 @@ int Application::run()
|
||||||
|
|
||||||
void Application::setAnimationInterval(double interval)
|
void Application::setAnimationInterval(double interval)
|
||||||
{
|
{
|
||||||
// NYI
|
JniMethodInfo methodInfo;
|
||||||
|
if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxRenderer", "setAnimationInterval",
|
||||||
|
"(D)V"))
|
||||||
|
{
|
||||||
|
CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, interval);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -82,31 +82,30 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
|
||||||
@Override
|
@Override
|
||||||
public void onDrawFrame(final GL10 gl) {
|
public void onDrawFrame(final GL10 gl) {
|
||||||
/*
|
/*
|
||||||
* FPS controlling algorithm is not accurate, and it will slow down FPS
|
* No need to use algorithm in default(60 FPS) situation,
|
||||||
* on some devices. So comment FPS controlling code.
|
* since onDrawFrame() was called by system 60 times per second by default.
|
||||||
*/
|
*/
|
||||||
|
if (sAnimationInterval <= 1.0 / 60 * Cocos2dxRenderer.NANOSECONDSPERSECOND) {
|
||||||
|
Cocos2dxRenderer.nativeRender();
|
||||||
|
} else {
|
||||||
|
final long now = System.nanoTime();
|
||||||
|
final long interval = now - this.mLastTickInNanoSeconds;
|
||||||
|
|
||||||
/*
|
if (interval < Cocos2dxRenderer.sAnimationInterval) {
|
||||||
final long nowInNanoSeconds = System.nanoTime();
|
try {
|
||||||
final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds;
|
Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND);
|
||||||
*/
|
} catch (final Exception e) {
|
||||||
|
}
|
||||||
// should render a frame when onDrawFrame() is called or there is a
|
|
||||||
// "ghost"
|
|
||||||
Cocos2dxRenderer.nativeRender();
|
|
||||||
|
|
||||||
/*
|
|
||||||
// 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) {
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* Render time MUST be counted in, or the FPS will slower than appointed.
|
||||||
|
*/
|
||||||
|
final long renderStart = System.nanoTime();
|
||||||
|
Cocos2dxRenderer.nativeRender();
|
||||||
|
final long renderEnd = System.nanoTime();
|
||||||
|
final long renderInterval = renderEnd - renderStart;
|
||||||
|
this.mLastTickInNanoSeconds = renderEnd - renderInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.mLastTickInNanoSeconds = nowInNanoSeconds;
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===========================================================
|
// ===========================================================
|
||||||
|
|
Loading…
Reference in New Issue