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

Null pointer exception with CCRenderTexture.renderTexture(int width, int height) #51

Closed agGitHub closed 12 years ago

agGitHub commented 12 years ago

I just want to create a CRenderTexture with this simple code:

CCRenderTexture rt = CCRenderTexture.renderTexture(width, width);//width and height have the same values

But it returns a null pointer exception.

The issue occurs in the CCRenderTexture class when the object is initialized:

protected CCRenderTexture(int width, int height) { GL11ExtensionPack egl = (GL11ExtensionPack)CCDirector.gl; egl.glGetIntegerv(GL11ExtensionPack.GL_FRAMEBUFFER_BINDINGOES, oldFBO, 0); ///ETC.... }

Indeed, "CCDirector.gl" is equal to null which induces that "egl" is equal to null => crash on egl.glGetIntegerv(...).

Somebody knows how to fix this bug ? (which seems due to the Android Coco 2D framework)

Thanks !!!

gwennguihal commented 12 years ago

Try to instanciate your CCRenderTexture on the "render" (overrided) method.

agGitHub commented 12 years ago

I am new with Coco2D Android. Can you detail a little bit more what you mean ? Few lines àf code would be useful. Thanks !!

gwennguihal commented 12 years ago

public class MyNode extends CCNode { @Override public void onEnter() { super.onEnter(); CCRenderTexture rt = CCRenderTexture.renderTexture(textureSize, textureSize); //.... } }

Is it more comprehensible ? +

agGitHub commented 12 years ago

Thanks for your answer. Still not working :( (but I am really new with Coco2D Android !!)

Now I have got the following error:

12-14 15:43:47.883: E/AndroidRuntime(5013): java.lang.NullPointerException 12-14 15:43:47.883: E/AndroidRuntime(5013): at org.cocos2d.nodes.CCAtlasNode.calculateMaxItems(CCAtlasNode.java:86) 12-14 15:43:47.883: E/AndroidRuntime(5013): at org.cocos2d.nodes.CCAtlasNode.(CCAtlasNode.java:80) 12-14 15:43:47.883: E/AndroidRuntime(5013): at org.cocos2d.nodes.CCLabelAtlas.(CCLabelAtlas.java:41) 12-14 15:43:47.883: E/AndroidRuntime(5013): at org.cocos2d.nodes.CCLabelAtlas.label(CCLabelAtlas.java:36) 12-14 15:43:47.883: E/AndroidRuntime(5013): at org.cocos2d.nodes.CCDirector.setGLDefaultValues(CCDirector.java:600) 12-14 15:43:47.883: E/AndroidRuntime(5013): at org.cocos2d.nodes.CCDirector.onSurfaceCreated(CCDirector.java:644) 12-14 15:43:47.883: E/AndroidRuntime(5013): at org.cocos2d.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1209)

Here is my (very simple) code:

public class GameLayer extends CCColorLayer { private CCSprite _background;

public static CCScene scene()
{
    CCScene scene = CCScene.node();
    ccColor4B colorB4 = ccColor4B.ccc4(255, 255, 255, 255);
    CCColorLayer layer = new GameLayer(colorB4);
    scene.addChild(layer);

    return scene;
}

public void onEnter() { super.onEnter(); doBackground(); }

protected GameLayer(ccColor4B color) { super(color);

    this.setIsTouchEnabled(true);       
}

private void doBackground() {

ccColor4B myColor = ccColor4B.ccc4(255,255, 255,255);
ccColor4F bgColor = ccColor4F.ccc4FFromccc4B(myColor);

    _background =  spriteWithColor(bgColor, 512);    
    CGSize winSize = CCDirector.sharedDirector().winSize();
         _background.setPosition(winSize.width/2, winSize.height/2);        
    addChild(_background,-1);    
}

private CCSprite spriteWithColor(ccColor4F bgColor, int textureSize) {

  CCRenderTexture rt = CCRenderTexture.renderTexture(textureSize, textureSize);
  rt.clear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);

      rt.end();

    return CCSprite.sprite((rt.getSprite()).getTexture());   
}

}

Thanks a lot for your help and sorry for my stupid questions... (my 2nd day with Coco 2D...)

gwennguihal commented 12 years ago

Without CCRenderTexture, does your code work ? Do you use the jar file or sources ? If sources, did you set "fps_images.png" in your asset folder ?

agGitHub commented 12 years ago

Thanks it works !!!!! You are a genius !! I can't believe it, so many hours lost just because of this stupid file...

I use the sources from ZhouWeikuan (not a jar). Is it the best choice ?

Thanks again for everything !