Well, I tried rendering the grass every frame and this time it worked without a big fps drop. I don’t know why it worked this time but I’m happy. Perhaps I forgot to remove another piece of code when I was testing it before. This is the code I’ve ended up with:
In the Main Class:
package com.src.raingame;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
public class Boot {
long lastFrame;
int fps;
long lastFPS;
private int playerX, playerY;
private int playerSpeed;
Player p;
Grid g;
Texture grass;
Texture player;
public static void main(String[] args) {
new Boot();
}
public Boot() {
boolean running = false;
p = new Player();
// Creating a new display
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
// All the initializations
Grid.loadGrass();
p.loadPlayer();
playerSpeed = 100;
playerX = p.getPlayerX();
playerY = p.getPlayerY();
initGL();
init();
grass = g.getGrass();
player = p.getPlayer();
running = true;
// Main Loop
while (running) {
if (Display.isCloseRequested()) {
running = false;
}
render();
updateFPS();
Display.update();
Display.sync(60);
}
// Getting rid of the display
Display.destroy();
System.exit(0);
}
// Returns the time
public long getTime() {
return System.nanoTime() / 1000000;
}
// Calculates the delta and then returns it
public int getDelta() {
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
// Updating the FPS once per second and setting the title
public void updateFPS() {
if (getTime() - lastFPS > 1000) {
Display.setTitle("Some name pre alpha :: FPS: " + fps);
fps = 0;
lastFPS += 1000;
}
fps++;
}
public void init() {
getDelta();
lastFPS = getTime();
g = new Grid();
p.drawPlayer();
}
// Initializing the openGL
public void initGL() {
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Enabling alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
public void render() {
glClear(GL_DEPTH_BUFFER_BIT);
grass.bind();
glBegin(GL_QUADS);
g.drawGrid();
glEnd();
checkInput();
player.bind();
p.updatePlayer();
}
public void checkInput(){
playerX = p.getPlayerX();
playerY = p.getPlayerY();
if(Keyboard.next()){
if(Keyboard.isKeyDown(Keyboard.KEY_A)){
p.setPlayerX(playerX - playerSpeed);
System.out.println("A pressed.");
}
if(Keyboard.isKeyDown(Keyboard.KEY_W)){
p.setPlayerY(playerY - playerSpeed);
System.out.println("W pressed.");
}
if(Keyboard.isKeyDown(Keyboard.KEY_S)){
p.setPlayerY(playerY + playerSpeed);
System.out.println("S pressed.");
}
if(Keyboard.isKeyDown(Keyboard.KEY_D)){
p.setPlayerX(playerX + playerSpeed);
System.out.println("D pressed.");
}
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
System.out.println("Escape key pressed. Shutting Down...");
System.exit(0);
}
}
}
}
In the Grid Class:
package com.src.raingame;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.glClear;
import java.io.IOException;
import org.lwjgl.opengl.Display;
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 Grid{
private float grassWidth;
private float grassHeight;
private float gTexWidth, gTexHeight;
static Texture grass;
static Texture player;
public Grid(){
// Clearing the screen and the depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
try{
grassWidth = grass.getImageWidth();
grassHeight = grass.getImageHeight();
}catch(NullPointerException e){
System.err.println("Getting grass image size failed. Shutting Down...");
System.exit(1);
}
try{
gTexWidth = grass.getWidth();
gTexHeight = grass.getHeight();
}catch(NullPointerException e){
System.err.println("Getting grass texture size failed. Shutting Down...");
System.exit(1);
}
grass.bind();
}
public static void loadGrass(){
try {
grass = TextureLoader.getTexture("PNG",
ResourceLoader.getResourceAsStream("res/grass.png"));
} catch (IOException e) {
e.printStackTrace();
System.err.println("Grass file not found, Shutting down...");
System.exit(1);
}
}
public void drawGrid(){
for (int i = 0; i < Display.getWidth() / grassWidth; i++) {
for (int b = 0; b < Display.getHeight() / grassHeight; b++) {
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(i * grassWidth,b * grassHeight);
GL11.glTexCoord2f(gTexWidth, 0);
GL11.glVertex2f(i * grassWidth + grassWidth, b* grassHeight);
GL11.glTexCoord2f(gTexWidth, gTexHeight);
GL11.glVertex2f(i * grassWidth + grassWidth,b * grassHeight + grassHeight);
GL11.glTexCoord2f(0, gTexHeight);
GL11.glVertex2f(i * grassWidth,b * grassHeight + grassHeight);
}
}
}
public Texture getGrass(){
return grass;
}
}
And in the Player Class:
package com.src.raingame;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import java.io.IOException;
import org.lwjgl.opengl.Display;
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 Player {
Texture player;
private int playerX;
private int playerY;
private float playerWidth, playerHeight, pTexWidth, pTexHeight;
public Player(){
playerX = 25;
playerY = 0;
}
public void loadPlayer(){
try {
player = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/player.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void drawPlayer(){
playerWidth = player.getImageWidth();
playerHeight = player.getImageHeight();
pTexWidth = player.getWidth();
pTexHeight = player.getHeight();
player.bind();
}
// Updating player position
public void updatePlayer(){
glBegin(GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(playerX,playerY);
GL11.glTexCoord2f(pTexWidth, 0);
GL11.glVertex2f(playerX + playerWidth, playerY);
GL11.glTexCoord2f(pTexWidth, pTexHeight);
GL11.glVertex2f(playerX + playerWidth, playerY + playerHeight);
GL11.glTexCoord2f(0, pTexHeight);
GL11.glVertex2f( playerX, playerY + playerHeight);
glEnd();
}
// Player position
public int getPlayerX() {
return playerX;
}
public void setPlayerX(int playerX) {
if(playerX <= Display.getWidth() && playerX >= 0){
this.playerX = playerX;
}
}
public int getPlayerY() {
return playerY;
}
public void setPlayerY(int playerY) {
if(playerY <= Display.getHeight() - playerHeight && playerY >= 0){
this.playerY = playerY;
}
}
// Player image sizes and texture sizes
public float getPlayerWidth() {
return playerWidth;
}
public float getPlayerHeight() {
return playerHeight;
}
public float getpTexWidth() {
return pTexWidth;
}
public float getpTexHeight() {
return pTexHeight;
}
public Texture getPlayer(){
return player;
}
}
I hope I did everything as efficiently as possible. Also, sorry if this is too much code.
Thanks for everything you guys,
Longarmx