For practice I'm making a 2D Jump n Run with a randomly generated world. My problem is that if I use more than one display list my fps start to drop. But with one list they improve. I'm using Java with lwjgl.
The world is saved into a 2D array:
public static void generate() {
//generate random world
for (int col = 0; col < cols; col++) {
for (int row = 12; row < rows; row++) {
a[col][row]= (int) (Math.random()*3);
}
}
//print world array to console
for (int i =0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(" " + a[j]);
}
System.out.println("");
}
}
public static void init() {
// print world
for (int col = 0; col < cols; col++) {
for (int row = 6; row < rows; row++) {
if(a[col][row]== 0) Blocks.loadSky(col,row); //why load sky?
if(a[col][row]== 1) Blocks.loadDirt(col,row);
if(a[col][row]== 2) Blocks.loadRock(col,row);
}
}
}
"generate()" generates the world at start and "init()" runs in my game loop and draws the world.
I commented out my first approach without display lists. I had around 1400 fps at that time.
Then I created only the dirt display list and my fps jumped up to 2700. (when the fps were 2700 the rocks were also rendering just without the list)
But after I implemented my rock display list the fps dropped to 1500.
static int scale = 32;
static int dirt;
static int rock;
//compile() compiles all block types which are later called by loadDirt etc...
public static void compile() {
//number of lists:
dirt = glGenLists(2);
//dirt block
glNewList(dirt, GL_COMPILE);
textureLoader.dirt.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(-16,-16);
glTexCoord2f(1.0f,0);
glVertex2f(-16,16);
glTexCoord2f(1.0f,1.0f);
glVertex2f(16,16);
glTexCoord2f(0,1.0f);
glVertex2f(16,-16);
glEnd();
glEndList();
rock = dirt+1;
glNewList(rock, GL_COMPILE);
textureLoader.rock.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(-16,-16);
glTexCoord2f(1.0f,0);
glVertex2f(-16,16);
glTexCoord2f(1.0f,1.0f);
glVertex2f(16,16);
glTexCoord2f(0,1.0f);
glVertex2f(16,-16);
glEnd();
glEndList();
}
public static void loadDirt(int col, int row) {
/*
textureLoader.dirt.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(scale * col - scale/2,scale * row - scale/2);
glTexCoord2f(1.0f,0);
glVertex2f(scale * col - scale/2,scale * row + scale/2);
glTexCoord2f(1.0f,1.0f);
glVertex2f(scale * col + scale/2,scale * row + scale/2);
glTexCoord2f(0,1.0f);
glVertex2f(scale * col + scale/2,scale * row - scale/2);
glEnd();*/
//store current Matrix
glPushMatrix();
//col and row change all the time => find save format to save the world
//System.out.println(col);
//move block in position
glTranslatef((scale*col),(scale*row),0);
//call dirt list
glCallList(dirt);
//restore old Matrix
glPopMatrix();
}
public static void loadRock(int col, int row) {
/*
textureLoader.rock.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(scale * col - scale/2,scale * row - scale/2);
glTexCoord2f(1.0f,0);
glVertex2f(scale * col - scale/2,scale * row + scale/2);
glTexCoord2f(1.0f,1.0f);
glVertex2f(scale * col + scale/2,scale * row + scale/2);
glTexCoord2f(0,1.0f);
glVertex2f(scale * col + scale/2,scale * row - scale/2);
glEnd();*/
glPushMatrix();
glTranslatef((scale*col),(scale*row),0);
glCallList(rock);
glPopMatrix();
}
public static void loadSky(int col, int row) {
}
What I don't understand is, why is the game running faster with one list which creates only the dirt but is almost as slow as before if I use almost the same one for the rock?
Let me know if you need any more information and I hope that makes sense at all.
Thanks!
And how can I post my code an keep its format? Would probably be better to read.