views:

805

answers:

2

Hello,

following this question : http://stackoverflow.com/questions/2125354/best-approach-for-oldschool-2d-zelda-like-game

Thank to previous replies, and with a major inspiration from http://insanitydesign.com/wp/projects/nehe-android-ports/ , i started to build a simple Tile Generator for my simple 2D zelda-like game project.

I can now generate a map with the same textured tile, using 2 for(..) imbricated iterations to draw horizontal and vertical tiles, and got some basic DPAD key input listeners to scroll over the x and y axis.

but now im running into my first performance problems, just with one texture and one model.

When trying to build a 10x10 map, scrolling is fine and smooth.

When trying with 50x50, things get worse, and with a 100x100, its way unacceptable.

Is there a way only to tell OpenGL to render the 'visible' part of my mapset and ignore the hidden tiles? im a totally new to this.

im using

GLU.gluLookAt(gl, cameraPosX, cameraPosY, 10.0f,cameraPosX, cameraPosY, 0.0f, 0.0f, 1.0f, 0.0f);

to set the camera and point of view for a 2D-style feeling.

Any help ? :)

for (int j = 0; j < 10; j++) {

        for (int i = 0; i < 10; i++) {

            gl.glPushMatrix(); // Sauvegarde la matrice sur le stack

            //Bind the texture according to the set texture filter
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[filter]);
            //Set the face rotation
            gl.glFrontFace(GL10.GL_CW);
            //Enable texture state
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            //Enable vertex state
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            //Point to our vertex buffer
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
            //point to our texture buff
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
            //Draw the vertices as triangle strip
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
            //Disable the client state before leaving
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

            gl.glTranslatef(1.0f, 0.0f, 0.0f); // on avance d'une tile
        }
        // on va commencer a dessiner la 2e ligne
        gl.glPopMatrix(); // Rappelle la matrice sur le stack
        gl.glTranslatef(0.0f, -1.0f, 0.0f);
    }
+1  A: 

You can easily make your loop to draw only the visible aria. Here is some example how it needs to be done. I don't know the android API so thread my example as metacode.

int cols = SCREEN_WIDTH / TILE_SIZE + 1;      // how many columns can fit on the screen
int rows = SCREEN_HEIGHT / TILE_SIZE + 1;     // haw many rows can fit on the screen
int firstVisibleCol = cameraPosX / TILE_SIZE; // first column we need to draw
int firstVisibleRow = cameraPosY / TILE_SIZE; // first row we need to draw

// and now the loop becomes
for (int j = firstVisibleRow; j < rows; j++) {
    for (int i = firstVisibleCol ; i < cols; i++) {
        ...
    }
}
Aleks
thanks the meta-idea worked for me :)
Dullahx
+1  A: 

The reason why the loop gets slow is that it makes OpenGL to do lots of unnecessary work. This is because there are lots of redundant state changes.

That means that you are calling gl functions with parameters that doesn't have any effect. Calling these functions eat up a lot of CPU time and might cause the whole OpenGL pipeline to stall as it cannot work very effectively.

For example you should call glBindTexture only if you want to change the texture used. The above code binds the same texture over and over again in the inner loop which is very expensive. Similarly you don't need to enable and disable texture coordinate and vertex arrays in the inner loop. Even setting texture coordinate pointer and vertex pointer in the inner loop is unnecessary as they don't change between subsequent loops.

The bottom line is, that in the inner loop you should only change translation and call glDrawArrays. Everything else just eats up resources for nothing.

There are more advanced things you can do to speed this up even more. Tile background can be drawn so that it causes only one call to glDrawArrays (or glDrawElements). If you are interested in, you should Google topics like batching and texture atlases.

Lauri