1

Originally I was using the canvas to draw my bitmaps in a 2d real time action type game, but for some reason my frame rate was terrible. I suspected it was the canvas so I switched to opengl. From surfing on the internet I learned to create a rectangle from 2 triangles and set a texture on it. I used glOrthof to set it up the 2d perspective and used the gltranslatef method to move my textures. I know the problem isn't the physics or anything because I tested moving a single texture at a constant velocity, (The x value moving at about 7 units per 33 milliseconds). It was still choppy. I set the fps to 30. This is my game loop:

public void onDrawFrame(GL10 gl) {

    startTime = System.nanoTime();

    gl.glClearColor(0f, .0f, .8f, 0.5f);

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    g.updatePhysics();
    onDraw(gl);

    sleepTime = (int) (TICKS_INTERVAL - (System.nanoTime() - startTime)/1000000);

    if(sleepTime > 0){
        try{
            Thread.sleep(sleepTime);
        }catch(Exception e){}
    }
}

and this is how they're being drawn:

public void onDraw(GL10 gl){

    gl.glLoadIdentity();

    if(g.state == g.STATE_GAME){

        gl.glTranslatef(x, y, z);
        rectangle.draw(gl);

        gl.glLoadIdentity();
        gl.glTranslatef(x, y, z);
        rectangle2.draw()gl;
    }
}

And this is the draw method within the rectangle:

public void draw(GL10 gl){
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glFrontFace(GL10.GL_CW); 

    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuff);

    gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);// Draw the vertices as triangle strip
    //gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}

Even when I set the frame rate to 60 there's still some stuttering, and it's frustrating because for the past month I've been just trying to get the frame rate smooth. I'm testing on a galaxy s2 so I know it isn't the hardware. If all this code is right, do you think maybe it could be something else in the activity? My GLSurfaceView is actually a custom view. Even getting a single texture to move smoothly at a constant velocity would be nice.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Sharkbird
  • 61
  • 6
  • 30 fps can be too demanding at times; try bumping it down to 29.97 or possibly 24 to see if there's any improvement or if it's worse. – Joe Habadas Aug 31 '12 at 16:26
  • I would do some google searching for more advanced tips on game loop design. For example, http://gafferongames.com/game-physics/fix-your-timestep/ – mbeckish Aug 31 '12 at 16:50
  • are you updating your entity's position using the delta time? you will see stuttering until you do. – Siamak M. Mirzaie Aug 31 '12 at 19:39

0 Answers0