Hi, what do you think about this game loop? The code is taken from a youtube tutorial.
I’ve read a lot of articles about game loop and timesteps (including the thread on this forum: http://www.java-gaming.org/index.php?topic=24220.0), but I don’t understand when the cpu is highly used.
I think this code is good, but i don’t know if the cpu can be stressed using it (i don’t see any Thread.sleep() in the code).
Can you say me if there are important problems?
package engine.test.game;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable
{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 100;
public static final int HEIGHT = 60;
public static final int SCALE = 6;
public static final String TITLE = "Game";
private Thread thread;
private JFrame frame;
private boolean running = false;
private Screen screen;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
public Game()
{
Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
setPreferredSize(size);
screen = new Screen(WIDTH, HEIGHT);
frame = new JFrame();
}
public synchronized void start()
{
running = true;
thread = new Thread(this, "Game");
thread.start();
}
public synchronized void stop()
{
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run()
{
long now = 0;
long timer = System.currentTimeMillis();
long startTime = System.nanoTime();
long lastTime = System.nanoTime();
long stopTime = System.nanoTime() + 5000000000L;
double ns = 1000000000 / 60;
double delta = 0;
int updates = 0;
int frames = 0;
// execute for a fixed time, i don't need to execute it more than some minutes
while (now <= stopTime) {
now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
update();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " upd, " + frames + " fps");
frame.setTitle(TITLE + " | " + updates + " upd, " + frames + " fps");
updates = 0;
frames = 0;
}
}
long endTime = System.nanoTime();
System.out.println("Duration: " + ((endTime - startTime)/1000000) + "ms");
stop();
}
public void update()
{
}
public void render()
{
BufferStrategy bs = getBufferStrategy();
if (null == bs) {
createBufferStrategy(3);
return;
}
screen.clear();
screen.render();
for (int i = 0, l = pixels.length; i < l; i++) {
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
public static void main(String[] args)
{
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle(TITLE);
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setVisible(true);
game.frame.setLocationRelativeTo(null);
game.start();
}
}
Thanks in advance and sorry for my bad english (correct me if you note mistake).