ZhouWeikuan / cocos2d

cocos2d for android, based on cocos2d-android-0.82, and now ported from cocos2d-iphone 0.99.4. The googlecode address is here: http://code.google.com/p/cocos2d-android-1/ . There are several demos to watch.
610 stars 291 forks source link

Framerate decreases with SceneTransition #42

Closed gwennguihal closed 12 years ago

gwennguihal commented 12 years ago

I notice a problem :

When I start just one CCScene, framerate is 60fps, but if I change of scene (2 or more times) with transition, framerate decreases at 30fps. My scenes are very simple :

SplashScreenLayer Scene and Home Scene (the sames for test) :

public static CCScene scene() { CCScene s = CCScene.node(); SplashScreenLayer layer = new SplashScreenLayer(); s.addChild(layer); return s; }

public SplashScreenLayer()
{
    super(ccColor4B.ccc4(0, 0, 0, 255));

    CCTexture2D texture = CCTextureCache.sharedTextureCache().addImage("splashscreen.png");
    CCSprite splashScreen = CCSprite.sprite(texture,CGRect.make(0, 0, 800, 480));
    splashScreen.setPosition(
        ( CCDirector.sharedDirector().winSize().width) / 2,
        ( CCDirector.sharedDirector().winSize().height) / 2
    );

    addChild(splashScreen);

    this.schedule("update");
}

public void update(float dt)
{
    _timeEllapsed += dt;

    C.trace(_timeEllapsed);

    if (_timeEllapsed > _kEndTime)
    {
        this.unschedule("update");
        GameManager.sharedGameManager().changeScene(Constants.SCENE_HOME);
    }
}

GameManager.sharedGameManager().changeScene :

public void changeScene(int sceneID) { CCScene newScene; CCScene oldScene;

    if (CCDirector.sharedDirector().getRunningScene() != null) oldScene = CCDirector.sharedDirector().getRunningScene(); 

    switch (sceneID)
    {
        case Constants.SCENE_SPASH_SCREEN:
            newScene = SplashScreenLayer.scene();
            break;
        case Constants.SCENE_HOME:
            newScene = HomeLayer.scene();
            break;
        default:
            newScene = HomeLayer.scene();
            break;
    }

    if (CCDirector.sharedDirector().getRunningScene() == null)
    {
        CCDirector.sharedDirector().runWithScene(newScene);
    } else
    {
        //CCDirector.sharedDirector().replaceScene(newScene);
        //oldScene = null;
        CCDirector.sharedDirector().replaceScene(CCFadeTransition.transition(_kTransitionDuration, newScene, ccColor3B.ccc3(0, 0, 0)));
    }
}

myrddin (sorry for my english)

dustinanon commented 12 years ago

Do you mean it changes to 30fps permanently, or just during the transition?

If it's just during the transition, then I think it's due to calling Math.pow every tick in the easing functions used during the transition.

Math.pow can take a lot of resources because of the accuracy required by java's StrictMath. You might try changing to a function that just approximates pow

What I did was replace the call to Math.pow with this:

        /** double style approximation of pow 
         *  using IEEE Double exploitation
         */

        final int x = (int) (Double.doubleToLongBits((double)t) >> 32);
        final int y = (int) (((double) 1/rate) * (x - 1072632447) + 1072632447);
        float result = (float) Double.longBitsToDouble(((long) y) << 32);
gwennguihal commented 12 years ago

Hello,

No, it's not just during transition but after. But I mean it's because my texture which is big (800x480) : I did some tests and the framerate dont' decrease, it stays at 43fps all the time. Thanks for the Math.pow tricks :)