I am making a bomberman game in Java, the game seems to be slowing the PC overloading the processors and CPU. I have used the loop in another game and it worked fine without causing any performance problem. However I did notice that by simply commenting out the gameloop() will cause major performance issue
The game loop I'm using:
public void start() {
window.setVisible(true);
running = true;
int fps = 60;
double timePerTick = 1e9 / fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
while (running) {
now = System.nanoTime();
delta += (now - lastTime) / timePerTick;
lastTime = now;
if (delta >= 1) {
gameLoop(); /* commenting this out will cause major performance
* problem to the PC */
delta--;
}
}
}
@Override
public void gameLoop() {
if(!gameover){
run();
gui.repaint();
}
}
Is there any problem with this loop which could be causing problem, or is there a better method which will reduce the performance issues
[EDIT] I changed the loop to do a similar task with timer, it works fine and does not cause any performance issues, anything still wrong with this method (long term issue)??
public void start(int fps){
window.setVisible(true);
running = true;
gameTimer.scheduleAtFixedRate(new GameTimer(), 0, 1000/fps);
}
private class GameTimer extends TimerTask{
@Override
public void run(){
gameLoop();
}
}