he is ppl!
im writing a game. since not much has changed over time, I switched to buffering parts of the board (on which the game was played) and copied it from time to time. I am changing the content in the foreground, so I still need high fps. I also want to zoom in, and here the fun begins: to save memory, I reuse buffers. Whenever I increase application latency, it runs fine.
After profiling, I came with two manufacturers:
cleaning the backbuffer (4000x4000 pixels, takes about 29 ms. To maintain transparency, I use g.fillRect)
Copying the buffer back to the real image (which, of course, is not live, but again from getBufferStrategy ()). It takes 300 ms, next time about 150, then from the third frame it works smoothly.
To find out where the problem may be a bit of code. I create my own buffers as follows:
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice device = env.getDefaultScreenDevice(); GraphicsConfiguration config = device.getDefaultConfiguration(); image = config.createCompatibleImage(width, height,Transparency.TRANSLUCENT);
And now the part where I copy the buffer back to the image. Please note that I need to cut out some parts of the buffer, so I went to the call with the maximum parameter.
g.drawImage(image, vs.boardOffsetX, vs.boardOffsetY, targetWidth, targetHeight, 0, 0, sourceWidth, sourceHeight, null);
Finally, for my other problem: I clean the images as follows:
Graphics2D g = (Graphics2D) image.getGraphics(); Color transparent = new Color(0, 0, 0, 0); g.setColor(transparent); g.setComposite(AlphaComposite.Src); g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
Thank you very much! stuck on this for quite some time. And feel free to give me stylistic advice on my approach: his first honest attempt to make graphics.
Thanks!
edit: The part that I really don't understand is that the exact same operation takes so many times. Since only AWT-Thread works in addition to my thread, I get twice ~ 300 ms, and then it down like 10 ฮผs !!!! and it is AWFULLY fast for copying 16 million pixels. Does anyone understand this effect? and perhaps knows a way to โpre-optimizeโ this behavior?