ValeriyLapin / beginning-android-games

Automatically exported from code.google.com/p/beginning-android-games
0 stars 0 forks source link

SpriteBatcher.java not so strong #51

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,

I think the class SpriteBatcher used in the OpenGl ES 2D framework is not so 
strong because if you call SpriteBatcher.endBatch() method without introducing 
any sprite (numSprite=0) will receive an IllegalArgumentException.

To avoid this you can check if numSprites == 0 into endBatch() method or simply 
catch the exception. 

The code would look like this:

public void endBatch(){
        try{
            vertices.setVertices(verticesBuffer, 0, bufferIndices);
            vertices.bind();
            vertices.draw(GL10.GL_TRIANGLES, 0, numSprites*6);
            vertices.unbind();
        }
        catch(Exception e){
        //nothing to do 
        }       
    }

Or like this:

public void endBatch(){
        if(numSprites > 0){
            vertices.setVertices(verticesBuffer, 0, bufferIndices);
            vertices.bind();
            vertices.draw(GL10.GL_TRIANGLES, 0, numSprites*6);
            vertices.unbind();
        }       
    }

I'm not an expert so I can be wrong with the solution...

What steps will reproduce the problem?
1. Create a new SpriteBatcher.
2. Call method beginBatch(Texture)
3. Call method endBatch() without calling drawSprite before.

Miquel Vidal.

Original issue reported on code.google.com by kelovi...@gmail.com on 10 Jan 2012 at 5:01