Hello,
I want to advance in Java, and decided to just learn a bit of Game Development. I create the 2D engine side to side with the game. I thought that optimisation problems will occur later on, but just at the beginning of my journey, I fucked up. My frame-rate is around 70 fps just for an empty window. If I don’t use a window, and simply write the elements to the buffer, I am ten times faster. I decided to stick with AWT for the beginning, and later on switch to openGL support. My (fairly) reduced setup looks like this:
The GameLoop:
public void run(){
while (isRunning){
boolean render = true;
while (unprocessedTime >= frameCap){
//update game
//update inputs
}
if(render){
renderer.clear(); //clears the buffered image -> sets default background colour for every pixel
game.render(this,renderer); //the elements are written to the buffer
window.update(image); //the buffer gets written to the graphics object and shown
}
}
//cleanUp
}
The image is a BufferedImage. The window class looks like this, and is heavily inspired by the documentation.
public class Display extends Frame {
public Display() {
//set initial stuff and call super
createBufferStrategy(3);
strategy = getBufferStrategy();
}
public void update(BufferedImage image){
do {
do {
Graphics g = strategy.getDrawGraphics();
//we draw the buffered image to the graphics object
g.drawImage(image,0,0,image.getWidth(),image.getHeight(),null);
g.dispose();
} while (strategy.contentsRestored());
// Display the buffer
strategy.show();
} while (strategy.contentsLost());
}
}
If I comment everything in the gameloop out, I get a “framerate” of over 1000 on my laptop. Simply adding this update method for a frame of size 800*800 drops the framerate to around 60, without even rendering a single thing. I do not know why writing an empty buffer is so expensive, or if I’m doing something completely wrong. Any advice, on how to display a buffer faster?
Thank you!