I'm working on a basic LWJGL game to prepare for the Ludum Dare this weekend.
I have barely any code, but as soon as I implemented Textures in my grid, I've been getting terrible FPS!
Here's some of my code:
The Screen class (used to render the game) (Scroll down to the drawGrid() method)
package game;
import game.blocks.Block;
import game.entities.EntityLiving;
import game.entities.Human;
import game.world.Grid;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
public class Screen {
//Screen Constructor
public Screen() {
try {
Display.setDisplayMode(new DisplayMode(Game.WIDTH*2,Game.HEIGHT*2));
Display.create();
init();
while(!Display.isCloseRequested()) {
render();
}
Display.destroy();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
}
//OpenGl Initialization
public void init() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Game.WIDTH, Game.HEIGHT, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glEnable(GL11.GL_TEXTURE_2D);
}
public void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
Display.sync(60);
Grid grid = new Grid();
drawGrid(grid);
drawEntity(new Human(64,64));
Display.update();
System.out.println("render");
}
public void drawEntity(EntityLiving e) {
Texture tex = e.getTexture();
tex.bind();
GL11.glColor4f(1, 1, 1, 1);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(e.getX(),e.getY());
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(e.getX()+e.getWidth(),e.getY());
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(e.getX()+e.getWidth(),e.getY()+e.getHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(e.getX(),e.getY()+e.getHeight());
GL11.glEnd();
}
public void drawGrid(Grid grid) {
Block[][] level = grid.getGrid();
for(int i = 0; i < Grid.WIDTH; i++) {
for(int j = 0; j < Grid.HEIGHT; j++) {
Texture tex = level[i][j].getTexture();
tex.bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(i*16,j*16);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(i*16+16,j*16);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(i*16+16,j*16+16);
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(i*16,j*16+16);
GL11.glEnd();
}
}
}
}
The Grid Class
package game.world;
import game.Game;
import game.blocks.Block;
import game.blocks.Grass;
public class Grid {
public static final int HEIGHT=Game.HEIGHT/16;
public static final int WIDTH=Game.WIDTH/16;
public Block[][] grid;
public Grid() {
grid = new Block[WIDTH][HEIGHT];
for(int i = 0; i < WIDTH; i++) {
for(int j = 0; j < HEIGHT; j++) {
grid[i][j] = new Grass();
}
}
}
public Block[][] getGrid() {
return grid;
}
}
The Block Class
package game.blocks;
import java.io.IOException;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Block {
public Texture tex;
public Block(String texname) {
try {
tex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/images/blocks/" + texname + ".png"), GL11.GL_NEAREST);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
public Texture getTexture() {
return tex;
}
}
Let me know if I should include anymore code. Thanks.