我正在开发一个简单的2D游戏,通过Java2D API渲染。我注意到,当我尝试在集成显卡上绘制时,性能崩溃。
我已经在我的主装备上用更新的ATI Radeon和我5岁的笔记本电脑测试了这款游戏,该笔记本电脑还有一个(令人难以置信的过时)Radeon。在这两个方面,我都得到了很好的 FPS,但是当我尝试使用我的英特尔 i5 的板载高清 4000 显卡时,它以大约 20 FPS 的速度爬行。
我使用的是全屏独占模式。
在任何给定的时刻,我一次渲染大约 1000 张图像。
令人讨厌的是,当我尝试获取AvailableAcceleratedMemory()时,它只为这张卡返回-1,并且似乎拒绝加速任何图像。
有人有任何想法如何解决这个问题吗?
渲染代码:
Graphics g = bufferStrategy.getDrawGraphics();
g.drawImage(img, x, y, img.getWidth(), img.getHeight(), null)
g.dispose();
bufferStrategy.show();
图像加载代码:
BufferedImage I = null;
I = ImageIO.read(new File(currentFolder+imgPath));
imgMap.put(imgIdentifier, I);
图像存储在由字符串标识的 BufferedImages 哈希图中,因此当实体需要绘制和图像时,它只是将其从哈希映射中取出并绘制它。在当前情况下,实体大多是地板和墙砖,因此它们永远不会改变(因此除了第一次之外,不必从哈希图中获取图像)。
编辑 - 我已经合并了MadProgrammer的方法,但它没有改变我的FPS。
这是将图像转换为兼容图像的示例...本身不是答案
这是我使用的一些库代码...
public static BufferedImage createCompatibleImage(BufferedImage image) {
BufferedImage target = createCompatibleImage(image, image.getWidth(), image.getHeight());
Graphics2D g2d = target.createGraphics();
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return target;
}
public static BufferedImage createCompatibleImage(BufferedImage image,
int width, int height) {
return getGraphicsConfiguration().createCompatibleImage(width, height, image.getTransparency());
}
public static GraphicsConfiguration getGraphicsConfiguration() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}
我会做这样的事情...
I = createCompatibleImage(ImageIO.read(new File(currentFolder+imgPath)));
imgMap.put(imgIdentifier, I);