so i am working on a game (i have just started).
and i have a problem: when i launch the game, everything is fine.
but as i move on right or down, the FPS drops a lot.
this is the code i am using:
main:
package main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import world.World;
import static org.lwjgl.opengl.GL11.*;
public class Main {
public static Texture air, dirt, grass, player;
private World world;
public static int width = 800;
public static int height = 640;
private static int x = 0, y = 0;
public int currentFPS = 0;
public int FPS = 0;
public long start = 0;
public static void main(String[] args) {
new Main();
}
public Main() {
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle("Blocks Fighter");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
init();
loadtextures();
world = new World(width, height);
gameloop();
}
private void loadtextures() {
try {
air = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/air.png")));
dirt = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/dirt.png")));
player = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/player.png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
} catch (RuntimeException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}
}
private void gameloop() {
while(!Display.isCloseRequested()) {
update();
render();
Display.update();
Display.sync(60);
getfps();
}
Display.destroy();
}
private void getfps() {
currentFPS++;
if(System.currentTimeMillis() - start >= 1000) {
FPS = currentFPS;
currentFPS = 0;
start = System.currentTimeMillis();
System.out.println(FPS);
}
}
private void render() {
glClear(GL_COLOR_BUFFER_BIT);
world.render(x,y);
renderPlayer(player, 32, 32);
}
private void update() {
if (Keyboard.isKeyDown(Keyboard.KEY_D)) x-=3;
if (Keyboard.isKeyDown(Keyboard.KEY_S)) y-=3;
if (Keyboard.isKeyDown(Keyboard.KEY_W)) y+=3;
if (Keyboard.isKeyDown(Keyboard.KEY_A)) x+=3;
}
private void init() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Resets any previous projection matrices
glOrtho(0, width, height, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
}
public static void renderBlock(Texture texture, int x, int y) {
if (x + 32 < 0 || x > width) return;
if (y + 32 < 0 || y > height) return;
if (!(x < 0) && !(y < 0) && !(y > height) && !(x > width)){
texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(x, y);
glTexCoord2f(1, 0);
glVertex2i(x + 32, y);
glTexCoord2f(1, 1);
glVertex2i(x + 32, y + 32);
glTexCoord2f(0, 1);
glVertex2i(x, y + 32);
glEnd();
}
}
public static void renderPlayer(Texture texture, int pwidth, int pheight) {
int middlex = width / 2 + pwidth /2;
int middley = height / 2 + pheight / 2;
texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2i(middlex - pwidth / 2, middley - pheight / 2);
glTexCoord2f(1, 0);
glVertex2i(middlex + pwidth / 2, middley - pheight / 2);
glTexCoord2f(1, 1);
glVertex2i(middlex + pwidth / 2, middley + pheight / 2);
glTexCoord2f(0, 1);
glVertex2i(middlex - pwidth / 2, middley + pheight / 2);
glEnd();
}
}
/* How to add stuff:
* textures- add a variable and a load at the "loadtextures" method.
* block texture- add a texture and add an if loop with the id at block render.
*/
world:
package world;
import java.util.Random;
import main.Main;
import block.block;
public class World {
private int width;
private int height;
public block[] blocks;
private Random random = new Random();
public World(int width, int height) {
this.width = width;
this.height = height;
blocks = new block[width * height];
for (int i = 0; i < width * height; i++){
int randomn = random.nextInt(2);
blocks[i] = new block(randomn);
}
}
public void render(int xp, int yp) {
for (int x = 0; x + xp < width; x++){
if (x > Main.width / 32 - xp) continue;
for (int y = 0; y + yp < height; y++){
if (y > Main.height / 32 - yp) continue;
int arrayPosition = x + y * width;
if(arrayPosition >= blocks.length) break;
blocks[arrayPosition].render(x * 32 + xp, y * 32 + yp);
}
}
}
}
block:
package block;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import main.Main;
import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import world.World;
import static org.lwjgl.opengl.GL11.*;
public class block {
/* ID list
* 0 = air
* 1 = dirt
*/
private int typeID = 0;
private Texture texture;
public block(int ID) {
this.typeID = ID;
}
public void render(int x, int y) {
//check for id, and getting the texture from the class "Main"
if (typeID == 0) texture = Main.air;
else if (typeID == 1) texture = Main.dirt;
else texture = Main.air;
Main.renderBlock(texture, x, y);
}
}
thanks!
Because your drawing 800x600 = 480.000 blocks in immediate mode, go take a look into VBOs and chunks