集成图形上的慢Java2D绘图

时间:2013-10-21 04:48:03

标签: java performance java-2d 2d-games

我正在开发一个简单的2D游戏,通过Java2D API渲染。我注意到,当我尝试使用集成显卡时性能崩溃。

我已经在我的主钻机上测试了这款游戏,其中包括更新的ATI Radeon和我5岁的笔记本电脑,它还有一个(令人难以置信的过时)Radeon。两者都得到了很好的FPS,但是当我尝试使用我的Intel i5的板载HD 4000显卡时,它的速度大约是20 FPS。

我正在使用全屏独占模式。

在任何特定时刻,我一次渲染大约1000张图像。

令人讨厌的是,当我尝试getAvailableAcceleratedMemory()时,它只为此卡返回-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。

1 个答案:

答案 0 :(得分:2)

这是将图像转换为兼容图像的示例......本身并不是答案

这是我使用的一些库代码......

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);