Hi guys. Thanks in advance for the help.
I’m whipping up a 2D engine for the first time. I’ve copied my render loop below. What seeking help for here is a better understanding of BufferStrategy and Buffering to an offscreen Image, when they should be employed, and if I’m doing it wrong.
One approach I’ve read about is reading images into an array of pixels then using a blit method to insert them on a per pixel basis to an offscreen image, then painting the image. I believe I’ve began preparing for this method below.
Would it be a better idea just to read the image into a BufferedImage and then use the drawImage() method to draw it onto the Graphics of the BufferStrategy? Moreover, when I draw text and lines would I be drawing them onto the offscreenGraphics? or the Graphics of the BufferStrategy?
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
requestFocus();
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.translate((getWidth() - GAME_WIDTH * SCALE) / 2, (getHeight() - GAME_HEIGHT * SCALE) / 2);
g.clipRect(0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE);
offscreenGraphics.setBackground(Color.WHITE);
offscreenGraphics.clearRect(0, 0, getWidth(), getHeight());
offscreenGraphics.setColor(Color.BLACK);
offscreenGraphics.drawLine(0, 0, 100, 100);
// Stress test of changing each pixel individually.
for (int x = 0; x < GAME_HEIGHT; x++) {
for (int y = 0; y < GAME_WIDTH; y++) {
pixels[(x * GAME_WIDTH) + y] = 0xFF3333;
}
}
// Testing drawing a line
offscreenGraphics.draw(new Line2D.Double(5, 5, 500, 500));
// Testing drawing a string.
offscreenGraphics.drawString(Integer.toString(framesPerSecond), 100, 100);
g.drawImage(offscreenImage, 0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE, 0, 0, GAME_WIDTH, GAME_HEIGHT, null);
g.dispose();
bs.show();
}
Other code that I’m using is:
renderHints.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
offscreenGraphics.setRenderingHints(renderHints);